If you are open to using a third-party library, Eclipse Collections 7.x will work with Java 6. MutableListYou can call the use of Eclipse collections groupByas follows:
MutableList<String> list =
Lists.mutable.with("calculator", "catch", "doll", "elephant");
Multimap<Character, String> multimap =
list.groupBy(StringFunctions.firstLetter());
System.out.println(multimap);
If you need to use java.util.Listfor strings, you can use a class ListAdapter.
List<String> list =
Arrays.asList("calculator", "catch", "doll", "elephant");
Multimap<Character, String> multimap =
ListAdapter.adapt(list).groupBy(StringFunctions.firstLetter());
Note. I am a committer for Eclipse collections.
source
share