?
Solved

Need help about collection?

Posted on 2005-05-13
6
Medium Priority
?
228 Views
Last Modified: 2010-03-31
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
Comment
Question by:ala
  • 3
  • 2
6 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 13996228
'?' means an untyped 'wildcard' parameter and 'E' means element
0
 
LVL 1

Accepted Solution

by:
frank_van_puffelen earned 500 total points
ID: 13996668
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
 

Author Comment

by:ala
ID: 13998676
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:ala
ID: 13998691
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
 
LVL 1

Expert Comment

by:frank_van_puffelen
ID: 13998808
>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
 
LVL 1

Expert Comment

by:frank_van_puffelen
ID: 13998843
>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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month14 days, 9 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question