Set vs. set<?>

Source: Internet
Author: User

Know that's unbounded wildcard set<?> can hold elements of any type, and a raw type Set can also hold Eleme NTS of any type. What's the difference between them?

Facts about Set<?>

There is facts about Set<?>:
Item 1:since the question mark? stands for any type. Set<?> is capable of holding any type of elements.  
Item 2:because we don ' t know the type of, we can ' t put any element into set<?>

So a set<?> can hold any type of element (item 1), but we can ' t put any element into it (item 2). Do the-the-statements conflict to each other? Of course they is not. This can is clearly illustrated by the following and the examples:

Item 1 means the following situation:

Legal codepublic 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"));p rintset (S2);} public static void Printset (Set<?> s) {for (Object o:s) {System.out.println (o);}}

Since set<?> can hold any type of elements, we simply use Object in the loop.

Item 2 means the following situation which is illegal:

Illegal codepublic static void Printset (Set<?> s) {S.add]//this line was illegal for (Object o:s) {System.out . println (o);}}

Because we don ' t know the type of <?> exactly, we can not add any thing to it other than null. For the same reason, we can not be initialize a set with Set<?>. The following is illegal:

Illegal codeset<?> set = new hashset<?> ();

Set vs. set<?>

What ' s the difference between raw type Set and unbounded wildcard set<?>?

This method declaration is fine:

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

Because raw type has no restrictions. However, this would easily corrupt the invariant of collection.

In brief, wildcard type was safe and the raw type is not. We can not put any element into a set<?>.

When set<?> is useful?

When your want to use a generic type, but don't know or care what the actual type the parameter are, you can use <?&G T [1]. It can only is used as parameters for a method.

For example:

public static void Main (string[] args) {hashset<integer> S1 = new Hashset<integer> (arrays.aslist (n/a)); 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.

Also like ...
    1. How is Static Type Checking Works in Java?
    2. Leetcode–merge Sorted Array (Java)
    3. How to Convert an Array to ArrayList in Java?
    4. Leetcode–permutations II (Java)

Category >> Collections >> generics >> Versus

http://www.programcreek.com/2013/12/raw-type-set-vs-unbounded-wildcard-set/

Set vs. set<?> (EXT)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.