Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

generic list

 
Udayan Kumar
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48976
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic