iT邦幫忙

0

[JAVA]載入Excel 檔案用ArrayList與HashMap做二次排序問題

目前依次排序結果如下
第一次分類是用原子筆與鉛筆分類
想請問想要用原子筆與鉛筆value內的ArrayList再各分成男女生

要如何實現
謝謝前輩指導

    for(int i = 1;i <= childSheet.getLastRowNum(); i++ ){
        HSSFRow row = childSheet.getRow(i);
        Map rowMap=new HashMap();
        if(null != row){
            for(int k=0;k<=row.getLastCellNum();k++ ){
                HSSFCell cell;
                cell = row.getCell(k);
                if(cell != null) {
                	rowMap.put(heder.getCell(k).getStringCellValue(),cell.getStringCellValue());
                }
            }
        }
        list.add(rowMap);
    }
    for(int t = 0; t < list.size(); t++) {
    	Map row = (Map)list.get(t);
    	if(map.get(row.get("pen")) == null) {
    		
    		map.put(row.get("pen"), new ArrayList());
    	}
    	ArrayList temp = (ArrayList)map.get(row.get("pen"));
    	temp.add(row);
    }
    System.out.println(map);
    
    結果
    {原子筆=[{car_name=汽車, food_name=果汁, boy_girl=男生, pen=原子筆, book_name=老人與海, id_name=bron}, {car_name=汽車, food_name=糖果, boy_girl=男生, pen=原子筆, book_name=安迪生的物事, id_name=john}, {car_name=汽車, food_name=蛋糕, boy_girl=男生, pen=原子筆, book_name=老人與海, id_name=van}, {car_name=機車, food_name=蛋糕, boy_girl=女生, pen=原子筆, book_name=老人與海, id_name=vivi}, {car_name=汽車, food_name=糖果, boy_girl=男生, pen=原子筆, book_name=安迪生的物事, id_name=mark}, {car_name=汽車, food_name=糖果, boy_girl=男生, pen=原子筆, book_name=安迪生的物事, id_name=apple}, {car_name=汽車, food_name=糖果, boy_girl=男生, pen=原子筆, book_name=安迪生的物事, id_name=andy}], 鉛筆=[{car_name=機車, food_name=蛋糕, boy_girl=男生, pen=鉛筆, book_name=哈里波德, id_name=joe}, {car_name=機車, food_name=蛋糕, boy_girl=女生, pen=鉛筆, book_name=安迪生的物事, id_name=dark}, {car_name=機車, food_name=糖果, boy_girl=男生, pen=鉛筆, book_name=哈里波德, id_name=pig}, {car_name=機車, food_name=餅乾, boy_girl=女生, pen=鉛筆, book_name=哈里波德, id_name=monkey}]}

1 個回答

0
史帝夫
iT邦新手 5 級 ‧ 2019-07-31 16:42:56

先把 excel 的所有 row 加入 list,就可以用 groupingBy 就解決了

Map<String, Map<String, List<OO>>> map = list.stream().collect(Collectors.groupingBy(OO::getPen, Collectors.groupingBy(OO::getBoy_girl)));

結果:
map = {原子筆={男生=[OO{car_name=汽車, food_name=果汁, boy_girl=男生, pen=原子筆, book_name=老人與海, id_name=bron}, OO{car_name=汽車, food_name=糖果, boy_girl=男生, pen=原子筆, book_name=安迪生的物事, id_name=john}, OO{car_name=汽車, food_name=蛋糕, boy_girl=男生, pen=原子筆, book_name=老人與海, id_name=van}, OO{car_name=汽車, food_name=糖果, boy_girl=男生, pen=原子筆, book_name=安迪生的物事, id_name=mark}, OO{car_name=汽車, food_name=糖果, boy_girl=男生, pen=原子筆, book_name=安迪生的物事, id_name=apple}, OO{car_name=汽車, food_name=糖果, boy_girl=男生, pen=原子筆, book_name=安迪生的物事, id_name=andy}], 女生=[OO{car_name=機車, food_name=蛋糕, boy_girl=女生, pen=原子筆, book_name=老人與海, id_name=vivi}]}, 鉛筆={男生=[OO{car_name=機車, food_name=蛋糕, boy_girl=男生, pen=鉛筆, book_name=哈里波德, id_name=joe}, OO{car_name=機車, food_name=糖果, boy_girl=男生, pen=鉛筆, book_name=哈里波德, id_name=pig}], 女生=[OO{car_name=機車, food_name=蛋糕, boy_girl=女生, pen=鉛筆, book_name=安迪生的物事, id_name=dark}, OO{car_name=機車, food_name=餅乾, boy_girl=女生, pen=鉛筆, book_name=哈里波德, id_name=monkey}]}}

我要發表回答

立即登入回答