2017-04-19 63 views
0

我有這個程序,我想收集ArrayList operatCible值和表employeeYearsOfService中的值,我想存儲在HashMap<String,integer>女巫包含RowKey和結果的值。加法表番石榴和arraylist

我不知道該怎麼做。這是我想要的東西

result

代碼

public class collaborativAlgorithme { 

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

    public static void main(String[] args) { 
    List<Double> operatCible = new ArrayList<Double>(); 
    operatCible.add(4.1); 
    operatCible.add(5.0); 
    System.out.println(operatCible); 

    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", 9.75); 
    System.out.println(employeeYearsOfService.rowKeySet()); 
    HashMap<String,Integer> result=new HashMap<String,Integer>; 
    System.out.println(employeeYearsOfService); 
    Map<String, Double> attEmployees = employeeYearsOfService.row("AT&T"); 
    for (Map.Entry<String, Double> employee : attEmployees.entrySet()) { 
     // what i do?? 
     // sum values arraylist and values table 
     System.out.println("Employee Name: " + employee.getKey() + ", Years 
    of Service: " + employee.getValue()); 
    } 
    } 
} 
+0

不是你想要做的事情 - 你有價值觀,總結它們?爲什麼'Integer'在地圖中,而不是'Double'? 「可操作性」及其價值是什麼? – Xaerxess

+0

@Xaerxess我只想總結這些數值,我在地圖上誤認了'Double',這筆交易是我想在'ArrayList'中使用'Table'中的數值得到總和值。 –

+0

因此,只需將你在循環中的數字.getValue()'與那些在列表中?你在Java 8上嗎? – Xaerxess

回答

0

我解決我的問題 我添加別的東西爲(開方).... 這是代碼

int k = 0; 
    Double sum = 0.0;//cosine similarity 
    Double sum2 = 0.0; 
    System.out.println(employeeYearsOfService); 
    for (String key : employeeYearsOfService.rowKeySet()) { 
     HashMap<String, Double> hm = new HashMap<String, Double>(); 
     for (Entry<String, Double> employee : 
     employeeYearsOfService.row(key).entrySet()) { 
      sum += employee.getValue() * operatCible.get(k); 
      sum2+=employee.getValue(); 
      k++; 
     } 
     k = 0; 

     System.out.println(sum); 
     System.out.println("----"); 
     System.out.println(sqrt(sum2)); 
     hm.put(key, sum); 
     sum = 0.0; 

     System.out.println(hm); 
    } 


} 
0

你可以說

  HashMap<String, Double> hm = 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); 

      k++; 
     } 
     k = 0; 

     System.out.println(sum); 


     hm.put(key, sum); 


    } 
+0

謝謝我解決了它 –