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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "generic list" Watch "generic list" New topic
Author

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
fordCarList.add(car);
-----------------

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



Thanks,
Uday
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
Sheriff

Joined: Oct 13, 2005
Posts: 38087
    
  22
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
Ford=Mustang,Focus,Taurus,etc..
Toyota=bla,bla
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?


Cheers!
RSR
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:
http://www.javaworld.com/javaworld/jw-10-2004/jw-1018-filter.html


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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: generic list
 
Similar Threads
Problem with Treemaps
Using Sun's Migration Tool...
URL encoding and arguments
how to find which attributes have changed before using merge() in EclipseLink?
For varsity...