ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • JAVA/Stream
    Language/Java 2022. 11. 29. 15:25

     

    Map List 검색

    List<Map<String, String>> payCompleteList = new ArrayList<>();
    payCompleteList.add(new HashMap<String, String>() {{ put("RECEIPT_YM", "202207"); put("aaa", "aa1"); } });
    payCompleteList.add(new HashMap<String, String>() {{ put("RECEIPT_YM", "202210"); put("aaa", "aa2"); } });
    
    Set<String> receiptYearMonthSets = new HashSet<String>();
    payCompleteList.forEach(e -> receiptYearMonthSets.add(e.get("RECEIPT_YM")));
    
    System.out.println(receiptYearMonthSets.contains("202207"));
    System.out.println(receiptYearMonthSets.contains("202209"));

     

    검색 및 변경

    List<Map<String, String>> payCompleteList = new ArrayList<>();
    payCompleteList.add(new HashMap<String, String>() {{ put("RECEIPT_YM", "202207"); put("aaa", "aa1"); } });
    payCompleteList.add(new HashMap<String, String>() {{ put("RECEIPT_YM", "202210"); put("aaa", "aa2"); } });
    
    Set<String> receiptYearMonthSets = new HashSet<String>();
    payCompleteList.forEach(e -> receiptYearMonthSets.add(e.get("RECEIPT_YM")));
    
    List<Map<String, String>> monthList = new ArrayList<>();
    monthList.add(new HashMap<String, String>() {{ put("RECEIPT_YM", "202207"); put("RECEIPT_DATE", "20220705"); } });
    monthList.add(new HashMap<String, String>() {{ put("RECEIPT_YM", "202208"); put("RECEIPT_DATE", "20220805"); } });
    monthList.add(new HashMap<String, String>() {{ put("RECEIPT_YM", "202209"); put("RECEIPT_DATE", "20220905"); } });
    
    List<Map<String, String>> searchResult =
        monthList.stream()
            .filter(map -> map.entrySet().stream().anyMatch(e -> e.getValue().equals("20220705")))
            .collect(Collectors.toList());
    
    System.out.println(searchResult);
    
    if (receiptYearMonthSets.size() > 0) {
        for (Map<String, String> map : monthList) {
            if (receiptYearMonthSets.contains(map.get("RECEIPT_YM")) == true) {
                map.put("aaa", "aaaaa");
            }
        }
    }
    
    System.out.println(monthList);

     

    내림, 오름차순, 찾기

    List<Map<String, String>> list = new ArrayList<>();
    list.add(new HashMap<String, String>() {{ put("HOUSE_ID", "6417"); put("HOUSE_NAME", "다비 쉐어하우스");  } });
    list.add(new HashMap<String, String>() {{ put("HOUSE_ID", "7955"); put("HOUSE_NAME", "덕준 광명 지점-t1"); } });
    
    System.out.println(list);
    System.out.println("========");
    
    
    // 내림차순
    List<Map<String, String>> sortingDescResult = list.stream()
        .sorted((o1, o2) -> o1.get("HOUSE_ID").toString().compareTo(o2.get("HOUSE_ID").toString()) )
        .collect(Collectors.toList());
    
    // 오름차순
    List<Map<String, String>> sortingAscResult = list.stream()
        .sorted((o1, o2) -> o2.get("HOUSE_ID").toString().compareTo(o1.get("HOUSE_ID").toString()) )
        .collect(Collectors.toList());
    
    // 찾기
    Map<String, String> searchResult = list.stream()
        .filter(x -> x.get("HOUSE_ID").equals("7955")).findAny()
        .get();

     

    피벗

    List<Map<String, String>> list = new ArrayList<>();
    list.add(new HashMap<String, String>() {{ put("HOUSE_ID", "6417"); put("HOUSE_NAME", "다비 쉐어하우스");  } });
    list.add(new HashMap<String, String>() {{ put("HOUSE_ID", "7955"); put("HOUSE_NAME", "덕준 광명 지점-t1"); } });
    
    
    Map<String, String> pivot = list.stream()
        .collect(Collectors.toMap(
            i1 -> i1.get("HOUSE_ID"),
            i2 -> i2.get("HOUSE_NAME")
        ));
    
    System.out.println(pivot);

    댓글

Designed by Tistory.