-1

I have the following code, which makes use of the collector groupingBy()

Stream<String> stream = Stream.of("lions", "tigers", "bears", "toads", "tarantullas");

Map<Integer, ?> map7 = stream.collect(
    Collectors.groupingBy(
        String::length,
        Collectors.mapping(s -> s.charAt(0),
            Collectors.toList()
        )
    ));

System.out.println("map7: " + map7);
System.out.println("value type: " + map7.get(5).getClass());

Output:

map7: {5=[l, b, t], 6=[t], 11=[t]}
value type: java.util.ArrayList

How can I sort letters in each list, and what should be the appropriate type of map's value?

Alexander Ivanchenko
  • 25,667
  • 5
  • 22
  • 46
march_in
  • 19
  • 1

3 Answers3

4

The proper type of map according to the downstream collector of groupingBy should be Map<Integer,List<Character>>.

To sort each list of characters, you can make use of the collector collectingAndThen().

Map<Integer, List<Character>> charactersByLength = Stream.of("lions", "tigers", "bears", "toads", "tarantullas")
    .collect(Collectors.groupingBy(
        String::length,
        Collectors.collectingAndThen(
            Collectors.mapping(s -> s.charAt(0), Collectors.toList()),
            list -> { list.sort(null); return list; }
        )
    ));

Sidenote: try to use more meaningful names than map7. When your code base is larger than a couple of tiny classes, it helps a lot in debugging and maintaining the code.

Alexander Ivanchenko
  • 25,667
  • 5
  • 22
  • 46
1

You can use Collectors.toCollection(TreeSet::new) to get a sorted Set and accordingly you can replace ? with SortedSet<Character>.

Map<Integer, SortedSet<Character>> map7 = ohMy.collect(
                Collectors.groupingBy(
                        String::length,
                        Collectors.mapping(s -> s.charAt(0),
                                Collectors.toCollection(TreeSet::new))));

Output:

map7:{5=[b, l, t], 6=[t], 11=[t]}
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
1

You can use sorted() method on stream and then collect it by using groupingBy feature.

  1. filter(Objects::nonNull) : It will filter out the non-null values before performing the sorted operation. It will handle the NullPointerException as it will filter out the null records.
  2. .sorted(): It will sort the stream before the grouping of the stream.

The code as follows:

public class Test {
    public static void main(String[] args) {
        Stream<String> stream = Stream.of("lions", "tigers", "bears", "toads", "tarantullas");
        Map<Integer, List<Character>> sortedData =
                stream.filter(Objects::nonNull).sorted()
                        .collect(Collectors.groupingBy(String::length,
                                Collectors.mapping(y -> y.charAt(0),Collectors.toList())));
        System.out.println(sortedData);
    }
}

Output:

{5=[b, l, t], 6=[t], 11=[t]}
GD07
  • 1,117
  • 1
  • 7
  • 9