0

as the title, I'd like to store data from result set to hash map and then use them for further processing (max, min, avg, grouping).

So far, I achieved this by using a proper hash map and implementing each operation from scratch - iterating over the hash map (key, value) pairs.

My question is: does it exist a library that performs such operations? For example, a method that computes the maximum value over a List or a method that, given two same-size arrays, performs a "index-to-index" difference.

Thanks in advance.

Fab
  • 1,145
  • 7
  • 20
  • 40

5 Answers5

1

Well there is the Collection class for instance. There is a bunch of useful static methods but you'll have to read and choose the one you need. Here is the documentation:

https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html

This class consists exclusively of static methods that operate on or return collections.

Example:

        List<Integer> list = new ArrayList<>();
        List<String> stringList = new ArrayList<>();

        // Populate the lists
        for(int i=0; i<=10; ++i){
            list.add(i);
            String newString = "String " + i;
            stringList.add(newString);
        }
        // add another negative value to the integer list
        list.add(-1939);

        // Print the min value from integer list and max value form the string list.

        System.out.println("Max value: " + Collections.min(list));
        System.out.println("Max value: " + Collections.max(stringList));

The output will be:

run:
Max value: -1939
Max value: String 9
BUILD SUCCESSFUL (total time: 0 seconds)

Similar question, however, was answered before for example here: how to get maximum value from the List/ArrayList

Community
  • 1
  • 1
pstoyanov
  • 61
  • 3
0

There are some usefull functions in Collections API already.

For example max or min

Collections.max(arrayList);

Please investigate collections documentation to see if there is a function that you need. Probably there woulde be.

Sergei Podlipaev
  • 1,331
  • 1
  • 14
  • 34
0

You can use java 8 streams for this.

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Testing {
    public static void main(String[] args) {

        //List of integers
    List<Integer> list = new ArrayList<>();
    list.add(7);
    list.add(5);
    list.add(4);
    list.add(6);
    list.add(9);
    list.add(11);
    list.add(12);

    //get sorted list using streams
    System.out.println(list.stream().sorted().collect(Collectors.toList()));

        //find min value in list
        System.out.println(list.stream().min(Integer::compareTo).get());

        //find max value in list
        System.out.println(list.stream().max(Integer::compareTo).get());

        //find average of list
        System.out.println(list.stream().mapToInt(val->val).average().getAsDouble());


        //Map of integers
        Map<Integer,Integer> map = new HashMap<>();

        map.put(1, 10);
        map.put(2, 12);
        map.put(3, 15);

        //find max value in map
        System.out.println(map.entrySet().stream().max((entry1,entry2) -> entry1.getValue() > entry2.getValue() ? 1: -1).get().getValue());

        //find key of max value in map
        System.out.println(map.entrySet().stream().max((entry1,entry2) -> entry1.getValue() > entry2.getValue() ? 1: -1).get().getKey());

        //find min value in map
        System.out.println(map.entrySet().stream().min((entry1,entry2) -> entry1.getValue() > entry2.getValue() ? 1: -1).get().getValue());

        //find key of max value in map
        System.out.println(map.entrySet().stream().min((entry1,entry2) -> entry1.getValue() > entry2.getValue() ? 1: -1).get().getKey());

        //find average of values in map
        System.out.println(map.entrySet().stream().map(Map.Entry::getValue).mapToInt(val ->val).average().getAsDouble());
    }



}

Keep in mind that it will only work if your system has jdk 1.8 .For lower version of jdk streams are not supported.

Innovation
  • 1,514
  • 17
  • 32
0

In Java8 there are IntSummaryStatistics, LongSummaryStatistics, DoubleSummaryStatistics to calculate max,min,count,average and sum

public static void main(String[] args) {
    List<Employee> resultSet = ...
    Map<String, DoubleSummaryStatistics> stats = resultSet.stream().collect(Collectors.groupingBy(Employee::getName, Collectors.summarizingDouble(Employee::getSalary)));
    stats.forEach((n, stat) -> System.out.println("Name " + n + " Average " + stat.getAverage() + " Max " + stat.getMax())); // min, sum, count can also be taken from stat
}

static class Employee {
    String name;
    Double salary;

    public String getName() {
        return name;
    }

    public Double getSalary() {
        return salary;
    }
}
Saravana
  • 12,647
  • 2
  • 39
  • 57
-1

For max, min, avg you can use Java 8 and it's stream processing.