(Java) Collection 초기화
- www.baeldung.com/java-init-list-one-line
- www.baeldung.com/java-initialize-hashmap
- www.baeldung.com/java-combine-multiple-collections
1
2
Arrays.asList(“a”, “b”, “c”);
String[] strs = {"a", "b", "c"};
자바의 immutable?
1
2
3
private static final Thing[] PRIVATE_VALUES = { ... };
public static final List<Thing> VALUES = 
    Collections.unmodifiableList(Arrays.asList(PRIVATE_VALUES));
1
2
3
4
5
6
7
8
9
return ImmutableMap.of(
  key1, value1, 
  key2, value2
); 
ImmutableMap.<String, String>builder()  
  .put(key1, value1)  
  .put(key2, value2)  
  .build();
map 초기화 시 runtime type safety를 챙기는 방법 (java 8)
- DB의 number1, number2 컬럼은 모두 NUMBER()타입이다.
- JDBC는 컬럼 타입이 DECIMAL,NUMERIC이면BigDecimal로 매핑하는게 기본 규칙이다.
- 따라서 Map<String, String>에 담기는 실제 데이터 타입은BigDecimal이다.
- new HashMap<String, String>을 사용한 맵 초기화는- BigDecimal이 들어가도 런타임에 에러가 발생하지 않는다. (컴파일 타임에 넣으려고 하면 컴파일 에러는 발생한다.)
- 반면 guava의 ImmutableMap은 런타임에java.lang.ClassCastException발생한다.
- type safety 관점에서 ImmutableMap초기화 사용하는 것이 좋아보인다.
 This post is licensed under  CC BY 4.0  by the author.

