Need help about collection?

Posted on 2005-05-13
Last Modified: 2010-03-31

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...
Question by:ala
    LVL 86

    Expert Comment

    '?' means an untyped 'wildcard' parameter and 'E' means element
    LVL 1

    Accepted Solution

    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:
    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();
       // 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.

    Author Comment


    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.


    Author Comment

    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...
    LVL 1

    Expert Comment

    >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
    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:
    typewise becomes
    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.
    LVL 1

    Expert Comment

    >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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now