ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button
  Keeping Up with the Java Joneses
Subject:   Lists and generics
Date:   2004-08-05 01:26:47
From:   slipset
Having played around with 1.5 for a while I've
come across a couple of things that are really

Firstly, deleting from a list:

for (String s : strings) {
if (foo(s)) strings.remove(s);

doesn't work and never has, since you're supposed
to use the lists iterator to remove objects. It
still does annoy me that the mentioned code
doesn't work. I know I can do it with code like

Iterator <String> i = strings.iterator();
while (i.hasNext()) {
String s = i.next();
if (foo(s)) i.remove();

but that's not the point. The point is that it
should be possible to remove things from a list
via the for-loop, or even better, if Collections
had a filter(Collection <T> c, Filter f) method
which deleted all elements of c which did (or did
not) return true for the filter f.

Secondly, overloading:

A class containing these two methods will not

void foo(List<String> list) {

void foo(List<Integer> list) {

since the both get rewritten to (I guess)
void foo(List list) {

I find this too a bit annoying.

I guess my point is that generics in Java is nice
but it still has some ways to go.

1 to 1 of 1
  1. Lists and generics
    2004-08-18 15:17:06  jleech [View]

1 to 1 of 1