Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you use PartitioningBy in streams to find a above and below average value?

I'm a beginner here trying to work out some stream syntax for one of my projects. My project is a Scrabble word value counter, where I have a method that uses HashMap to store the letters and their values, while I have a String array that stores the words that need to be processed. Using stream I was able to grab the top three and average value of the words. I am currently having issues trying to find:

  • those word above average
  • those words below average

How would I split the values to above and below average using the partitioningBy method? Keeping in mind that this has to be done via Streams.

Here is my code so far:

import java.util.Arrays;
import java.util.HashMap;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class scrabble {

    public static int scrapleVal(String word)
    {
    Map<Character, Integer> letterValues = new HashMap<>();

    letterValues.put('a', 1);
    letterValues.put('b', 3);
    letterValues.put('c', 3);
    letterValues.put('d', 2);
    letterValues.put('e', 1);
    letterValues.put('f', 4);
    letterValues.put('g', 2);
    letterValues.put('h', 4);
    letterValues.put('i', 1);
    letterValues.put('j', 8);
    letterValues.put('k', 5);
    letterValues.put('l', 1);
    letterValues.put('m', 3);
    letterValues.put('n', 1);
    letterValues.put('o', 1);
    letterValues.put('p', 3);
    letterValues.put('q', 10);
    letterValues.put('r', 1);
    letterValues.put('s', 1);
    letterValues.put('t', 1);
    letterValues.put('u', 1);
    letterValues.put('v', 8);
    letterValues.put('w', 4);
    letterValues.put('x', 8);
    letterValues.put('y', 4);
    letterValues.put('z', 10);

    return word.toLowerCase().chars().map(e->letterValues.get((char)e)).sum();
    }

    public static void main(String[] args) {



    String [] words = {"Java", "program", "list", "string", "unix",
    "hours", "syntax", "error"};
    
    //top three words
    System.out.println("Top three words are: ");
    Stream.of(words).sorted((e1,e2)->scrapleVal(e2)-scrapleVal(e1)).limit(3).forEach
    (e->System.out.println(e+" : "+scrapleVal(e)));
    //average word value
    System.out.println("\nAverage value fo words is: ");
    double averageScrapleValue = Stream.of(words)
    .mapToInt(scrabble::scrapleVal).average().orElse(0.0);
    System.out.println(averageScrapleValue);
    //above average words
    Stream.of(words).collect(Collectors
    .partitioningBy((scrabble::scrapleVal) > averageScrapleValue)).forEach
    (e->System.out.println(e+" : "+scrapleVal(e)));
    //below average words
    Stream.of(words).collect(Collectors
    .partitioningBy((scrabble::scrapleVal) < averageScrapleValue)).forEach
    (e->System.out.println(e+" : "+scrapleVal(e)));
    }
    }
like image 397
Jeff Avatar asked Dec 31 '25 03:12

Jeff


1 Answers

You get data in map after partitioningBy

Map<Boolean, List<String>> 
            map = Stream.of(words).collect(Collectors
                  .partitioningBy(w -> scrapleVal(w) < averageScrapleValue));

Here map.get(true) give bellow average and map.get(false) give equal and above average words.

List<String> bellowAvg = map.get(true);
List<String> aboveAndEqualAvg = map.get(false);

Output:

[list, string, hours, error]
[Java, program, unix, syntax]
like image 160
Eklavya Avatar answered Jan 01 '26 15:01

Eklavya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!