2016-09-28 87 views
0

作爲標題,我想將結果集中的數據存儲到哈希映射中,然後將它們用於進一步處理(max,min,avg,分組)。JAVA-將結果集中的數據存儲到hashmap中並將它們聚合

到目前爲止,我通過使用正確的哈希映射並從頭開始實現每個操作(遍歷哈希映射(鍵,值)對)來實現此目的。

我的問題是:它是否存在執行此類操作的庫? 例如,計算List上的最大值的方法或給定兩個相同大小的數組,執行「index-to-index」差異的方法。

在此先感謝。

回答

0

Collections API已經有一些有用的功能。

例如maxmin

Collections.max(arrayList); 

請調查收集documentation,看看是否有你需要的功能。可能會有。

-1

對於max,min,avg,您可以使用Java 8及其流處理。

+0

你能不能讓我知道如何流處理將在這裏工作,至今爲止還沒有使用.. –

0

那麼這裏有Collection類。有一些有用的靜態方法,但你必須閱讀並選擇你需要的。下面是文檔:

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

這類專門由上進行操作或 返回集合的靜態方法。

例子:

 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)); 

輸出將是:

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

類似的問題,但是,在回答之前,例如這裏: how to get maximum value from the List/ArrayList

0

您可以使用Java 8爲此流。

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()); 
    } 



} 

請記住,只有當您的系統具有jdk 1.8時才能工作。對於較低版本的jdk流不支持。

0

在Java8有IntSummaryStatisticsLongSummaryStatisticsDoubleSummaryStatistics計算maxmincountaveragesum

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; 
    } 
} 
相關問題