• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

generic list

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic