📌 FunctionalInterface
- 인터페이스 추상 메소드 강제
- 인테페이스 내부 메소드를 한 개만 정의할 수 있도록 강제하는 역할
- 형식
@FunctionalInterface
public interface Calc {
public double exec(double num1, double num2);
}
📌 lambda function
- 람다식 메소드
- 메소드 한 개 인테페이스 구현 시 implement 하지 않고 main 메소드에서 구현 가능
- 형식
public class MainClass05 {
int a1 = 10;
int a2 = 20;
Calc plus = (a1, a2) -> {
return a1+a2;
};
Calc minus = (a1, a2) -> a1-a2;
double result = plus.exec(a1, a2);
double result2 = minus.exec(a1, a2);
}
📌 동적 타입 받기 (Generic Class)
- Class 사용 시, 동적 타입 받는 방법
클래스명 뒤에 <> 를 키워드 사용
해당 클래스로 객체 생성 시, 데이터 타입을 전달할 수 있다.
- 형식
public class FruitBox<T> {
private T item;
public void setItem(T item) {
this.item = item;
}
public T getItem() {
return item;
}
}
public class MainClass01 {
FruitBox<Apple> apple = new FruitBox<Apple>();
FruitBox<Integer> integer = new FruitBox<Integer>();
}
📌 ArrayList
- 동적 Array
- 동적으로 타입을 전달해 해당 타입에 맞는 데이터를 저장하는 ArrayList 객체 생성
참고 사항) ArrayList 객체를 List 인터페이스 타입의 변수로 받을 수 있다.
- 메소드 종류
- add(String) : Array 마지막 인덱스에 타입에 맞는 데이터를 추가
- get(index) : Array의 index에 매칭되는 데이터를 가져옴
- remove(index) : Array의 index에 매칭되는 데이터를 삭제
- set(index, String) : Array의 index에 지정 타입의 새로운 데이터를 추가
- size() : Array의 크기를 반환
- clear() : Array의 모든 데이터를 제거
- 형식
List<String> str = new ArrayList<String>();
str.add("a1");
str.add("a2");
str.add("a3");
String item = str.get(0);
str.remove(0);
str.set(0, "a0");
int size = str.size();
str.clear();
📌 확장 for문
- 확장 for문
Array의 데이터를 하나씩 불러오기 위한 for문
- 형식
for(String name:names) {
System.out.println(name);
}
📌 HashMap 제너릭 클래스
- HashMap
- 제너릭 타입으로 두개의 타입을 전달한다.
- key, value 쌍으로 타입을 지정한다.
참고 사항) HashMap 객체를 Map 인터페이스 타입의 변수로 받을 수 있다.
- 사용 메소드
- put(key, value) : key와 value를 저장
- get(key) : key와 매칭되는 value를 가져옴
- containsKey(입력값) : 입력값과 같은 key가 있을 경우 true를 return함
- containsValue(입력값) : 입력값과 같은 Value가 있을 경우 true를 return함
- remove(key) : key와 매칭되는 value까지 모두 삭제
- keySet() : 전체 key들을 Array로 불러옴
- values() : 전체 value들을 Array로 불러옴
- 형식
Map<String, String> dict = new HashMap<String, String>();
dict.put("데이터", "data");
dict.put("과학", "science");
dict.put("house", "집");
dict.put("phone", "전화기");
dict.put("car", "자동차");
System.out.println(dict.keySet());
System.out.println(dict.values());
Scanner sc = new Scanner(System.in);
System.out.print("검색할 단어를 입력하세요 : ");
String inputKey = sc.nextLine();
if(!dict.containsKey(inputKey)) {
System.out.println(inputKey+"은 목록에 없습니다.");
} else {
System.out.println(inputKey+"의 뜻은 "+dict.get(inputKey)+"입니다.");
}
📌 HashMap 케스팅
- casting
- HashMap 클래스 타입에 제너릭 타입으로 Object 사용 시, casting 필요
- get() 메소드를 사용할 때 반환된 결과 앞에 데이터 타입을 명시해야 한다.
- 형식
Map<String, Object> map = new HashMap<String, Object>();
map.put("num", 1);
map.put("name", "a1");
map.put("addr", "강남");
int num = (int) map.get("num");
String name = (String) map.get("name");
String addr = (String) map.get("addr");
📌 HashSet
- HashSet
- 순서가 없다.
- key 값이 없다.
- 중복을 허용하지 않는다.
- 어떤 데이터를 집합으로 관리하고자 할 때 사용한다.
참고 사항) HashSet 객체를 Set 인터페이스 타입의 변수로 받을 수 있다.
- 형식
Set<Integer> set1 = new HashSet<Integer>();
set1.add(10);
set1.add(10);
System.out.println(set1);