HashBasedTable Table<String,String,Double> HashMap<String,Double>

HashBasedTable Table<String,String,Double> HashMap<String,Double>

我有HashMap<String,Double>hm1和番石榴tableTable<String, String, Double> employeeYearsOfService

HashMap hm1

fatima |0.97
AT&T   |0.96

Table 员工服务年限

 Google={Bill Smith=1.75, Stacy Lerner=11.5}, 
 Microsoft={Bill Smith=13.2,Stacy Lerner=3.5}, 
 AT&T={Bill Smith=2.0, Stacy Lerner=1.4},
 fatima={Bill Smith=1.0, Stacy Lerner=2.0}

Table 结果

fatima={Bill Smith=1.0, Stacy Lerner=2.0}
AT&T={Bill Smith=2.0, Stacy Lerner=1.4}

我想通过创建一个新的 table Table<String, String, Double> results = HashBasedTable.create() 包含一行 employeeYearsOfService 来得到这个结果 谁拥有与 HashMap hm1 相同的密钥(这是我的问题)

这张图片供大家了解

我的代码

Table番石榴

    Table<String, String, Double> employeeYearsOfService = 
    HashBasedTable.create();

    employeeYearsOfService.put("AT&T", "Stacy Lerner", 1.4);
    employeeYearsOfService.put("Microsoft", "Stacy Lerner", 3.5);
    employeeYearsOfService.put("Microsoft", "Bill Smith", 13.2);
    employeeYearsOfService.put("Google", "Stacy Lerner", 11.5);

    employeeYearsOfService.put("AT&T", "Bill Smith", 2.0);
    employeeYearsOfService.put("Google", "Bill Smith", 1.75);
    employeeYearsOfService.put("fatima", "Bill Smith", 1.0);
    employeeYearsOfService.put("fatima", "Stacy Lerner", 2.0);

hashmap hm1

HashMap<String, Double> hm = new HashMap<String, Double>();
    HashMap<String, Double> hm1 = new HashMap<String, Double>();
    System.out.println(employeeYearsOfService);
    for (String key : employeeYearsOfService.rowKeySet()) {

        for (Entry<String, Double> employee : 
        employeeYearsOfService.row(key).entrySet()) {
            sum += employee.getValue() * operatCible.get(k);
            sum2 += employee.getValue() * employee.getValue();
            vect1 += operatCible.get(k) * operatCible.get(k);
            Result = (sum / (sqrt(sum2) * sqrt(vect1)));
            k++;

        }

        hm.put(key, Result);

        k = 0;
        sum = 0.0;
        sum2 = 0.0;
        vect1 = 0.0;
        Result = 0.0;

    }
    System.out.println(hm);
    Set<Entry<String, Double>> set = hm.entrySet();
    List<Entry<String, Double>> list = new ArrayList<Entry<String, Double>>
     (set);
    Collections.sort(list, new Comparator<Map.Entry<String, Double>>() {
        public int compare(Map.Entry<String, Double> o1,
                Map.Entry<String, Double> o2) {
            return o2.getValue().compareTo(o1.getValue());
        }
    });

    System.out.println(list);
    System.out.println("le K nn");
    for (Entry<String, Double> entry : list.subList(0, 2)) {
        hm1.put(entry.getKey(), entry.getValue());


    }

新循环 Table

Table<String, String, Double> results = HashBasedTable.create();
    System.out.println(hm1);
    for (Entry<String, Double> entry : list) {
       if(entry.getKey().equals(employeeYearsOfService.rowKeySet())){
           results.put(employeeYearsOfService.row(entry.getKey())));
           // how i do it
       }


    }

非常感谢

一种方法是从 table:

中删除不需要的元素
employeeYearsOfService.rowKeySet()
    .removeIf(key -> !hm1.containsKey(key));

这里我使用 Table.rowKeySet method to get the set of row keys from the table. This set is bounded to the original table, meaning that when an element is removed from this set, an entire row (with the same key) will be removed from the table. And this is what I'm doing with the Collection.removeIf method,如果键不在 hm1 映射中,其谓词 returns true