How to partition a list by predicate using java8?

Elad Benda

I have a list a which i want to split to few small lists.

say all the items that contains with "aaa", all that contains with "bbb" and some more predicates.

How can I do so using java8?

I saw this post but it only splits to 2 lists.

public void partition_list_java8() {

    Predicate<String> startWithS = p -> p.toLowerCase().startsWith("s");

    Map<Boolean, List<String>> decisionsByS = playerDecisions.stream()
            .collect(Collectors.partitioningBy(startWithS));

    logger.info(decisionsByS);

    assertTrue(decisionsByS.get(Boolean.TRUE).size() == 3);
}

I saw this post, but it was very old, before java 8.

Pshemo

Like it was explained in @RealSkeptic comment Predicate can return only two results: true and false. This means you would be able to split your data only in two groups.
What you need is some kind of Function which will allow you to determine some common result for elements which should be grouped together. In your case such result could be first character in its lowercase (assuming that all strings are not empty - have at least one character).

Now with Collectors.groupingBy(function) you can group all elements in separate Lists and store them in Map where key will be common result used for grouping (like first character).

So your code can look like

Function<String, Character> firstChar =  s -> Character.toLowerCase(s.charAt(0));

List<String> a = Arrays.asList("foo", "Abc", "bar", "baz", "aBc");
Map<Character, List<String>> collect = a.stream()
        .collect(Collectors.groupingBy(firstChar));

System.out.println(collect);

Output:

{a=[Abc, aBc], b=[bar, baz], f=[foo]}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Partition a predicate by "OR" logic in Java

From Dev

How to effectively create list using if in java8?

From Dev

How to effectively create list using if in java8?

From Dev

How to convert List of Lists to single List using Java8 streams

From Dev

Filtering List using Java8 streams

From Dev

Java 8 partition list

From Dev

How to do filter a list in Java8?

From Dev

How to cut a sequence intro a list of lists according to a predicate using recursion

From Dev

how to set a weekday using java8

From Dev

CollectionUtils in java using predicate

From Dev

Using Predicate in Java for Comparison

From Dev

Using Java Predicate and Lambda

From Dev

how to pass a list in a predicate in prolog

From Java

Reduce elements in a java list using Java8 streams

From Dev

Partition by list using expressions

From Java

Comparing two Integer list using java8 filter

From Dev

Sum distances between points in list using Java8 Streams

From Dev

Count number of objects in a list using Java8 Stream

From Dev

List of specification combine using java8 stream

From Dev

List filter in Java8 using isPresent method

From Dev

Comparing two Integer list using java8 filter

From Dev

Using java8 Streams merge internal lists within a list

From Dev

Count number of objects in a list using Java8 Stream

From Dev

Check if DateString is valid from List of formats using Java8

From Dev

Implementing a filter with Predicate<T> in Java8

From Dev

Java8 lambda predicate exception

From Java

How to add elements of a Java8 stream into an existing List

From Dev

how to change items in a list of string in java8

From Dev

How do I sort a List of TreeSets with java8 streams

Related Related

HotTag

Archive