
(푸른색 interface, 회색 class // 실선 - 상속, 점선 - 구현 )

add(element)get(index), indetOf(찾고자 하는 내용) remove(index) , remove(element), clear()set(index, element) Product[] result = products.toArray(new Product[products.size()]);public static void main(String[] args) {
List<String> names = new ArrayList<>();
names.add("luna");
names.add("max");
names.add("luna"); // 중복 허용
// 리스트가 비어있는지 확인
System.out.println(names.isEmpty());
// 수정
names.set(0, "Sting");
// 조회
names.get(0);
names.indexOf("max");
// 삭제
names.remove(0);
names.clear();
}
add(element)isEmpty(), contains(찾고자 하는 내용) , size()remove(element), clear()toArray() - Object 형 배열로 변환 / iterator() - iterator 인터페이스 객체 반환 public class Person {
String name;
int age;
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
}
public class settest {
public static void main(String[] args) {
Set<Person> set = new HashSet<>();
set.add(new Person("luna", 3));
set.add(new Person("luna", 3));
System.out.println(set);
// equals는 동일하지만, new 사용으로 hashcode가 다르기 때문에 다른 값으로 된다.
}
}
이런 경우, 동일한 값을 동일하게 처리하게 위해서는 Person class 에서 중복 값을 확인하는 메소드를 재정의 해야 한다.
@Override
public int hashCode() {
// 이름이 동일하면 hashcode가 동일하게 나올 수 있도록
return name.hashCode();
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Person) {
Person p = (Person) obj;
return this.age == p.age;
}
return super.equals(obj);
}
put(Key, value) , putAll(Map<key, value> m - 기존 Collection 데이터 추가get(key) , containsKey(key) - boolean 형remove(key)Set<Map.Entry<key, value>> entrySet() , keySet() - 모두 반환 가능 public class MapTest {
public static void main(String[] args) {
//Map<키 자료형, 값 자료형>
Map<String, String> map = new HashMap<>();
map.put("luna", "cat");
map.put("max", "dog");
map.put("daisy", "cow");
map.put("toby", "cat");
map.put("luna", "dog"); // 중복의 key 값이 있으면, 그 뒤에 값이 수정된다.
// map 순회
// 1. keySet()
for(String key : map.keySet()) {
System.out.printf("%s : %s \n", key, map.get(key));
}
// 2. entrySet();
for(Map.Entry<String, String> entry : map.entrySet()) {
System.out.printf("%s : %s \n", entry.getKey(), entry.getValue());
}
}
}
offer(element) , add(element)peek()poll()isEmpty()public class QueueTest {
public static void main(String[] args) {
Queue<Integer> queue = new LinkedList<>();
queue.offer(1);
queue.offer(2);
queue.offer(3);
//while 문과 함께 사용 가능
while (!queue.isEmpty()) {
System.out.println(queue.poll());
}
}
}
push(element) peek()pop()isEmpty()public class StackTest {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);
stack.push(3);
// 값을 꺼내지 않고, 조회만 할 때
System.out.println(stack.peek()); //3
System.out.println(stack.peek()); //3
System.out.println(stack.peek()); //3
while (!stack.isEmpty()) {
System.out.println(stack.pop());
}
// 3, 2, 1 순서로 나옴
}
}
addFirst(element) , addLast(element) removeFirst(), removeLast()peekFirst(), peekLast()isEmpty()public class DequeTest {
public static void main(String[] args) {
Deque<Integer> deque = new ArrayDeque<>();
deque.addFirst(1);
deque.addFirst(2);
deque.addLast(3);
deque.addFirst(4);
System.out.println(deque);
// 4, 2, 1, 3
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class SortTest {
public static void main(String[] args) {
// 순서가 있는 자료 구조만 가능
List<String> names = new ArrayList<>();
names.add("daisy");
names.add("luna");
names.add("max");
names.add("alice");
System.out.println(names);
// [daisy, luna, max, alice]
Collections.sort(names);
System.out.println(names);
// [alice, daisy, luna, max]
}
}
public class SortTest {
public static void main(String[] args) {
// 순서가 있는 자료 구조만 가능
List<Person> persons = new ArrayList<>();
persons.add(new Person("daisy", 3));
persons.add(new Person("luna", 5));
persons.add(new Person("max", 6));
persons.add(new Person("alice", 2));
System.out.println(persons);
Collections.sort(persons);
System.out.println(persons);
}
}
이와 같이 사용자 정의 class의 경우 기본적으로 정렬이 불가능하다.

사용자 정의 class에 비교 비교 기준을 만드는 방법
public class Person implements Comparable<Person>{
String name;
int age;
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public int compareTo<Person o>{
return this.age - o.age;
// 양수 : 자리 바꿈
// 음수 : 자리 그대로
// 0 : 동일 위치
}
public class PersonComparator implements Comparator<Person>{
@Override
public int compare(Person o1, Person o2) {
return o1.age - o2.age;
}
}
이렇게 작성 후에는 Collections.sort(persons, new PersonComparator()); 으로 쓰면 된다.
Collections.sort(persons, new PersonComparator<String>(){
@Override
public int compare(Person o1, Person o2) {
return o1.age - o2.age;
}
});
Collections.sort(persons, (o1, o2) -> {
return Integer.compare(o1.length(), o2.length());
});