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
반응형