Java, Set and Set <?> In the end the difference between what?

As you probably know, unbounded wildcard Set <?> Can hold any type of element, and the original type Set can also accommodate any type of element. So what is the difference between them?

About two facts 1. Set <?> Of

<?> Set on two facts:

  • Item 1 :? On behalf of any type. Set <?> Can hold any type of element. _
  • Item 2: Because we do not know? Type, so you can not put any element Set <?>.

Therefore, Set <?> Can hold any type of element (Item 1), but we can not put any element which (Item 2). Are these two statements conflict with each other? of course not. The following two examples clearly illustrate this point:

** Item 1 indicates the following conditions: **

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

Since Set <?> Can hold any type of element, so we only use the Object in a loop.

** Item 3 refers to the illegal situation: **

//非法代码
public static void printSet(Set<?> s) {
    s.add(10);//this line is illegal 
    for (Object o : s) {
        System.out.println(o);
    }
}

Because we do not exactly know <?> Type, so in addition to the null, we can not add anything else. For the same reason, we can not use the Set <?> Initialize the collection. The following are illegal:

//非法代码 
Set<?> set = new HashSet<?>();

2.Set the Set <?>

Original type sets and sets unbounded wildcard <?> What is the difference?

This method is very good statement:

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

There is no limit because the original type. However, it is very easy to destroy the invariance of the collection.

In short, the wildcard type is safe, and the original type is not. We can not put any element Set <?> In.

3.Set <?> What time is useful?

If you are using generic type, but do not know or do not care about the actual type of the parameter, you can use the <?>. It can only use the method parameters.

E.g:

public static void main(String[] args) {
    HashSet<Integer> s1 = new HashSet<Integer>(Arrays.asList(1,2,3));
    HashSet<Integer> s2 = new HashSet<Integer>(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:

1. Bloch, Joshua. Effective java. Addison -Wesley Professional, 2008.


"A short step, a thousand miles," hope in the future you can: have a dream horse habitat everywhere! Come on, Junior!

No public concern: "Java confidant" update Java knowledge every day Oh, look forward to your arrival!

Send "interview" to receive information BATJ interview, video interview Raiders.
Send "Group", progress with 100,000 programmers.
Send "Fun algorithm" to receive "Fun algorithm" series of video tutorials.
Do not send "1024", or else ......

Guess you like

Origin www.cnblogs.com/java-friend/p/11612711.html
Recommended