1. Collection의 개념을 이해하고 기술할 수 있다. (자바의 데이터 자료관리를 구현할 수 있다.)
list : 자료의 데이터를 인덱스로 관리한다. 중복 데이터 가능, null 가능
- ArrayList, Vector
set : 값으로 데이터를 관리한다. 중복 데이터 허용 불가능, null 하나만 가능
- HashSet
map : 키와 값으로 데이터가 저장되고 키로 데이터를 관리한다. 중복 데이터 가능, null 가능, 키 중복 불가능
- HashMap
하는 이유?
1. 원하는 타입을
2. 동적으로 관리할 수 있다.
============================================================================
1. List
package com.Collections;
import java.util.*;
import com.exam.Score;
public class Test {
public static void Prn01() {
List<Score> li = new ArrayList<Score>();
li.add(new Score("홍길동", 90, 70, 60));
li.add(new Score("홍길동1", 90, 70, 60));
li.add(new Score("홍길동2", 90, 70, 60));
li.add(new Score("홍길동3", 90, 70, 60));
list_print(li);
List<Score> rm = list_print02(li); // list_print02() 메서드를 통해서 값 변경 후 리턴 받는다.
list_print(rm);
}
public static void list_print(List<Score> listall) {
for (Score res : listall) {
System.out.println(res);
}
}
public static List<Score> list_print02(List<Score> all) {
// 국어 점수를 모두 100점으로 변경하자
for (Score s : all) {
s.setKor(100);
}
return all;
}
public static void list_print03(List<Score> all) {
System.out.println("============================");
Iterator<Score> ir = all.iterator(); // Iterator -> next() haNext()
while(ir.hasNext()) {
System.out.println(ir.next());
}
}
public static void main(String[] args) {
Prn01();
}
}
2. Set
package com.Collections;
import java.util.*;
import com.exam.*;
public class Test02 {
public static void Prn() {
Set<Score> si = new HashSet<Score>();
si.add(new Score("홍길동", 90, 70, 60));
si.add(new Score("홍길동1", 90, 70, 60));
si.add(new Score("홍길동2", 90, 70, 60));
Score score = new Score("홍길동3", 90, 70, 60);
si.add(score);
si.add(score);
si.add(score);
for (Score sm : si) {
System.out.println(sm);
}
}
public static void Prn02() {
Set<String> si02 = new HashSet<>();
si02.add("abc");
si02.add("abc");
si02.add(new String("abcd"));
si02.add(new String("abcd"));
for (String sm : si02) {
System.out.println(sm);
}
}
public static void main(String[] args) {
Prn();
Prn02();
}
}
3. Map
package com.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import com.exam.Score;
public class Test03 {
public static void Prn() {
Map<Integer, String> m1 = new HashMap<>();
m1.put(11, "1임");
m1.put(22, "2임");
m1.put(33, "3임");
System.out.println(m1);
System.out.println(m1.keySet());
System.out.println(m1.values());
m1.put(1, "1");
System.out.println(m1);
Set<Entry<Integer, String>> res = m1.entrySet();
System.out.println(res);
}
public static void Prn02() {
Map<Integer, Score> si = new HashMap<>();
si.put(1, new Score("홍길동", 90, 70, 60));
si.put(2, new Score("홍길동1", 90, 70, 60));
si.put(3, new Score("홍길동2", 90, 70, 60));
System.out.println(((Object)si).getClass());
System.out.println(si);
System.out.println(si.values());
System.out.println(si.keySet());
Set<Map.Entry<Integer, Score>> res = si.entrySet();
System.out.println();
System.out.println(res);
for(Map.Entry<Integer, Score> i : res ) {
System.out.println("Key : " + i.getKey() + ",\tValue : " + i.getValue());
}
}
public static void main(String[] args) {
Prn02();
}
}
'데이터과학자 - 강의 > java' 카테고리의 다른 글
210701 Java - GUI (0) | 2021.07.01 |
---|---|
210630 Java - File I/O (0) | 2021.06.30 |
210629 Java - 인터페이스, 추상클래스, 상속, 다형성 (0) | 2021.06.29 |
210626 Java - String, StringBuffer, StringBuilder (0) | 2021.06.28 |
210626 Java - 클래스, 생성자, 상속 (0) | 2021.06.26 |