본문
Map에 List 넣기(JAVA)
프로그래밍/Java 2016. 8. 21. 21:40
# Map With List
source) MapWithList.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | package com.source.map; package com.source.map; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class MapWithList { public static void main(String[] args) { List list_01 = new ArrayList<Object>(); List list_02 = new ArrayList<Object>(); Map<String, Object> map_01 = new HashMap<String, Object>(); Map<String, Object> map_02 = new HashMap<String, Object>(); Map<String, Object> map_03 = new HashMap<String, Object>(); // Map에 Data를 넣은 뒤 List에 담는 로직 (S) map_01.put("key_01", "value_01"); map_01.put("key_02", "value_02"); list_01.add(map_01); map_02.put("key_03", "value_03"); map_02.put("key_04", "value_04"); list_02.add(map_02); // Map에 Data를 넣은 뒤 List에 담는 로직 (E) // Map에 List를 담는 로직 (S) map_03.put("firstList", list_01); map_03.put("secondList", list_02); // Map에 List를 담는 로직 (E) showContents(map_03); } public static void showContents(Map<String, Object> mapReceiver) { List list_01 = new ArrayList<Object>(); List list_02 = new ArrayList<Object>(); Map<String, Object> map_01 = new HashMap<String, Object>(); Map<String, Object> map_02 = new HashMap<String, Object>(); list_01 = (List) mapReceiver.get("firstList"); list_02 = (List) mapReceiver.get("secondList"); map_01 = (Map<String, Object>) list_01.get(0); map_02 = (Map<String, Object>) list_02.get(0); System.out.println(map_01); System.out.println(map_02); System.out.println(map_01.get("key_01")); System.out.println(map_01.get("key_02")); System.out.println(map_02.get("key_03")); System.out.println(map_02.get("key_04")); } } | cs |
result)
1 2 3 4 5 6 7 | {key_02=value_02, key_01=value_01} {key_04=value_04, key_03=value_03} value_01 value_02 value_03 value_04 | cs |
댓글