본문 바로가기
Java

Java - 람다 스트림 예제

by sinabeuro 2023. 2. 10.
728x90

 

람다 중복 체크 예시 코드

list.stream()
    .collect(Collectors.groupingBy(HQStoreOrderUploadDto::getGdsCd, Collectors.toList()))
    .entrySet()
    .stream()
    .filter(e -> e.getValue().size() > 1)
    .map(Entry::getValue)
    .flatMap(Collection::stream)
    .forEach(e -> e.setErrCont("gdsCd 중복"));

 

MapKey 를 활용한 컬랙션 그룹핑

List<CenterStockMoveExcelDto> datas = (List<CenterStockMoveExcelDto>) list;

datas.stream()
        .collect(
                Collectors.groupingBy(
                        e -> new MapKey(e.getStkoutCntrCd(), e.getStkinCntrCd(), e.getMovTobeYmd(), e.getPoYmd()),
                        Collectors.toList()
                )
        )
        .entrySet()
        .stream()
        .sorted(
                origin.thenComparing(destination)
                        .thenComparing(moveYmd)
                        .thenComparing(poYmd)
        )
        .forEach(entry -> {
            makeMoveDoc();
        });

 

MapKey 클래스 정의

@Getter
@EqualsAndHashCode
@AllArgsConstructor
class MapKey {
    String originStrCd;
    String destinationStrCd;
    String moveYmd;
    String poYmd;
    
    private final Comparator<Map.Entry<MapKey, List<CenterStockMoveExcelDto>>> origin =
    Comparator.comparing(e -> e.getKey().getOriginStrCd(), Comparator.naturalOrder());

private final Comparator<Map.Entry<MapKey, List<CenterStockMoveExcelDto>>> destination =
    Comparator.comparing(e -> e.getKey().getOriginStrCd(), Comparator.naturalOrder());

private final Comparator<Map.Entry<MapKey, List<CenterStockMoveExcelDto>>> moveYmd =
    Comparator.comparing(e -> e.getKey().getOriginStrCd(), Comparator.naturalOrder());

private final Comparator<Map.Entry<MapKey, List<CenterStockMoveExcelDto>>> poYmd =
    Comparator.comparing(e -> e.getKey().getOriginStrCd(), Comparator.naturalOrder());
}

 

 


 

@Getter
@EqualsAndHashCode
@AllArgsConstructor
@ToString
public class CntrPoSlipNoMapKeyDto {

    // 센터코드
    private String cntrCd;
    // 협력사
    private String suplrCd;
    // 발주일자
    private String poYmd;
    // 입고예정일자
    private String stkinTobeYmd;
    // 입고구분코드
    private String stkinSpCd;
    //센터발주구분
    private String cntrPoSpCd;

    public static final Comparator<Entry<CntrPoSlipNoMapKeyDto, List<CntrPoCommonDto>>> orderByCntrCd =
            Comparator.comparing(e -> e.getKey().getCntrCd(), Comparator.naturalOrder());

    public static final Comparator<Entry<CntrPoSlipNoMapKeyDto, List<CntrPoCommonDto>>> orderBySuplrCd =
            Comparator.comparing(e -> e.getKey().getSuplrCd(), Comparator.naturalOrder());

    public static final Comparator<Entry<CntrPoSlipNoMapKeyDto, List<CntrPoCommonDto>>> orderByPoYmd =
            Comparator.comparing(e -> e.getKey().getPoYmd(), Comparator.naturalOrder());

    public static final Comparator<Entry<CntrPoSlipNoMapKeyDto, List<CntrPoCommonDto>>> orderByStkinTobeYmd =
            Comparator.comparing(e -> e.getKey().getStkinTobeYmd(), Comparator.naturalOrder());

    public static final Comparator<Entry<CntrPoSlipNoMapKeyDto, List<CntrPoCommonDto>>> orderByStkinSpCd =
            Comparator.comparing(e -> e.getKey().getStkinSpCd(), Comparator.naturalOrder());

    public static final Comparator<Entry<CntrPoSlipNoMapKeyDto, List<CntrPoCommonDto>>> orderByCntrPoSpCd =
            Comparator.comparing(e -> e.getKey().getCntrPoSpCd(), Comparator.naturalOrder());
}

 

 

 

        List<CntrPoCommonDto> result = dataList.stream()
            .collect(
                    Collectors.groupingBy(e -> new CntrPoSlipNoMapKeyDto(
                            e.getCntrCd(), e.getSuplrCd(), e.getPoYmd(), e.getStkinTobeYmd(), e.getStkinSpCd(), e.getCntrPoSpCd())
                    )
            )
            .entrySet()
            .stream()
            .sorted(
                    CntrPoSlipNoMapKeyDto.orderByCntrCd
                            .thenComparing(CntrPoSlipNoMapKeyDto.orderBySuplrCd)
                            .thenComparing(CntrPoSlipNoMapKeyDto.orderByPoYmd)
                            .thenComparing(CntrPoSlipNoMapKeyDto.orderByStkinTobeYmd)
                            .thenComparing(CntrPoSlipNoMapKeyDto.orderByStkinSpCd)
                            .thenComparing(CntrPoSlipNoMapKeyDto.orderByCntrPoSpCd)
            ).map(e -> {
                e.getValue().get(0).setIsCallCntrPoSlipNo("Y");
                return e.getValue();
            })
            .flatMap(Collection::stream)
            .collect(Collectors.toList());

 

728x90

댓글