List Comparator
Comparator | 1.가격 오름차순이라는 정렬 기준을 작성하는 용도의 클래스 2.Comparator 인터페이스를 상속 받으면 오버라이딩 해야 하는 메소드가 강제화 된다. 3.sort()에서 내부적으로 사용되는 메소드이다. 4.비교 대상 두 인스턴스 가격이 오름차순 정렬이 되기 위해서는 앞의 가격이 더 작은 가격이여야 하고 만약 뒤의 가격이 더 작은 경우 두 인스턴스의 순서를 바꿔야 한다. 그 때 두 값을 바꾸라는 신호로 양수를 보내주게 되면 정렬 시 순서를 바꾸는 조건이 사용된다 |
장점 | 제네릭 작성 시 Object가 아닌 구체화 된 타입으로 매개변수가 선언 된 다운캐스팅 할 필요가 없어진다. |
제네릭 사용하여 bookDTO를 넣어준다.
public class AscendingPrice implements Comparator<bookDTO>{
int result = 0;
if(o1.getPrice() > o2.getPrice()) {
/*오름차순을 위해 순서를 바꿔야 하는 경우 양수 반환*/
}else if (o1.getPrice() < o2.getPrice()) {
/* 이미 오름차순으로 정렬 되어 있는 경우 음수 반환*/
result = -1;
}else {
/* 두 값이 같은 경우 0을 반환*/
}
return result;
}
ArrayList
ArrayList-JDK 1.2부터 제공 |
특징 1. | 인스턴스 생성 시 내부적으로 10칸짜리 배열을 생성해서 관리한다. 리스트는 리스트 페이지의 부모타입 다형성 적용 다형성을 적용하여 상위 레퍼런스로 ArrayLIst 객체를 참조할 수 있다. |
장점 | 1.내부적으로는 배열을 이용해서 요소를 관리하며, 인덱스를 이용해 배열 요소에 빠르게 접근 2. 배열을 보완하기 위해 (배열의 단점 1. 크기 변경 불가 2. 요소의 추가/수정/삭제/정렬이 복잡) 단, 자동으로 수행되는 것이지 속도가 빨라진다는 의미는 아니다. |
10칸짜리 배열을 생성해서 관리
ArrayList alist = new ArrayList();
List 인터페이스 하위의 다양한 구현체들로 타입 변경이 가능하기 때문에 타입은 List로 해두는 것이 더 유연한 코드를 작성하는 것이다.
List list = new ArrayList();
더 상위 타입인 Collection 타입을 이용할 수도 있다.
Collection clist = new ArrayList();
특징 2. | Object 클래스 하위 타입 인스턴스를 모두 저장할 수 있다. int나 double은 Intefer,Double로 오토 박싱 된다 어래이에 투스트링이 오버라이딩 되어 있다는 것을 알 수 있음 |
어래이에 투스트링이 오버라이딩 되어 있다는 것을 알 수 있음, 저장 순번이 유지 되고 있다.
alist.add("apple");
alist.add(123);
alist.add(45.67);
alist.add(new Date());
System.out.println("alist : " + alist);
alist : [apple, 123, 45.67, Fri Jan 06 17:39:53 KST 2023]
특징3. | 저장하고 있는 요소가 몇개인지 알려줌 add 몇개인지 size 메소드는 배열의 크기가 아닌 요소의 갯수를 반환한다. 내부적으로 관리되는 배열의 사이즈는 외부에서 알 필요가 없으므로 기능을 제공하지 않는다. |
System.out.println("alist size : " + alist.size());
alist의 size : 4
특징 4. | 내부 배열에 인덱스가 지정 되어 있기 때문에 for문으로 접근 가능 인덱스 하나하나를 가져올 때 |
for(int i=0; i<alist.size(); i++) {
System.out.println(i+ ":"+alist.get(i));
}
0 : apple
1 : 123
2 : 45.67
3 : Fri Jan 06 17:39:53 KST 2023
특징 5. | 데이터 중복 저장을 허용한다. 배열과 같이 인덱스로 요소를 관리하기 때문에 인덱스가 다른 위치에 동일한 값을 저장하는 것이 가능하다. |
alist.add("apple");
System.out.println("alist : "+alist);
alist : [apple, 123, 45.67, Fri Jan 06 17:39:53 KST 2023, apple]
특징6. | 원하는 인덱스 위치에 값을 추가할 수도 있다. 값을 중간에 추가하는 경우 인덱스 위치에 덮어쓰는 것이 아니라 새로운 값이 들어가는 인덱스 위치에 값을 넣고 이후 인덱스는 하나씩 뒤로 밀리게 된다 |
alist.add(1, "banana");
System.out.println("alist : " +alist);
alist : [apple, banana, 123, 45.67, Fri Jan 06 17:39:53 KST 2023, apple]
특징 7. | 지정 된 값을 삭제할 때는 remove 메소드를 사용한다. 중간 인덱스의 값을 삭제하는 경우 자동으로 인덱스를 하나씩 앞으로 당긴다 |
alist.remove(2);
System.out.println("alist : "+alist);
alist : [apple, banana, 45.67, Fri Jan 06 17:39:53 KST 2023, apple]
alist : [apple, true, 45.67, Fri Jan 06 17:39:53 KST 2023, apple]
지정 된 위치의 값을 수정할 때에도 인덱스를 활용할 수 있으면 set() 메소드를 사용한다. 참조변수값. 제너릭 타입을 지정하면 지정한 타입 외의 인스턴스는 저장하지 못한다 stringList.add(123); |
alist.set(1,true);
System.out.println("alist : "+alist);
/*모든 컬렉션 프레임워크 클래스는 제네릭 클래스로 작성되어 있다.*/
List<String> stringList = new ArrayList<>();
stringList.add("banana");
stringList.add("orage");
stringList.add("mango");
stringList.add("grape");
System.out.println("stringList : " +stringList);
stringList : [banana, orange, mango, grape]
stringList : [banana, grape, mango, orange]
저장 순서를 유지하고 있는 stringList를 오름차순 정렬한다.sort 메소드를 사용하면 list가 정렬 된 뒤 해당 상태가 유지된다 |
Collections.sort(stringList);
System.out.println("stringList : " +stringList);
banana
grape
mango
orange
orange
/* ArrayList에는 역순으로 정렬 기능이 제공 되지 않고 LinkedList에는 정의되어 있다 * 현재의 ArrayLIst를 LinkedList로 변경해 본다 */ //stringList를 재활용햇다 라는 의미 //상위 타입으로 선언하면 뭘 하든 상관없음 |
//stringList = new LinkedList<>(stringList);
//new LinkedList<>(stringList);
Iterator | Collection 인터페이스의 interator() 메소드를 이용해서 생성하는 인스턴스로 컬렉션에서 값을 읽어오는 통일된 방식을 제공하기 위해 사용한다. 반복자라고 불리우며 반복문을 이용해서 목록을 하나씩 꺼내는 방식으로 사용하기 위함이다. 인덱스로 관리 되는 컬렉션이 아닌 경우 반복문을 사용해서 요소에 하나씩 접근할 수 없고 인덱스 사용하지 않고 반복문을 사용하기 위한 목록을 만들어주는 역할을 한다. |
Iterator<String>iter = stringList.iterator();
while(iter.hasNext()) { //다음 요소를 가지고 있는 경우 true 더이상 요소가 없는 경우 false
System.out.println(iter.next()); //다음 요소를 반환
}
Iterator<String>dIter = ((LinkedList<String>)stringList).descendingIterator();
while(dIter.hasNext()) { //다음 요소를 가지고 있는 경우 true 더이상 요소가 없는 경우 false
System.out.println(dIter.next()); //다음 요소를 반환
}
//한번 꺼내면 반환 안됨
while(dIter.hasNext()) {
System.out.println(dIter.next());
mango
grape
banana
여러 권의 책 목록을 관리할 ArrayList 인스턴스 생성 |
List<BookDTO> bookList = new ArrayList<>();
bookList.add(new BookDTO(1, "홍길동전", "허균", 50000));
bookList.add(new BookDTO(2, "목민심서", "정약용", 40000));
bookList.add(new BookDTO(3, "동의보감", "허준", 30000));
bookList.add(new BookDTO(4, "삼국사기", "김부식", 20000));
bookList.add(new BookDTO(5, "삼국유사", "일연", 35000));
추가 된 도서 정보 출력 1. for문과 2. 향상된 for문
for(int i = 0; i < bookList.size(); i++) {
System.out.println(bookList.get(i));
}
for(BookDTO book : bookList) {
System.out.println(book);
}
BookDTO [number=1, title=홍길동전, author=허균, price=50000]
BookDTO [number=2, title=목민심서, author=정약용, price=40000]
BookDTO [number=3, title=동의보감, author=허준, price=30000]
BookDTO [number=4, title=삼국사기, author=김부식, price=20000]
BookDTO [number=5, title=삼국유사, author=일연, price=35000]
BookDTO [number=1, title=홍길동전, author=허균, price=50000]
BookDTO [number=2, title=목민심서, author=정약용, price=40000]
BookDTO [number=3, title=동의보감, author=허준, price=30000]
BookDTO [number=4, title=삼국사기, author=김부식, price=20000]
BookDTO [number=5, title=삼국유사, author=일연, price=35000]
/* 정렬 기준이 존재하지 않아 정렬할 수 없는 상태이다. */ //Collections.sort(bookList); |
가격 순으로 오름차순 정렬 - AscendingPrice 클래스 추가
Collections.sort(bookList, new AscendingPrice());
System.out.println("가격 오름차순 정렬 --------------------");
for(BookDTO book : bookList) {
System.out.println(book);
}
가격 오름차순 정렬 --------------------
BookDTO [number=4, title=삼국사기, author=김부식, price=20000]
BookDTO [number=3, title=동의보감, author=허준, price=30000]
BookDTO [number=5, title=삼국유사, author=일연, price=35000]
BookDTO [number=2, title=목민심서, author=정약용, price=40000]
BookDTO [number=1, title=홍길동전, author=허균, price=50000]
인터페이스를 구현할 클래스를 재사용하는 경우 AscendingPrice 클래스처럼 작성하면 되지만
* 한 번만 사용하고 더 이상 재사용할 일이 없을 경우에는 조금 더 간편한 방법을 이용할 수 있다. * 익명 클래스(Anonymous)를 이용한 방법이다. 익명클래스는 뒤에 {}를 만들어서 * Comparator 인터페이스를 상속 받은 클래스인데 이름이 없다고 생각하고 사용하는 것이다. * */ |
가격 내림차순 기준 작성- 가격 오름차순과는 반대로 뒤에 있는 값인 o2의 price가 더 클 경우 양수를 반환하여 순서를 바꾸게 한다. */ |
Collections.sort(bookList, new Comparator<BookDTO> () {
@Override
public int compare(BookDTO o1, BookDTO o2) {
return o2.getPrice() - o1.getPrice();
}
});
System.out.println("가격 내림차순 정렬 --------------------");
for(BookDTO book : bookList) {
System.out.println(book);
}
가격 내림차순 정렬 --------------------
BookDTO [number=1, title=홍길동전, author=허균, price=50000]
BookDTO [number=2, title=목민심서, author=정약용, price=40000]
BookDTO [number=5, title=삼국유사, author=일연, price=35000]
BookDTO [number=3, title=동의보감, author=허준, price=30000]
BookDTO [number=4, title=삼국사기, author=김부식, price=20000]
제목 오름차순 정렬
문자열은 대소비교를 할 수 없다. 문자 배열로 변경 후 인덱스 하나 하나를 비교해서 어느 것이 더 큰 값인지 확인해야 하는데 String 클래스의 compareTo() 메소드에서 이미 정의해 놓았다. */ |
Collections.sort(bookList, new Comparator<BookDTO> () {
@Override
public int compare(BookDTO o1, BookDTO o2) {
return o1.getTitle().compareTo(o2.getTitle());
}
});
System.out.println("제목 오름차순 정렬 --------------------");
for(BookDTO book : bookList) {
System.out.println(book);
}
제목 오름차순 정렬 --------------------
BookDTO [number=3, title=동의보감, author=허준, price=30000]
BookDTO [number=2, title=목민심서, author=정약용, price=40000]
BookDTO [number=4, title=삼국사기, author=김부식, price=20000]
BookDTO [number=5, title=삼국유사, author=일연, price=35000]
BookDTO [number=1, title=홍길동전, author=허균, price=50000]
제목 내림차순 정렬
Collections.sort(bookList, new Comparator<BookDTO> () {
@Override
public int compare(BookDTO o1, BookDTO o2) {
return o2.getTitle().compareTo(o1.getTitle());
}
});
System.out.println("제목 내림차순 정렬 --------------------");
for(BookDTO book : bookList) {
System.out.println(book);
}
제목 내림차순 정렬 --------------------
BookDTO [number=1, title=홍길동전, author=허균, price=50000]
BookDTO [number=5, title=삼국유사, author=일연, price=35000]
BookDTO [number=4, title=삼국사기, author=김부식, price=20000]
BookDTO [number=2, title=목민심서, author=정약용, price=40000]
BookDTO [number=3, title=동의보감, author=허준, price=30000]
BookDTO implements Comparable<BookDTO> 를 작성하고나면 sort 메소드 호출을 할 수 있다. 내부적인 정렬 기준이 정의 되었기 때문이다. */ Vector 클래스의 경우 스레드 동기화 처리가 된다는 점만 다르고 ArrayList와 동일하게 동작한다. ArrayList의 구 버전이므로 따로 코드는 작성해보지 않는다. JDK 1.0부터 사용하긴 했지만 하위 호환을 위해 남겨 놓았을 뿐 성능 문제로 현재는 잘 사용하지 않으며 가급적이면 ArrayList를 사용하면 된다. |
Collections.sort(bookList);
System.out.println("번호 오름차순 정렬 --------------------");
for(BookDTO book : bookList) {
System.out.println(book);
}
번호 오름차순 정렬 --------------------
BookDTO [number=1, title=홍길동전, author=허균, price=50000]
BookDTO [number=2, title=목민심서, author=정약용, price=40000]
BookDTO [number=3, title=동의보감, author=허준, price=30000]
BookDTO [number=4, title=삼국사기, author=김부식, price=20000]
BookDTO [number=5, title=삼국유사, author=일연, price=35000]
'java' 카테고리의 다른 글
java Collection12-Set,Linked,Map (0) | 2023.01.09 |
---|---|
java Collection12 -list3,4 (0) | 2023.01.06 |
java Generic11 (0) | 2023.01.06 |
java API 10-4 Wrapper,Calendar (0) | 2023.01.06 |
java 변수타입 기본형, 참조형2 (0) | 2023.01.06 |