Reputation: 32
i have HashMap<String,Double>hm1
and guava table Table<String, String, Double> employeeYearsOfService
fatima |0.97
AT&T |0.96
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}
fatima={Bill Smith=1.0, Stacy Lerner=2.0}
AT&T={Bill Smith=2.0, Stacy Lerner=1.4}
and i want this result by create a new table Table<String, String, Double> results = HashBasedTable.create()
contains a row of employeeYearsOfService
who have a same key with HashMap hm1
(this is my question)
this picture for mor understand
Table guava
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());
}
loop for new 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
}
}
thank you very much
Upvotes: 1
Views: 463
Reputation: 34470
One way would consist of removing the elements you don't want from the table:
employeeYearsOfService.rowKeySet()
.removeIf(key -> !hm1.containsKey(key));
Here I'm using the 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, whose predicate returns true
if the key is not present in the hm1
map.
Upvotes: 1