The Artima Developer Community
Sponsored Link

Java Buzz Forum
Generics Usage beyond Collections

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Marc Logemann

Posts: 594
Nickname: loge
Registered: Sep, 2002

Marc Logemann is founder of www.logentis.de a Java consultancy
Generics Usage beyond Collections Posted: Feb 19, 2004 4:12 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Marc Logemann.
Original Post: Generics Usage beyond Collections
Feed Title: Marc's Java Blog
Feed URL: http://www.logemann.org/day/index_java.xml
Feed Description: Java related topics for all major areas. So you will see J2ME, J2SE and J2EE issues here.
Latest Java Buzz Posts
Latest Java Buzz Posts by Marc Logemann
Latest Posts From Marc's Java Blog

Advertisement

After a deeper look into the generics feature of 1.5 (and in the tutorial of gilad bracha), i doubt that there will be a widespread usage at least not in defining generics, using them in conjunction with the collection classes seems an easy goal for most people.

But why do you rarely see any complaints about the inherent complexity when going into the details?? Thats easy, all you see in the web regarding generics is this:

"Hey dudes, generics is easy look at this:"

List<String> myStringList = new ArrayList<String>();
myStringList.add("EASY!");
String s = myStringList.iterator().next();

Of course thats quite easy and this is the 95% of generics usage we will see in the next months, but perhaps people start wondering how this works behind the scenes. Thats where the fun begins and to be honest, thats where the syntax hell starts.

I still believe that most application developers wont care, but i am sure a lot of framework and API designers will and the migration will be a lot of work. But back to the basics, which can be confusing enough:

Lets start in writing a utility method which prints out collections (this way we dont have to write a generic aware class ourselves).

void printCollection(Collection<?> c) {
for(Object e : c) System.out.println(e)
}

Here we can work on any type of collection and we can reference them in the loop as Objects. Seems intuative and i dont have a problem with this, but look at this:

Collection<?> c = new ArrayList<String>;
c.add(new Object()); // <--- compile time error

? stands for "unknown Type" and i dont know how you think about it, i tend to think that i should be able to add Objects, because whatever this collection is, it should accept Objects. But it reads as "Object is not a subtype of unknown Type". Intuitive? No!

Lets take a look at generic methods:

<T> static void fromArrayToCollection(T[] a, Collection<T> c) {
for(T o:a) c.add(o);
}

If you feel a little bit weird when looking at such code, than stop thinking about creating generics aware APIs, because this is the most simple structure of such a method. Here you can pass in any array which will be added to the collection, where collection must be supertype of the array.

Look at one possible call:
String[] stringArray = new String[10];
Collection<Object> objectCollection = new ArrayList<Object>();
fromArrayToCollection(stringArray, objectCollection);

BTW it would be nice if the gilad bracha tutorial would be correct, because a lot of ArrayList creations were printed this way:
... = new ArrayList<Object>;

There is enough confusion around all this without doing such crimes or perhaps the language changed in a way that we dont need to call the contructor anymore ;-)

You should really read the tutorial, its the first "readable" indepth material about generics. And if you like such constructs (below) you cant lose.

public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll)

And if you are an API or framework designer and need some kind of binary compatability with your old API. Check DJ Java Decompiler for reverse engineer API bytecode and do some signature diffs.

Read: Generics Usage beyond Collections

Topic: List of Java and Win32 stuff Previous Topic   Next Topic Topic: box: source code available

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use