1

I have List<List<MyObj>>.

List(1) : List<MyObj>
List(2) : List<MyObj>
List(3) : List<MyObj>

... & so on.

Structure of MyOBj is:

class MyObj {
  String name;
  String type;
}

I have to get name from MyObj, if type is same for any 2 entries present in List>. What will be the optimal way to do that in java 7?

madeeha
  • 156
  • 3
  • 11

1 Answers1

0
List<List<MyObj>> temp = new ArrayList();
sortByType(temp);
List<MyObj> result = new ArrayList();
        for (List<MyObj> obj : temp) {
            result.add(obj.get(0));
        }
        Collections.sort(result, new Comparator<MyObj>() {
            @Override
            public int compare(MyObj o1, MyObj o2) {
                return o1.getType() - o2.getType();
            }
        });
return result.get(0);

 private static void sortByType(List<List<MyObj>> result) {
        for(List<MyObj> objs : result) {
            Collections.sort(objs, new Comparator<MyObj>() {
                @Override
                public int compare(MyObj o1, MyObj o2) {
                    return o1.getType() - o2.getType();
                }
            });
        }
    }
madeeha
  • 156
  • 3
  • 11