Split a alphabetically sorted list into sublists based on alphabets in java

前端 未结 4 1422
时光取名叫无心
时光取名叫无心 2021-01-19 06:20

I have a sorted list in java, i just want to split this list into sublists based on the first alphabet at each index of list. For example List contains

{
cal         


        
4条回答
  •  囚心锁ツ
    2021-01-19 06:47

    You could use Java 8 Streams. Unfortunately, this method doesn't take advantage from the list being sorted already. list has to be the list containing your elements.

    Map> collect =
            list.stream().collect(Collectors.groupingBy(elem -> elem.charAt(0)));
    

提交回复
热议问题