计算地图中重复键值的数量

时间:2018-10-30 17:24:14

标签: java groovy

我有一张自己绘制的地图。它以XML格式遍历巨大的WAPI答复,并创建此映射。对于每个名称,我要获取一个名称,然后是匹配的运营商对的总数,然后是该名称的最便宜价格。

因此,根据下面的结果将是:

 AACATDOM: [AA/AA: 3, UA/AA: 1] 390.99
 11PUB2: [ UA/AA: 1, AS/AS: 4] 486.99

 myMap = [[name:AACATDOM, cost:390.99, carrierpair:AA/AA], [name:AACATDOM, cost:390.99, carrierpair:AA/AA], [name:AACATDOM, cost:395.99, carrierpair:UA/AA], [name:AACATDOM, cost:393.99, carrierpair:AA/AA], [name:AACATDOM, cost:393.99, carrierpair:AA/AA], [name:11PUB2, cost:486.99, carrierpair:UA/AA], [name:11PUB2, cost:486.99, carrierpair:UA/AA], [name:11PUB2, cost:892.99, carrierpair:AS/AS], [name:11PUB2, cost:892.99, carrierpair:AS/AS], [name:11PUB2, cost:892.99, carrierpair:AS/AS], [name:11PUB2, cost:892.99, carrierpair:AS/AS]]

在这上面有些卡住了!预先感谢您的帮助!

4 个答案:

答案 0 :(得分:1)

不是使用Map,而是使用Bag集合,该集合通过调用getCount

来节省计数
  

定义一个集合,该集合计算对象在集合中出现的次数。

答案 1 :(得分:1)

这可以是使用Groovy的一种方法。

def list = [
    [name:'AACATDOM', cost:390.99, carrierpair:'AA/AA'],
    [name:'AACATDOM', cost:390.99, carrierpair:'AA/AA'],
    [name:'AACATDOM', cost:395.99, carrierpair:'UA/AA'],
    [name:'AACATDOM', cost:393.99, carrierpair:'AA/AA'],
    [name:'AACATDOM', cost:393.99, carrierpair:'AA/AA'],
    [name:'11PUB2', cost:486.99, carrierpair:'UA/AA'],
    [name:'11PUB2', cost:486.99, carrierpair:'UA/AA'],
    [name:'11PUB2', cost:892.99, carrierpair:'AS/AS'],
    [name:'11PUB2', cost:892.99, carrierpair:'AS/AS'],
    [name:'11PUB2', cost:892.99, carrierpair:'AS/AS'],
    [name:'11PUB2', cost:892.99, carrierpair:'AS/AS']
]

list.groupBy ({ it.name }, { it.carrierpair }).collectEntries { key, value ->
  [ key, value.collectEntries { k, v -> [ k, v.size() ] } ]
}.collectEntries { key, value ->
  [ key, value << [ minCost: list.findResults { it.name == key ? it.cost : null }.min() ] ]
}

// results:
// [AACATDOM:[AA/AA:4, UA/AA:1, minCost:390.99], 11PUB2:[UA/AA:2, AS/AS:4, minCost:486.99]]

答案 2 :(得分:0)

在Java中,您可以使用Collectors.groupingBy(...)构建诸如map之类的地图,该地图会生成类似

的数据结构
Map<String, List<Value>> map = listOfValues.stream()
                                           .collect(Collectors.groupingBy(v -> v.getName());

列表包含相同键的所有值。

答案 3 :(得分:0)

@Sulteric 假设您的数据可以描述为

public class Data  {
    private String name;
    private List<Map<String, Integer>> pairs;
    private Double cost; 
// getters and setters
    public int getPairsCount () {
        return pairs.size();
    }

然后您将处理结果分组为

public class ProcessedData {
    private int pairsCount;
    private Double cheapestCost;    

    public ProcessedData (int pairsCount,Double cheapestCost ) {
        this.pairsCount = pairsCount;
        this.cheapestCost = cheapestCost;
    }
// getters and setters

您可以执行以下操作:

    List<Data> data = new ArrayList<>();
    // Fill the data

    // Find the lowest cost for every name
    Map<String, Double> minByName = data.stream().collect(Collectors.groupingBy(Data::getName, Collectors.minBy(Data::getCost)));
    // Counts the pairs for every name and unites this data with min cost
    Map<String, ProcessedData> result = data.stream().collect(Collectors.groupingBy(Data::getName, new ProcessedData(Collectors.summingInt(Data::getPairsCount), minByName.get(Data::getName))));   
    // Proceed with result