
키(Key) - 값(Value) 쌍 저장. 키 중복 X, 값 중복 O.HashMap, Hashtable, TreeMap.// 선언/기본 조작
Map<Object, Object> hmap = new HashMap<>();
hmap.put("one", new Date());
hmap.put(12, "red apple");
hmap.put(12, "yellow banana"); // 같은 키 → 값 덮어씀
hmap.remove(12);
Object v = hmap.get("one");
System.out.println(hmap.size());
Map<String, String> map = Map.of(
"one","java 8","two","oracle 11g","three","jdbc"
);
// 1) keySet + iterator / for-each
for (String k : map.keySet()) System.out.println(k + " = " + map.get(k));
// 2) values()
for (String v : map.values()) System.out.println(v);
// 3) entrySet() ← 가장 선호
for (Map.Entry<String,String> e : map.entrySet())
System.out.println(e.getKey() + " : " + e.getValue());
(String, String) 전용 해시 테이블.Properties prop = new Properties();
prop.setProperty("driver","oracle.jdbc.driver.OracleDriver");
prop.setProperty("url","jdbc:oracle:thin:@127.0.0.1:1521:xe");
prop.setProperty("user","student");
prop.setProperty("password","student");
// 저장
prop.store(new FileOutputStream("driver.dat"), "jdbc driver");
prop.store(new FileWriter("driver.txt"), "jdbc driver");
prop.storeToXML(new FileOutputStream("driver.xml"), "jdbc driver");
// 로드
Properties p2 = new Properties();
p2.load(new FileInputStream("driver.dat"));
p2.load(new FileReader("driver.txt"));
p2.loadFromXML(new FileInputStream("driver.xml"));
p2.list(System.out);
public enum Foods {
MEAL_STEW, MEAL_SOUP, DRINK_LATTE
}
Foods f = Foods.MEAL_STEW;
System.out.println(f); // MEAL_STEW
public enum Foods {
MEAL_STEW(0,"앙버터김치찜"),
DRINK_LATTE(1,"열무김치라떼");
private final int code;
private final String label;
Foods(int code, String label){ this.code=code; this.label=label; }
public int getCode(){ return code; }
public String getLabel(){ return label; }
@Override public String toString(){ return label; } // 출력 커스터마이즈
}
// values(): 전체 순회
for (Foods f : Foods.values()) System.out.println(f);
// valueOf(): 이름으로 찾기
Foods x = Foods.valueOf("MEAL_STEW");
// ordinal(): 선언 순서 인덱스
int idx = Foods.DRINK_LATTE.ordinal();
// name()/toString()
String n1 = Foods.MEAL_STEW.name();
String n2 = Foods.MEAL_STEW.toString();
EnumSet<Foods> all = EnumSet.allOf(Foods.class);
EnumSet<Foods> some = EnumSet.of(Foods.MEAL_STEW);
EnumSet<Foods> except = EnumSet.complementOf(EnumSet.of(Foods.DRINK_LATTE));
// 형식
() -> { /* 본문 */ }
x -> x * x // 매개 1개면 () 생략
(x, y) -> x + y // 본문 1문장이면 {}/return 생략
java.util.functionConsumer: 입력만 받고 리턴 없음
Consumer<String> c = s -> System.out.println(s + " 입력됨");
c.accept("람다");
Supplier: 입력 없음, 값 제공
BooleanSupplier s = () -> ((int)(Math.random()*10)+1) % 2 == 0;
System.out.println(s.getAsBoolean());
Function: 입력 → 다른 타입으로 매핑
Function<Integer,String> f = i -> String.valueOf(i);
ToIntFunction<String> g = Integer::parseInt;
Operator: 입력 → 같은 타입으로 연산/반환
int[] arr = {64,90,80,92,100};
IntBinaryOperator maxOp = Math::max;
int max = Arrays.stream(arr).reduce(maxOp).orElseThrow();
Predicate: 입력을 판별(boolean)
IntPredicate pass = n -> n >= 80;
System.out.println(pass.test(87)); // true
기억법: CSFOP
C(Consumer) 입력만, S(Supplier) 공급, F(Function) 매핑, O(Operator) 동형연산, P(Predicate) 판별
람다를 더 줄인 표기. 타입/매개수/반환형이 함수형 인터페이스와 호환되어야 함.
형식
객체참조::메소드클래스명::정적메소드클래스명::new// 1) 매개변수 메소드 참조
BiFunction<String,String,Boolean> eq = String::equals;
System.out.println(eq.apply("A","A")); // true
// 2) 생성자 참조
class Member { Member(String id){ this.id=id; } String id; }
Function<String, Member> maker = Member::new;
Member m = maker.apply("Lambda");
entrySet()이 가장 깔끔/효율적.java.util.function 조합을 익혀두면 컬렉션/스트림 처리에 강력해짐.