본문 바로가기

java16

collections framework - HashSet HashSet - 순서 X, 중복 X, Set HashSet SortedSet TreeSet HashSet - Set 인터페이스를 구현한 대표적인 컬렉션 클래스 - 순서를 유지하려면, LinkedHashSet클래스를 사용하면 된다. - HashSet은 객체를 저장하기전에 기존에 같은 객체가 있는지 확인 후 같은 객체가 없으면 저장하고, 있으면, 저장하지 않는다. - boolean add(Object o)는 저장할 객체의 equals()와 hashCode()를 호출 - equals() 와 hashCode()가 TreeSet - 범위 검색과 정렬에 유리한 컬레션 클래스 - from ~ to - HashSet보다 데이터 추가, 삭제에 시간이 더 걸림 HashSet - 주요 매서드 생성자 매서드 HashSet(.. 2023. 9. 2.
collections framework - Comparator 와 Comparable 객체 정렬에 필요한 매서드(정렬기준 제공)를 정의한 인터페이스 Comparable 기본 정렬기준을 구현하는데 사용 Comparator 기본 정렬기준 외에 다른 기준으로 정렬하고자할때 사용 public interface Comparator{ int compare(Object o1, Object o2); boolean equals(Object obj); } public interface Comparable { int compareTo(Object o); 주어진 객체(o)를 자신(this)과 비교 } 연습문제 class Ex11_7 { public static void main(String[] args){ String[] strArr = {"cat", "Dog", "lion", "tiger"}; Arrays... 2023. 9. 2.
collection framework : Arrays Arrays - 배열을 다루기 편리한 매서드(static) 제공 (1) 1. 배열의 출력 toString(배열) 매개변수에 배열을 넣으면 배열 그대로 출력해 준다. {1, 2, 3, 4, 5} -> [1, 2, 3, 4, 5] int[] 배열 = new int[]{1,2,3,4,5}; System.out.println(Arrays.toString(배열)); static String toString(boolean[] a) static String toString(byte[] a) static String toString(char[] a) static String toString(short[] a) static String toString(int[] a) static String toString(long[].. 2023. 9. 1.
collection framework : Iterator, Enumeration, Map Iterator, Enumeration, Map (1) 컬렉션에 저장된 데이터를 접근하는데 사용되는 인터페이스 Enumeration은 Iterator의 구버전 Iterator, Enumeration은 next() 다음 요소를 확인하는 매서드가 있다. ListIterator는 Iterator의 접근성을 향상시킨 것(단방향 -> 양방향) ListIterator는 양방향이라 이전 객체를 확인하는 previous() 와 next() 매서드 둘 다 있다. Iterator 인터페이스의 매서드 매서드 설 명 boolean hasNext() 읽어 올 요소가 남아있는지 확인한다. 있으면 true, 없으면 false를 반환한다. Object next() 다음 요소를 읽어 온다. next()를 호출하기 전에 hasNext(.. 2023. 8. 31.