Hamdan Sultan
Hamdan Sultan

Reputation: 226

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

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

{
calculator,
catch,
doll,
elephant
}

i want the sublists as

{calculator,catch}
{doll}
{elephant}

I dont want to put 26 if statements, is there any efficient and correct way of doing this. The list is already sorted alphabetically. Any help will be highly appreciated.

Upvotes: 4

Views: 2131

Answers (4)

SilverNak
SilverNak

Reputation: 3381

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<Character, List<String>> collect =
        list.stream().collect(Collectors.groupingBy(elem -> elem.charAt(0)));

Upvotes: 7

Donald Raab
Donald Raab

Reputation: 6686

If you are open to using a third-party library, Eclipse Collections 7.x will work with Java 6. Using Eclipse Collections MutableList you can call groupBy as follows:

MutableList<String> list = 
        Lists.mutable.with("calculator", "catch", "doll", "elephant");
Multimap<Character, String> multimap = 
        list.groupBy(StringFunctions.firstLetter());
System.out.println(multimap); 
// Prints {d=[doll], e=[elephant], c=[calculator, catch]}

If you need to use a java.util.List for the Strings, then you can use the ListAdapter class.

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.

Upvotes: 1

Youcef LAIDANI
Youcef LAIDANI

Reputation: 59978

The solution of @SilverNak is good with Java-8, you can use this also if you are not using Java-8 :

public static void main(String[] args) {
    String list[] = {"calculator", "catch", "doll", "elephant"};
    Map<Character, List<String>> map = new HashMap<>();

    List<String> lst;
    for (String str : list) {
        //if the key exit then add str to your list else add a new element
        if (map.containsKey(str.charAt(0))) {
            map.get(str.charAt(0)).add(str);
        } else {
            lst = new ArrayList<>();
            lst.add(str);
            map.put(str.charAt(0), lst);
        }
    }
}

Upvotes: 3

Wim De Troyer
Wim De Troyer

Reputation: 492

Something like this could work.

private List<List<String>> subListOnFirstLetter(List<String> list) {
    List<List<String>> result = new ArrayList<>();
    char first = list.get(0).charAt(0);
    List<String> subList = new ArrayList<>();
    for (String element : list) {
        char next = element.charAt(0);
        if (next != first) {
            result.add(subList);
            subList = new ArrayList<>();
        } else {
            subList.add(element);
        }
        first = next;
    }
    return result;
}

Upvotes: 0

Related Questions