Sorry, I'm too tired to figure it out now (too long no sleep), and project deadline is in 1 hour...
I have a lists i.e. like this: {aaa,aaa,bbb,ccc,aaa,bbb,ccc,bbb}. I need to prepare myList defined this way:
List<List<String>> myList = new ArrayList<List<String>>();
to present itself like separate lists according to number of duplicates: {aaa,bbb} {ccc}
here is my ugly code:
int maxAnagramsNumber = 0;
Set<String> unique = new HashSet<String>(anagramLineList);
for (String key : unique) { //set max number of anagrams
if (maxAnagramsNumber < Collections.frequency(anagramLineList, key)){
maxAnagramsNumber = Collections.frequency(anagramLineList, key);
}
}
int countedAnagrams = 0;
for (int i=maxAnagramsNumber; i > 1; i--){ // group and make new list items acc.to number of duplicates
for (String anagramUnit : unique){
if(countedAnagrams != i && countedAnagrams != 0){
anagramLineListSorted.add(anagramUnit);
}
countedAnagrams = i;
}
myList.add(anagramLineListSorted);
all anagrams will have the same set of characters. create a hashmap with the sorted list of chars as key and a list of the actual strings as values. Then finally combine all the values into a single list. try to think how this can be done. i can provide some code if you struggle
pass your original array of strings to this function
Code
if all you need is to divide the list
now result will have all your strings with anagrams grouped together.