views:

99

answers:

4

I have a List of MyObjects ... MyObject{ int id,String name}. Now I want to split the the list into sublists that have identical "id" values, can any one suggest an efficient approach for doing this.

+2  A: 

Loop through the elements, check their id values, and place them in a Hashtable with id as the key. That's O(N), which is as efficient as you're going to get.

Ian Henry
And what about the value?
Kirk Woll
@Kirk - That depends on how the OP would be using it. With such a trivial object it might be convenient to have `name` be the value, however in many situations it would make more sense to have the value be the object itself: it's not a question you can answer without more context.
Ian Henry
+1  A: 
ArrayList<MyObject> list=new ArrayList<MyObject>();
//fill Objects..
HashMap<Integer,ArrayList<MyObject>> hash=new HashMap<Integer,ArrayList<MyObject>>();
for(MyObject elem:list)//iterate the list
{
ArrayList<MyObject> tmp=null; //temporary variable 
if((tmp=hash.get(elem.getId()))==null) // check if id already present in map
 {
  tmp=new ArrayList<MyObject>();   
  hash.put(elem.getId(),tmp); //if not put a new array list
 }
names.add(elem); //if present add the name to arraylist
}
Emil
+1, the value is a list.
Kirk Woll
names never becomes non-null in this solution in the case that you have to add a new ArrayList.
Mark E
@Mark:I did't test the code.Thanks for pointing out.
Emil
+4  A: 
// create the thing to store the sub lists
Map<Integer, List<MyObject>> subs = new HashMap<Integer, List<MyObject>>();

// iterate through your objects
for(MyObject o : list){

    // fetch the list for this object's id
    List<MyObject> temp = subs.get(o.getId());

    if(temp == null){
        // if the list is null we haven't seen an
        // object with this id before, so create 
        // a new list
        temp = new ArrayList<MyObject>();

        // and add it to the map
        subs.put(o.getId(), temp);
    }

    // whether we got the list from the map
    // or made a new one we need to add our
    // object.
    temp.add(o);
}
Mark E
thanks this solution looks elegant to me.
james
+1 - for properly indenting the code and adding comments
Stephen C
+1  A: 

Using Guava:

ListMultimap<Integer, MyObject> myObjectsById = Multimaps.index(myObjects,
    new Function<MyObject, Integer>() {
      public Integer apply(MyObject myObject) {
        return myObject.id;
      }
    });
Kevin Bourrillion