0

I write code to clear the doubt about Pair class in java. I got my output what I expected but while compiling I got a note.

Note: MyArrayList.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

My Code is

import java.util.ArrayList;
 
class MyArrayList
{
   public static void main(String[] args)
     {
        Pair<String, Integer> p1 = new Pair("Shubham", 1804017);
    Pair<Integer, String> p2 = new Pair(1804025,"Sanket");
    Pair<Boolean, String> p3 = new Pair(true,"Sanket");

    p1.getInfo();
    p2.getInfo();
    p3.getInfo();
     }
}

class Pair<X,Y>
{
    X x;
    Y y;
    
    public Pair(X x,Y y)
      {
         this.x = x;
         this.y = y;
      }
    public void getInfo()
       {
         System.out.println(x+" "+y);  
       }
}

So What is that note and can we avoid it?

1 Answers1

1

Just use the diamond operator here.

Pair<String, Integer> p1 = new Pair<>("Shubham", 1804017);
Pair<Integer, String> p2 = new Pair<>(1804025,"Sanket");
Pair<Boolean, String> p3 = new Pair<>(true,"Sanket");

See also What is the point of the diamond operator (<>) in Java 7?

Milgo
  • 2,617
  • 4
  • 22
  • 37