This discussion is archived
6 Replies Latest reply: Apr 25, 2012 10:54 AM by 609306 RSS

Set<Future<Integer>> set = new HashSet<Future<Integer>>()  What is going on

609306 Newbie
Currently Being Moderated
I can define a collection like this:

1) List<String> list = new ArrayList<String>();

The parts of that line are:

2) Interface<Type> CollectionName = new Implementation<Type>

I am seeing the following with no explanation about what is going on:

3) Set<Future<Integer>> set = new HashSet<Future<Integer>>()

Obviously there are a lot of similarities between lines 1 and 3, but there is additional components. Could someone break down line 3 as I did in line 2?


Thank you
  • 1. Re: Set<Future<Integer>> set = new HashSet<Future<Integer>>()  What is going on
    doremifasollatido Journeyer
    Currently Being Moderated
    The breakdown of 1 and 3 into Line 2 is exactly the same. In Line 3, the Type itself happens to be generic (hence the additional &lt; and &gt; in Line 3).
  • 2. Re: Set<Future<Integer>> set = new HashSet<Future<Integer>>()  What is going on
    609306 Newbie
    Currently Being Moderated
    The Future object is returned by the Submit method which is used to retrieve the Callable return value. But you are saying that Future is a generic type. I am not finding much that clears this up for me.

    The line above creates a HashSet of the type "Future<Integer>>" whic you say is a generic type. What role does <Integer> play? Sorry for all the questions.


    Thank you,
  • 3. Re: Set<Future<Integer>> set = new HashSet<Future<Integer>>()  What is going on
    DrClap Expert
    Currently Being Moderated
    To quote from the API documentation for Future:
    V - The result type returned by this Future's get method
    You don't need to apologize for all the questions, but you might find it more practical to read the API documentation before asking them. This one at least could have been answered in under a minute that way.
  • 4. Re: Set<Future<Integer>> set = new HashSet<Future<Integer>>()  What is going on
    796440 Guru
    Currently Being Moderated
    user606303 wrote:
    I can define a collection like this:

    1) List<String> list = new ArrayList<String>();
    Right, because the List type takes a single type Parameter for its element type. And you can do
    Map<String, Integer> map = new Hashmap<String, Integer>();
    because the Map type takes two type parameters, for its key type and its value type.

    And other classes besides collections have type parameters that they use for their own purposes.

    And if, for instance, you have a List of Maps, you might do
    List<Map> list = new ArrayList<Map>();
    But since Map also takes type parameters, you'd probably specify those too:
    List<Map<String, Integer>> list = new ArrayList<Map<String, Integer>>();
    >
    The parts of that line are:

    2) Interface<Type> CollectionName = new Implementation<Type>
    When Type itself has no type parameters of its own, yes. But if Type is itself parameterized, then you get something like the most recent example above.
    I am seeing the following with no explanation about what is going on:

    3) Set<Future<Integer>> set = new HashSet<Future<Integer>>()
    In this case, Type is Future, and Future itself takes a type parameter. As DrClap pointed out, the meaning of that particular type parameter for that particular class can be found in Future's docs, but, structurally, since a type can be parameterized by another type, as List is parmaeterized by <String> in your first example, the type parameter can itself be parameterized, theoretically to infinite depth, but I think the language puts a limit of 256 levels or something like that.
  • 5. Re: Set<Future<Integer>> set = new HashSet<Future<Integer>>()  What is going on
    Alex Geller Newbie
    Currently Being Moderated
    Try to instantiate a <i>list of list of strings</i> as you already know how to instantiate a <i>list of strings</s> and you will see that you end up similar syntax. There is a tutorial on generics at http://docs.oracle.com/javase/tutorial/java/generics/gentypes.html.
  • 6. Re: Set<Future<Integer>> set = new HashSet<Future<Integer>>()  What is going on
    609306 Newbie
    Currently Being Moderated
    jverd, thank you for your full answer. I am getting closer to an understanding.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points