I have a list of objects in this format :
class myObj {
private String identifier;
public myObj(String identifier){
this.identifier = identifier;
}
}
List<myObj> allobjects = new ArrayList<myObj>();
allobjects.add(new myObj("123"));
allobjects.add(new myObj("123"));
allobjects.add(new myObj("123"));
allobjects.add(new myObj("123"));
allobjects.add(new myObj("1234"));
allobjects.add(new myObj("12345"));
allobjects.add(new myObj("12"));
allobjects.add(new myObj("12"));
What is an elegant method of extracting the duplicate objects into seperate Lists ? So in above example a new List is returned containing two lists. The first lists contains :
new myObj("123");
new myObj("123");
new myObj("123");
new myObj("123");
The second list contains :
new myObj("12");
new myObj("12");
A possible solution is to create a new object :
List<List<myObj>> newList = new ArrayList<List<myObj>>
And then for each element in the list 'allobjects' iterate over each element and for each element that is contained more than once add it to the list. Then at the end of the iteration for the current element add the newly created list to 'newList'
Is this acceptable or is there another solution ?