Given a List of items of different types, how do I efficiently separate those, so each group only contains item of the same type?
More details
In java, I have a List of about 10000 items. There are 10 different types of item. Type as in property of value. For example, List<Foo>, then type is Foo#getType().
I need to create zip-files that contain only one type of item.
Please give me some idea. Code are most-welcomed and appreciated, even pseudo-code. Thank you.
Accepted Code taking the idea from Jon Skeet
List<Foo> list = ...;
ImmutableListMultimap<String, Foo> grouped = MultiMaps.index(list,
new Function<Foo, String>() {
public String apply(Foo input) {
return input.getType();
}
});
for(String type : grouped.keySet()){
//The below list will contains items with the same type.
ImmutableList<Foo> fooListWithSameType = grouped.get(type);
String zipName = getZipName(type);
//Zip the list to a given file name
zipList(fooListWithSameType, zipName);
}
You could use Guava‘s
MultiMapfunctionality withMultiMaps.index, so using yourFoo.getType()example, if it returns a string: