KakaoTalk_20220615_114125602.jpg

구현 클래스

1)HashMap

객체 생성

package com.java.datastruct02.map;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;

public class Map01 {

	public static void main(String[] args) {
		//Map(key,value)
		
		//1.객체 생성
		Map map1 = new HashMap();
		Map map2 = new TreeMap();
		Map map3=new LinkedHashMap();
		
		// 2.제넥릭스를 이용한 객체 생성
		//   key     value
		Map<String, Integer> map4 = new HashMap<>();
		Map<String, Integer> map5 = new TreeMap<>();
		Map<String, Integer> map6 = new LinkedHashMap<>();
	}

}

데이터 입력,수정,삭제

package com.java.datastruct02.map;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class Map02 {

	public static void main(String[] args) {
		// Map(key,value)
		// 1.객체 생성
		Map<String, Integer> map = new HashMap<>();

		// 2.데이터 입력
		map.put("a", 1);
		map.put("b", 2);
		map.put("c", 3);
		map.put("d", 4);
		// map.put("a", 5); key값은 중복 안됨

		System.out.println(map.toString()); // {a=1, b=2, c=3, d=4, e=5}

		// 3.데이터 수정:
		map.put("b", 22);
		System.out.println(map.toString());

		// 4.삭제 d값을 삭제하고 결과값을 리턴 받으시오.
		// Integer in = map.remove("d");
		int in = map.remove("d");
		System.out.println(in);

		boolean bl = map.remove("c", 3);
		if (bl)
			System.out.println("데이터를 삭제 했습니다.");
		else
			System.out.println("데이터를 삭제되지 않았습니다..");

		map.put("c", 3);
		map.put("d", 4);

		System.out.println(map.toString());

		// 5.데이터 포함 여부를 확인하고 그 값이 포함 되어 있는지 확인하는 로직을 구현하시오
		// 1)key 포함 여부 "a" 포함 여부
		boolean contkey = map.containsKey("a");
		if (contkey)
			System.out.println("키가 포함되었습니다.");
		else
			System.out.println("키가 포함 되어 있지 않습니다.");
		// 2)value 포함 여부 3

		boolean containValue = map.containsValue(3);
		if (containValue)
			System.out.println("값이 포함 되었습니다.");
		else
			System.out.println("값이  포함 되어 있지 않습니다.");

		// 6.데이터 조회 및 출력
		// 6.1 전체 key값 조회
		Set<String> keyset = map.keySet(); // ["a","b","c","d"] 집합,순서 없다.
		for (String ks : keyset)
			System.out.print(ks + " ");

		// 6.2 전체 value값 조회
		Collection<Integer> values = map.values();
		for (Integer in2 : values)
			System.out.println(in + " ");

		System.out.println();
		// 배열의 형태 변환 출력
		Object[] obj = map.values().toArray();
		
		//값 출력 1
		for (int i = 0; i < obj.length; i++) {
			// int in3 = (int) obj[i];
			Integer in3 = (Integer) obj[i];
			System.out.println(in);
		}

		//값 출력 2 - 축약
		Integer[] arr = map.values().toArray(new Integer[map.values().size()]);
		for (Integer num : arr)
			System.out.print(num + " ");

		System.out.println();

		//값 출력 3
		System.out.println(Arrays.toString(arr));

		System.out.println();

		// 값 출력 4 -forEach: 순회하면서 각 요소에 대한 특정 작업을 수행하라는 의미
		// Collection,Map,Stream
		// JS에도 비슷한 메서드가 있다.
		// entry객체에서 key,value값을 가져와서 매개변수에 집어 넣는다.
		// ->:람다식
		map.forEach((key, value) -> System.out.print(key + ":" + value + " "));

		// 6.3 전체 entrySet(key+value)를 이용해 key와 value값을 출력하시오.
		Set<Entry<String, Integer>> entrySet = map.entrySet();
		for(Entry<String, Integer> et : entrySet)
			System.out.println(et.getKey() + ":"+et.getValue());
		
		//축약
		for(Entry<String, Integer> et : map.entrySet())
			//System.out.println(et.getKey() + ":"+et.getValue());
			System.out.print(et+",");
		System.out.println();
		System.out.println("----------------");
		
		//iterator 객체를 이용해서 추력
		  Iterator<Entry<String,Integer>> et2 =  map.entrySet().iterator();
		while(et2.hasNext()) {  
			Entry<String, Integer> it = et2.next();
			System.out.println(it.getKey()+":"+it.getValue());
			System.out.println( et2.next().getKey()+et2.next().getValue());
		}

	}

}

Map에 객체 삽입