-1

If there is an array or list that contains Foo objects, and I want to split the collection based on the property Foo.bar, is there a quick way to do it in Java besides using a map and traversing the collection?

For example, say the list looks like:

  1. Foo.bar = 1
  2. Foo.bar = 1
  3. Foo.bar = 2
  4. Foo.bar = 3
  5. Foo.bar = 3
  6. Foo.bar = 4

The list will be split into 4 different lists because there are 4 distinct values of bar.

J. Lin
  • 2,259
  • 5
  • 20
  • 17
  • 2
    possible duplicate of [split a java collection into sub collections based on a object property](http://stackoverflow.com/questions/3836621/split-a-java-collection-into-sub-collections-based-on-a-object-property) – Mike B Jul 25 '14 at 18:47
  • http://stackoverflow.com/questions/2921003/split-java-arraylist-by-properties-of-contained-objects – Mike B Jul 25 '14 at 18:50
  • http://www.coderanch.com/t/491631/java/java/easy-split-Arraylist-multiple-lists – Mike B Jul 25 '14 at 18:50
  • Specifically asked for a method w/o using a map -_- – J. Lin Jul 25 '14 at 20:13

1 Answers1

0

Yes there is Treeset where you can add different values. but also you have override equals method.

public boolean equals(Object anObject){
   Foo f = (Foo) anObject;
   return f.bar==this.bar
}
Prashant Gurav
  • 505
  • 7
  • 17