책/Head First Java
-
List 정렬에 대하여책/Head First Java 2023. 12. 21. 03:18
List를 정렬하는 여러가지 방법들을 정리해본다. Collections.sort 에서부터 stream.sorted까지 살펴본다. 글의 흐름은 Head First Java를 기초로 하고 있다. 정렬하는 방법들 1. Collections.sort(List) List인 경우만 정렬이 가능하다. /* 배열은 정렬 불가 */ String[] sArr = {"z", "b", "k"}; Collections.sort(sArr); // error /* List로 변경해서 정렬 */ String[] sArr = {"z", "b", "k"}; List sList = Arrays.asList(sArr); System.out.println("b4" + sList); // z,b,k Collections.sort(sList);..
-
HashSet! 무엇을 기준으로 같다고 할 것인가?책/Head First Java 2023. 12. 20. 01:09
HashSet은 2번의 검증을 한다. HashSet은 이름 처럼 hash로 구성된 set이라고 보면 된다. 기본적으로 hash function이 반환하는 키를 기반으로 중복을 검사한다. 하지만 hash function은 같은 값을 반환할 수도 있기 때문에 혹은 임의로 hash key가 수정된 경우 때문에, 키가 같은 경우에는 obj.equals를 이용해서 한번 더 같음을 검사한다. 따라서 HashSet을 이용해서 객체의 중복을 거르고 싶다면, hashCode 메소드와 equals 메소드를 override 해야한다. class Movie { String title; @Override public int hashCode() { return title.hashCode(); } @Override public b..
-
원시타입과 래퍼클래스책/Head First Java 2023. 12. 20. 01:03
Integer vs int 이 둘은 무슨차이? 자바 1.5에서 auto-boxing과 auto-unboxing을 제공하여, 타입 측면에서는 차이가 없이 사용할 수 있다. (컴파일 타임에 확인 가능) int int는 Primitives 타입으로 4바이트만 차지한다. 메모리와 연산 속도에 이점이 있다. Integer 타입에 할당되는 경우, 컴파일 타임에 자동으로 Integer.valueOf()를 통해 auto-boxing 된다. Integer Integer는 Reference 타입이기 때문에 보다 많은 메모리를 차지한다. (기본 int 값 + 각종 메소드) 힙에 할당된다. null 사용 가능하다. int 타입에 할당되는 경우, 컴파일 타임에 자동으로 Integer.initValue()를 통해 auto-unb..