컬렉션(List, Set)의 모든 요소들에 순차적으로 접근할 때 사용
👉🏻 for-each 구문과 비슷한 역할을 수행(저장된 요소들을 읽어옴)
hasNext() : 읽어올 요소가 남았는지 확인하는 메소드next() : 다음 요소를 읽어오는 메소드List list = Arrays.asList("A", "B", "C", "D", "E");
//반복자 변수 선언 및 생성
Iterater iter = list.iterator(); //메소드 호출만으로 Iterator 객체 생성
while(iter.hasNext()) { //요소가 존재하는만큼 반복
System.out.println(iter.next()); //요소를 반환하고 다음 요소를 반환할 수 있도록 준비
}
👉🏻 같은 동작을 for, for-each로도 구현 가능
💡 println에 오버로딩된 toString때문에 객체, 요소가 문자열로 출력될 수 있음
비선형 구조로 된 데이터의 모음(집합) 👉🏻 데이터의 입력된 순서를 유지하지 않으며 인덱스가 없음
💡 중복데이터를 허용하지 않음
➡ hashCode(), equals()를 이용해 요소의 중복검사 수행
class HashSet, class LinkedHashSet, class TreeSet
가장 기본이 되는 Set 구현체
데이터의 입력순서를 유지하는 Set 👉🏻 인덱스는 없음
정렬기능이 포함된 Set 👉🏻 오름차순 정렬

매핑되어있는 데이터를 요소로 가짐 ➡ Entry : 매핑된 쌍
key value 두 데이터를 한 쌍(Entry)으로 묶어서 데이터를 관리Key : Entry의 기준값으로, 데이터들을 식별할 때 사용 👉🏻 중복값으로 사용할 수 없음 Value : Entry의 데이터로, key에 매핑된(대응하는) 값 👉🏻 중복값 사용 가능hashCode(), equals()로 판단 👉🏻 Set과 동일Set에 따로 저장 후 Iterator로 출력class HashMap, class Hashtable, class Properties두 집합을 구성하는 원소들 간의, 데이터와 데이터 사이의 대응 관계
가장 기본이 되는 Map 객체
null이 허용 ➡ 그래도 쓰지 않는 것이 좋음HashMap + 동기화 처리
null을 허용하지 않음Map map = new HashMap();
Map map = new Hashtable();
👉🏻 key와 value로 null값응 허용하느냐의 차이
map.put(key, value); / 데이터 삽입
map.get(key); / 데이터 출력
➡ 존재하지 않는 key를 이용하여 get()하면 null반환
❗ 중복키를 사용할 수 없음 ➡ 중복입력된 key에 매핑된 기존 데이터를 덮어씌움
map.containsKey(key); / 입력된 key를 이미 갖고있는지
➡ if(!map.comtainsKey(2)) { map.put(2, "Cherry"); }
➡ key를 확인하고 같은 key가 없으면 데이터 삽입
map.containsValue(value); / 입력된 value가 이미 존재하는지
map.remove(key); / 입력된 key 확인 후 Entry 제거
map.remove(key, value); / 입력된 Entry확인 후 일치하면 제거
map.values() / values 출력 ➡ 컬렉션 형태로 [3.345, Apple, Apple, Durian, Orange]
map.size(); / map.isEmpty(); /
<모든 요소 출력하기>
Set keys = map.keyset();
Iterator iter = keys.iterator();
while(iter.hasNext()) {
System.out.println(iter.next() + " = " + map.get(key));
}
or
for(Object key : map.keyset()) {
System.out.println(key + " = " + map.get(key));
}
💡 key와 value의 데이터타입이 String으로 제한됨 ➡ 문자열 타입의 Entry 관리
Hashtable의 자식 클래스 👉🏻 상속받은 기능 + 추가 특성
Entry(key-vlaue)를 저장하고 불러오는 기능이 추가되어있음 ➡ 파일의 확장자 .properties.store(), .load()Properties sys = System.getProperties();
sys.getProperty("java.version");
sys.getProperty("os.name");

