Day 41 (23.02.22)

Jane·2023년 2월 22일
0

IT 수업 정리

목록 보기
49/124

1. hashCode(), equals()의 Overriding

1-1. String 요소가 들어간 HashSet

import java.util.*;

public class JavaTest extends Object {
	public static void main(String[] args) {
		HashSet<String> hSet = new HashSet<>();
		hSet.add(new String("7799"));
		hSet.add(new String("7799"));
		hSet.add(new String("7799"));

		System.out.println("인스턴스 수 : " + hSet.size());
	}
}

[Console]
인스턴스 수 : 1

  • String 클래스 안에 toString(), hashCode(), equals(Object anObject)가 존재하며, 이미 Overriding이 되어 있는 상태.
    (오버라이딩이 안 되어 있는 것 : Object 클래스의 것을 쓰고 있다.)



1-2. 로또 만드는 코드와 Integer 클래스

import java.util.*;

class JavaPractice {

	public static void main(String[] args) {
		HashSet<Integer> lotto = new HashSet<>();

		while (lotto.size() < 6) {
			int random = (int) ((Math.random() * 45) + 1);
			lotto.add(random);
		}

		System.out.print("번호 : " + lotto);

	}

}

[Console]
번호 : [17, 18, 35, 6, 27, 11]
(번호는 돌릴 때마다 다르게 나온다)

  • Integer 클래스에도 hashCode()와 equals(Object obj)가 Overriding 되어 있다.
@Override
public int hashCode() {
	return Integer.hashCode(value);
}
    
public static int hashCode(int value) {
	return value;
}
    
public boolean equals(Object obj) {
	if (obj instanceof Integer) {
		return value == ((Integer)obj).intValue();
	}
	return false;
}

2. MAP <K, V>

  • Key와 Value로 구성되어 있다.
  • C#, Python에서는 Dictionary(사전)의 개념

2-1. Map의 사용 예시

import java.util.*;

class JavaPractice {

	public static void main(String[] args) {
		HashMap<Integer, String> map = new HashMap<>();

		map.put(45, "Brown");
		map.put(37, "James");
		map.put(23, "Martin");
		
		System.out.println("Key 23 : " + map.get(23));
		System.out.println("Key 37 : " + map.get(37));
		System.out.println("Key 45 : " + map.get(45));
		
		map.remove(37);
		
		System.out.println("Key 37 : " + map.get(37));
	}

}

[Console]
Key 23 : Martin
Key 37 : James
Key 45 : Brown
Key 37 : null (해당하는 key의 value가 삭제되어 null로 표시)

2-2. Map의 특징

  • Map의 Key는 중복을 허용하지 않는다. 새로운 값이 들어오면 그것으로 바뀐다.
map.put(45, "Brown");
map.put(37, "James");
map.put(23, "Martin");
map.put(23, "Tom");
		
System.out.println("Key 23 : " + map.get(23));
System.out.println("Key 37 : " + map.get(37));
System.out.println("Key 45 : " + map.get(45));

[Console]
Key 23 : Tom
Key 37 : James
Key 45 : Brown

  • Map에서 중복을 허용하지 않는 이유 : KeySet으로 만들어져 있기 때문이다.
import java.util.*;

class JavaPractice {

	public static void main(String[] args) {

		HashMap<Integer, String> map = new HashMap<>();

		map.put(45, "Brown");
		map.put(37, "James");
		map.put(23, "Martin");

		Set<Integer> keyset = map.keySet();

		System.out.println("전체 key 출력");
		for (Integer n : keyset) {
			System.out.print(n.toString() + '\t');
		}
		System.out.println();

		System.out.println("전체 value 출력");
		for (Integer n : keyset) {
			System.out.print(map.get(n) + '\t');
		}
		System.out.println();

		System.out.println("반복자 이용 가능");
		for (Iterator<Integer> itr = keyset.iterator(); itr.hasNext();) {
			System.out.print(map.get(itr.next()) + '\t');
		}

	}

}

[Console]
전체 key 출력
37 23 45
전체 value 출력
James Martin Brown
반복자 이용 가능
James Martin Brown

2-3. TreeMap <K, V>

import java.util.*;

class JavaPractice {

	public static void main(String[] args) {

		Map<Integer, String> map = new TreeMap<>();

		map.put(45, "Brown");
		map.put(37, "James");
		map.put(23, "Martin");

		Set<Integer> keyset = map.keySet();

		System.out.println("전체 key 출력");
		for (Integer n : keyset) {
			System.out.print(n.toString() + '\t');
		}
		System.out.println();

		System.out.println("전체 value 출력");
		for (Integer n : keyset) {
			System.out.print(map.get(n) + '\t');
		}
		System.out.println();

		System.out.println("반복자 이용 가능");
		for (Iterator<Integer> itr = keyset.iterator(); itr.hasNext();) {
			System.out.print(map.get(itr.next()) + '\t');
		}

	}

}

[Console]
전체 key 출력
23 37 45
전체 value 출력
Martin James Brown
반복자 이용 가능
Martin James Brown

  • TreeMap은 HashMap과 다르게 순서가 있다. (순차적 접근이 가능하다.)
  • 많은 양의 요소를 걸러낼 때 편하다. (반으로 나눠서 관리)

3. Stack과 Queue

StackQueue
Last-In-First-OutFirst-In-First-Out
import java.util.*;

class JavaPractice {

	public static void main(String[] args) {
		Queue<String> que = new LinkedList<>();
		que.offer("Box");
		que.offer("Toy");
		que.offer("Robot");
		
		System.out.println("next : " + que.peek());
		
		System.out.println(que.poll());
		System.out.println(que.poll());
		
		System.out.println("next : " + que.peek());
		
		System.out.println(que.poll());
	}

}

[Console]
next : Box
Box
Toy
next : Robot
Robot

profile
velog, GitHub, Notion 등에 작업물을 정리하고 있습니다.

0개의 댓글