lang/java

Java - 2 ArrayList join LinkedHapMap Filter to CSV String

C/H 2022. 2. 23. 23:55

입력값: CSV

package com.company;

import java.util.*;
import java.util.stream.Collectors;

public class Main {

    public static void main(String[] args) {
        long begin = System.nanoTime();
        String key = "ROOT,Z00002,M333333333333,K/ROOT2,K/G00001,K/00003,Z00002";
        String keyNm = "루트,제트공공공공이,엠삼,K루트2,지공일,공삼,젯공이";
//        key = "K/ROOT2";
//        keyNm = "K루트2";
//        key = "";
//        keyNm = "";
//        key = null;
//        keyNm = null;
        ArrayList<String> keyList = new ArrayList<>(Arrays.asList((key==null?"":key).split(",")));
        ArrayList<String> keyNmList = new ArrayList<>(Arrays.asList((keyNm==null?"":keyNm).split(",")));

        LinkedHashMap<String, String> keyMap = new LinkedHashMap<>();
        for(int i = 0; i < keyList.size(); i++){
            keyMap.put(keyList.get(i), keyNmList.get(i));
        }
        System.out.println(keyMap);


        Boolean existNRoot =  keyMap.entrySet().stream().anyMatch(item->"ROOT".equals(item.getKey()));
        Boolean existKRoot = keyMap.entrySet().stream().anyMatch(item->"K/ROOT2".equals(item.getKey()));
        System.out.println("### existNRoot: " + existNRoot);
        System.out.println("### existKRoot: " + existKRoot);

        String keyString = null;
        String keyNmString = null;
        if(existNRoot && existKRoot){
            keyString = "ROOT,K/ROOT2";
            keyNmString = keyMap.get("ROOT") + "," + keyMap.get("K/ROOT2");
        }
        else if(existKRoot && keyMap.size() == 1){
            keyString = "ROOT2";
            keyNmString = keyMap.get("K/ROOT2");
        }
        else if(existNRoot){
            keyString = keyMap.entrySet().stream()
                    .filter(item->item.getKey().startsWith("K/")||"ROOT".equals(item.getKey()))
                    .map(item->item.getKey())
                    .collect(Collectors.joining(","));
            keyNmString = keyMap.entrySet().stream()
                    .filter(item->item.getKey().startsWith("K/")||"ROOT".equals(item.getKey()))
                    .map(item->item.getValue())
                    .collect(Collectors.joining(","));
        }
        else if(existKRoot){
            keyString = keyMap.entrySet().stream()
                    .filter(item->!item.getKey().startsWith("K/")||"K/ROOT2".equals(item.getKey()))
                    .map(item->item.getKey())
                    .collect(Collectors.joining(","));
            keyNmString = keyMap.entrySet().stream()
                    .filter(item->!item.getKey().startsWith("K/")||"K/ROOT2".equals(item.getKey()))
                    .map(item->item.getValue())
                    .collect(Collectors.joining(","));
        }
        else if(keyMap.size() > 0){
            keyString = keyMap.entrySet().stream()
                    .map(item->item.getKey())
                    .collect(Collectors.joining(","));
            keyNmString = keyMap.entrySet().stream()
                    .map(item->item.getValue())
                    .collect(Collectors.joining(","));
        }
        System.out.println("keyString: " + keyString);
        System.out.println("keyNmString: " + keyNmString);

        System.out.println("EllipsesTime: " + (System.nanoTime() - begin) + " ns, " + ((System.nanoTime() - begin) / 1000000) + " ms");
    }
}
{ROOT=루트, Z00002=젯공이, M333333333333=엠삼, K/ROOT2=K루트2, K/G00001=지공일, K/00003=공삼}
### existNRoot: true
### existKRoot: true
keyString: ROOT,K/ROOT2
keyNmString: 루트,K루트2
EllipsesTime: 47655805 ns, 47 ms
반응형

'lang > java' 카테고리의 다른 글

Java Optional  (0) 2023.06.07
Java Date Offset  (0) 2023.06.07
java filter speed Test  (0) 2021.12.31
온라인에서 자바를 배울수 있는 사이트 10가지  (0) 2014.10.06
Spring  (0) 2013.07.17