Properties prop = new Properties();
prop.setProperty(String, String); / prop.getProperty(String, String);
---------------------------------------------------------------------------------------------
<파일 출력>
//파일 출력스트림 변수
FileWriter writer = null; 👉🏻 System.out(모니터 출력)과 같은 역할
//파일 출력스트림 생성 ➡ 입력된 주소에 파일이 없으면 파일을 생성
writer = new FileWriter("//파일 주소//") 👉🏻 이 문구만 작성시 예외 에러 발생 ➡ try-catch로 감싸주기
//Properties 객체의 데이터를 파일로 출력(파일로 저장)
prop.store(writer, "comment : User Information");
👇🏻
Properties propt = new Properties();
propt.setProperty("want", "vacation");
propt.setProperty("hope", "Holiday");
try {
FileWriter fw = new FileWriter("D:\\eclipse-workspace\\StudyAtHome\\src\\java10_collection");
propt.store(fw, "comment : study again");
} catch (IOException e1) {
e1.printStackTrace();
}
---------------------------------------------------------------------------------------------
<파일 입력>
//파일 입력 스트림 객체
FileReader read = null;
//입력받은 데이터를 확인할 Properties 객체
Properties again = new Properties();
//입력객체에 데이터 저장
read = new FileReader("D:\\eclipse-workspace\\StudyAtHome\\src\\java10_collection"); 👉🏻 예외 발생 / 파일 못찾은 경우 ➡ try-catch로 감싸주기
👇🏻
FileReader read = null;
Properties again = new Properties();
try {
read = new FileReader("D:\\eclipse-workspace\\StudyAtHome\\src\\java10_collection");
again.load(read); 👉🏻 불러온 파일 확인 / 예외발생 ➡ try-catch로 감싸주기
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//파일 확인
System.out.println(again);
자료형들의 개별적인 특징, 특성을 하나의 공통적인 특성으로 바꿔서 표현
| int | ===일반화===> | 데이터타입 Type |
|---|---|---|
| double | T | |
| boolean | T | |
| String | T | |
| Point | T | |
| Person | T | |
| List | T | |
| ... | T |
클래스, 메소드 정의코드에서 사용되는 데이터타입들을 일반화시켜 프로그래밍
👉🏻 자바에서는 제네릭(Generic) 문법을 적용해서 구현
👀 Example
- 일반화가 필요한 상황 👉🏻 타입만 다른 같은 메소드
데이터 타입부분만 달라지며 같은이름, 같은 동작을 하는 중복코드가 작성되는 상황public void out(int data) { System.out.println("데이터 : " + data); } public void out(double data) { System.out.println("데이터 : " + data); } public void out(Point data) { System.out.println("데이터 : " + data); } public void out(Person data) { System.out.println("데이터 : " + data); } -------------------------------------------------------------------------- public <T> void out(!!T!! data) { System.out.println("데이터 : " + data); } ➡ 어떤 데이터를 대입해도 JVM이 오버로딩해서 자동으로 채워줌 ex) 123 -> int -> Integer✔ 개별적인 데이터타입으로 작성해야하는 정의코드들을 하나의 일반화된 데이터타입 T를 이용해 한번만 작성하고 활용할 수 있도록 👉🏻 제네릭 메소드
특정 자료형으로 결정되기 전 상태의 일반화된 데이터타입
raw type이라고 함raw type으로 결정되면 형변환이 항상 필요 ➡ Object는 최상위 부모타입이므로 더 많은 기능을 가진 자식클래스타입이 될 수 없음ArrayList<String> list; //타입 파라미터를 String으로 결정하면서 객체변수 선언 👉🏻 int형은 값 저장 불가
list = new ArrayList<String>();
list.add("Apple");
list.add("Banana");
ArrayList list2; //타입 파라미터를 결정하지 않아 Object 타입으로 자동결정
//객체 선언 및 생성 동시에
ArrayList<String> list = new ArrayList<>();
ArrayList<Integer> list = new ArrayList<>();
Iterator<Integer> iter = new Iterator<>();
제네릭 타입을 적용해 정의한 클래스
class Class02 <T> {
//T 타입 멤버필드
private T data;
//T 타입 멤버메소드
//반환타입, 매개변수타입, 지역변수타입이 모두 T
public T display(T data) {
this.data = data;
return data;
}
//T 타입 게터세터
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
👉🏻 클래스를 정의할 때 제네릭타입을 적용, 클래스 내부 코드에서 T를 자료형처럼 사용할 수 있음
------------------------------------------------------
public class GenericClass {
public static void main(String[] args) {
Class02 c01 = new Class02(); 👉🏻 타입파라미터를 결정하지 않아 Object 타입으로 자동 결정 / raw type
c01.setData(1234); // 1234 ➡ int ➡ Integer ➡ Object
int num = (int)c01.getData(); //raw type이라 형변환 필수!
//제네릭 클래스 객체변수 선언 및 생성시 타입 파라미터 결정, 사용
Class02<String> c02 = new Class02<>();
c02.setData("Grape");
String fruit = c02.getData();
}
메소드 내부에서 사용할 수 있는 제네릭 타입(타입 파라미터)을 적용한 메소드
class Class03 {
//일반메소드 + <T> 👉🏻 제네릭 메소드
public <T> void disply(int num) {
int data;
}
//제네릭 메소드 👉🏻 return타입, 매개변수타입, 지역변수타입
public <T> T out(T obj) {
T data = obj;
return data;
}
}
---------------------------------------------------
//일반 클래스 생성
Class03 cl = new Class03();
cl.out(1234);
👉🏻 타입파라미터를 결정하지 않고 호출 ➡ 매개변수의 전달인자를 보고 Integer타입으로 자동 결정
cl.<Double>out(3.14); 👉🏻 타입파라미터를 Double로 결정 후 호출