• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 231
  • Last Modified:

Need help about collection?

Hi,

I was Java programmer 3 years before. Now I am trying to revise Java. I am stuck on collection. I know that they are collection of objects but what it means <E>?

for example:

public static <E> void swap(List<E> a, int i, int j {
     E tmp...
... logic to swap the elements, which I understand but declaration style I dont understand.
}
- Also in List interface it declares a method like this

abstract boolean addAll(int index, Collection<? extends E> c );
Can you explain tha specially '?' sign...
Thanks
0
ala
Asked:
ala
  • 3
  • 2
1 Solution
 
CEHJCommented:
'?' means an untyped 'wildcard' parameter and 'E' means element
0
 
frank_van_puffelenCommented:
CEHJ is correct. Basically it means the swap method you have is now parameterized. Let's say you have a list of String objects, you'd now specify that as:
   List<String>
When you then invoke the swap method in this list, it will return a String. You won't need to cast it to a String yourself anymore. The JVM will take of the cast for you (or actually the compiler will).

If you pass a List<Integer> then the result will be an Integer. Once again, the casting is handled for you.

The addAll method is a bit more involved. Basically if you've got a Collection of Integer objects and a Collection of your own custom sub-class MyInteger, you'd be able to add the MyIntegers to the collection of Integers. But adding the Integers to the collection of MyIntegers won't compile as it would violate the Collection<MyInteger> contract.
   class MyInteger extends Integer {}
   Collection<Integer> ints = new HashMap();
   Collection<MyInteger> myInts = new HashMap();
   ints.addAll(myInts);
   // this won't compile: myInts.addAll(ints);

The first addAll compiles as the
   ? extends E
boils down to something like
  MyInteger extends Integer
which is true.

In the second addAll we're saying something like
   Integer extends MyInteger
which is not true and thus will not compile.
0
 
alaAuthor Commented:
Hi,

Thanks both of you. I understand all now but the last part (about addAll) of 'frank_van_puffelen', I could not understand.

Do you mean that the function below is wrong and would not complile?
addAll(int index, Collection<? extends E> c );

This function i just copied from sun tutorial.
Please can you explain it in more detail.
Thanks for both of you.


0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
alaAuthor Commented:
One more quick question.

CEHJ said  'E' means element.
Is element is java object or some special type or just means simple element... like unit of some thing...
 
0
 
frank_van_puffelenCommented:
>Do you mean that the function below is wrong and would not complile?
>  addAll(int index, Collection<? extends E> c );
The function itself will compile fine. But when you start using the examples, the one with
   myInts.addAll(ints);
will not compile. That's because your trying to add Integer objects to a Collection that is initialized to contain MyInteger objects. Before Java 1.5 this would have caused a runtime exception. But since Java 1.5 the compiler is able to catch the error.

The fragment from the tutorial you used:
   addAll(Collection<? extends E> c )
tells the compiler that it's ok to call this method with a Collection c that's initialized to contain any type that extends the type that "this" collection was initialized with.

So in my example myInts is a collection initialized to contain just MyInteger objects and ints is a collection initialized to contain any Integer objects (including MyInteger objects). So this:
   myInts.addAll(ints);
typewise becomes
   Collection<MyInteger>.addAll(Collection<Integer>);
After executing this addAll it would not be guaranteed that the myInts collection contains only instances of the MyInteger class.

Let me know if this isn't clear, because I should be able to come up with a better example.
0
 
frank_van_puffelenCommented:
>CEHJ said  'E' means element.
>Is element is java object or some special type or just means simple element...
>like unit of some thing...
E in these examples is the type that the Collection was initialized with.

So if you have a List that is to contain only String objects (or instances of subclasses of String), you would initialize it like this:
   List<String> strings = new LinkedList();
After this, the E for this string collection would be the String class. So the swap method would require a List<String> parameter and return a String. And the addAll method would take any Collection<String> as a parameter or a collection initialized to contain a subclass of String.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now