加法表番石榴和arraylist

时间:2017-04-19 18:25:21

标签: java arraylist hashmap guava

我有这个程序,我想收集ArrayList operatCible中的值和表employeeYearsOfService中的值,我希望将结果保存在包含RowKey和结果的HashMap<String,integer>中。

我不知道怎么做。这就是我想要的

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

2 个答案:

答案 0 :(得分:0)

我解决了我的问题 我添加其他东西(sqrt)....    这是代码

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


}

答案 1 :(得分: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);


    }