tags:

views:

132

answers:

6

I recently came across a module in my application which heavily uses Collections and Lists. And yeah its in Java. So either for performance reasons or whatsoever the author has used Lists and also maps in so many places. He does the usual operations on it like deleting some elements, adding, updating the list and every time he does that, he does it by traversing the entire list. Some times the lists are in the range of 10,000 elements.

Now my question here is why there isnt a language feature which facilitates this operation ? I mean why cant we have some rudimentary SQL which can be performed on the lists and collections ?? Is there any language which has this kind of feature ?

P.S: Well, I am not sure whether this is a bit subjective and against the SO rules. But I am strongly tempted to ask this here. May be the admins can place it right. So here goes.

A: 

LINQ in .NET may be the feature you're looking for, but I don't know one for Java.

Lucero
A: 

When Java was invented, it came without iterators and collections utilities. The idea just wasn't there, yet. The only thing you had was Enumeration which felt clumsy, especially without the utility functions.

With Java 1.2, iterators and collections were added. That was a great improvement but from todays point of view, it leaves a lot of room for improvement.

To fill the gap, look at Groovy which has excellent support for collections at the language level or use helper frameworks like commons collections.

[EDIT] If the code uses ArrayList, then traversing the list is pretty cheap (as is insertion and deleting, surprisingly; todays computers are really fast at copying memory around).

Aaron Digulla
Java originally had the `Enumeration` interface which is very similar to `Iterator` except with longer method names.
finnw
(And no remove.) `Enumeration` is precisely an iterator.
Tom Hawtin - tackline
I stand corrected. What I meant was that there was no tooling support. Today, we have helper functions to sort/search/filter collections and there is for(type:collection), etc.
Aaron Digulla
+4  A: 

There are some functional-style methods in Google Collections, specifically in Collections2, Iterables and Maps.

But due to the lack of closures or compact inner-class syntax in (pre-7) Java, using them can become quite verbose.

Joachim Sauer
+6  A: 

Along with Google Collections, I'd recommend looking at LambdaJ:

List<Person> sortedByAgePersons = new ArrayList<Person>(persons);
Collections.sort(sortedByAgePersons, new Comparator<Person>() {
        public int compare(Person p1, Person p2) {
           return Integer.valueOf(p1.getAge()).compareTo(p2.getAge());
        }
});

becomes

List<Person> sortedByAgePersons = sort(persons, on(Person.class).getAge());

Lots of neat sorting, filtering and list manipulation functions.

GaryF
Pardon my ignorance, but is anything in that example LambdaJ? It looks like plain Java to me.
Joachim Sauer
I think he meant that whole chunk can be replaced by this line with LambdaJ - List<Person> sortedByAgePersons = sort(persons, on(Person.class).getAge());I think lambdaJ is the closest we can get around here.
anilit99
Oops, must've accidentally deleted the middle part. I've corrected it now. My mistake.
GaryF
Ok, that looks more non-standard now ;-)
Joachim Sauer
Now that's an interesting API. Good to bridge the gap until closures are available in JDK.
BalusC
A: 

I read the example mentioned above for LambdaJ. It does shorten the code but, I am not sure if that automatically signifies improved performance. Java has plenty of data structures to choose from and looks like your developer is using different ones for a reason. There used to be a few cases in which existing data structures and sorting algorithms will not make sense for the kind of data that you have and I have used my own algorithms for performance reasons. That was a few years back. Unless your application uses a limited subset of Java like mobile sdk, standard Java offers a sufficient variety of Collections to choose from and chances are that you will be able to find one that meets your needs, without compromising on performance.

A: 

The key consideration here is whether List was an appropriate collection type to begin with. If it's common to want to do some operation to everyone with the same first name, then organize the data accordingly -- such as in a Multimap<String, Whatever>, where the key is the first name. (Multimap is from Google Collections.)

Kevin Bourrillion