my dog learned polymorphism
The moose likes Beginning Java and the fly likes generic list Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "generic list" Watch "generic list" New topic

generic list

Udayan Kumar
Ranch Hand

Joined: Jan 16, 2007
Posts: 66
Hi All,

I have a generic java List something like this List<Car>.
The Car object looks like this
public class Car {

private String name;
private String model;
private int year;


The List collection could be huge in size. I need to create a sublist from this huge list based on a condition.
What i am currently doing is sifting through the huge list and populating the sublist List<Car> fordCarList
something like
if (car.getName().equals("MUSTANG") || car.getName().equals("FOCUS") || car.getName().equals("TAURUS")) // this will be long list again

What I am wondering is there a better way to acheiving the same.

Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Yes and no. What you're doing is the standard way to do it using just "core Java". If you're not opposed to using a third party library there are other ways (more robust?) ways of doing it. For example check out Collections2.filter() in the Google Collections Library. I think there's a similar method in one of the Apache collections libraries also.

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46362
How about a Comparator which sorts on the case-insensitive names. Use it to put the objects into a TreeSet, then search for them. Of course the Set will lose any duplicates. And you will have to search for "Escort" "Sierra" and "Granada" individually.
rakesh sugirtharaj
Ranch Hand

Joined: Dec 16, 2007
Posts: 151
One another problem i see with this is the following condition.
if (car.getName().equals("MUSTANG") || car.getName().equals("FOCUS") || car.getName().equals("TAURUS"))

What if ford adds a new car to its group? You will have to change the code again. But to make this extensible you can(with some more effort) do this
1. Create a properties file and load it in this class.
2. The properties file could have values as
3. When you want to create a new list of ford cars get the property for 'ford', parse the string(split or tokenize) to give you list of ford car names.
4. Then check if this list has car.getName() and on true add it to the List.
Advantage of this is that, next time you want to add a new ford car you can add it to the property file without touching the code.
Am i confusing?

Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
How about adding the manufacturer(like ford etc,) to the class 'Car' itself and then filtering the collection based on the manufacturer. For filtering the collection based on a criteria, you might want to look here:

Thanks and Regards
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
Originally posted by Satya Maheshwari:
How about adding the manufacturer(like ford etc,) to the class 'Car' ...

Seconded! That attribute definitely belongs in the Car class.
I agree. Here's the link:
subject: generic list
It's not a secret anymore!