What is the difference between set and set in Java?

Time:2019-11-8

As you may know, the unbounded wildcard set > can hold any type of element, and the original type set can hold any type of element. What’s the difference between them?

1. Two facts about set >

There are two facts about set >

  • Item 1:? Represents any type. Set > can hold any type of element. A kind of
  • Item 2: because we don’t know? Cannot put any element in set >.

So set > can hold any type of element (item 1), but we can’t put any element in it (item 2). Do the two statements conflict with each other? Of course not. The following two examples illustrate this clearly:

**Item 1 indicates the following:**

//Legal code
public static void main(String[] args) {
    HashSet s1 = new HashSet(Arrays.asList(1, 2, 3));
    printSet(s1);
 
    HashSet s2 = new HashSet(Arrays.asList("a", "b", "c"));
    printSet(s2);
}
 
public static void printSet(Set> s) {
    for (Object o : s) {
        System.out.println(o);
    }
}

Because set > can hold any type of element, we only use object in the loop.

**Item 3 refers to the following illegal situations:**

//Illegal code
public static void printSet(Set> s) {
    s.add(10);//this line is illegal 
    for (Object o : s) {
        System.out.println(o);
    }
}

Because we don’t fully know the type of > we can’t add anything but null. For the same reason, we cannot initialize the collection using set >. The following are illegal:

//Illegal code 
Set> set = new HashSet>();

2. Set and set >

What’s the difference between a primitive type set and an unbounded wildcard set?

This method declaration is good:

public static void printSet(Set s) {
    s.add("2");
    for (Object o : s) {
        System.out.println(o);
    }
}

Because there is no limit to the original type. However, it is easy to break the invariance of the set.

In short, wildcard types are safe, while the original type is not. We can’t put any elements into set >.

3. When is set > useful?

If you want to use a generic type, but do not know or care about the actual type of the parameter, you can use >. It can only use parameters of methods.

For example:

public static void main(String[] args) {
    HashSet s1 = new HashSet(Arrays.asList(1,2,3));
    HashSet s2 = new HashSet(Arrays.asList(4,2,3));
 
    System.out.println(getUnion(s1, s2));
}
 
public static int getUnion(Set> s1, Set> s2){
    int count = s1.size();
    for(Object o : s2){
        if(!s1.contains(o)){
            count++;
        }
    }
    return count;
}

Reference resources:

1. Bloch, Joshua. Valid Java. Addison Wesley professional, 2008.


“Every step is a thousand miles”, I hope you can: have a dream for a horse to live everywhere! Come on, boy!

Pay attention to the public number: “Java confidant”, update Java knowledge every day, and look forward to your coming!

Send “interview” to get interview materials and video strategies of batj.
Send “group” and make progress with 100000 programmers.
Send “play algorithm” to get “play algorithm” series of video tutorials.
Do not send “1024”, otherwise

What is the difference between set and set  in Java?

Recommended Today

The use of progressbarcontrol, a progress bar control of devexpress – Taking ZedGraph as an example to add curve progress

scene WinForm control – devexpress18 download installation registration and use in vs: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100061243 When using ZedGraph to add curves, the number of curves is slower if there are many cases. So in the process of adding curve, the progress needs to be displayed, and the effect is as follows     Note: Blog home page:https://blog.csdn.net/badao_liumang_qizhi […]