주어진 데이터
public class ListMapMethod3 {
public static void main(String[] args) {
// 1. 문자열 정보대로 자르기
String planeData = "홍모씨,hong@abc.com,남,서울,재직|김모씨,kim@abc.com,남,전주,재직|최모씨,choi@abc.com,여,서울,재직|박모씨,park@abc.com,남,인천,휴직";
String[] datas = planeData.split("\\|");
String[] keys = {"name","email","gender","office","status"};
ListMapMethod3 l = new ListMapMethod3();
List<Map> list = l.getInfoList(datas, keys);
l.getInfoMap(list, keys);
}
// 2. 자른 문자열 Map에 담아 List에 넣기
public List<Map> getInfoList(String[] datas, String[] keys){
Map<String, String> infoMap = null;
List<Map> infoList = new ArrayList<>();
for(int i=0;i<datas.length;i++) {
String[] data = datas[i].split(",");
infoMap=new HashMap<>();
for(int j=0;j<keys.length;j++) {
infoMap.put(keys[j], data[j]);
}
infoList.add(infoMap);
}
System.out.println(infoList);
return infoList;
}
/*
출력값
[{gender=남, name=홍모씨, office=서울, email=hong@abc.com, status=재직}, {gender=남, name=김모씨, office=전주, email=kim@abc.com, status=재직}, {gender=여, name=최모씨, office=서울, email=choi@abc.com, status=재직}, {gender=남, name=박모씨, office=인천, email=park@abc.com, status=휴직}]
*/
// 3. 자른 문자열
public void getInfoMap(List<Map> infoList, String[] keys) {
for(int i=0;i<infoList.size();i++) {
Map<String, String> infoOut = infoList.get(i);
System.out.println("--------------------------");
for(int j=0;j<infoOut.size();j++) {
System.out.println(keys[j]+": "+infoOut.get(keys[j]));
}
System.out.println("--------------------------");
}
}
} // 클래스의 끝