How to initialize Java variables - Array, List, Set, Map
( 이미지 출처 : https://openjdk.java.net )
This post describes how to initialize a variable at the same time as declaring that.
Array
public class Sample {
private final String[] values0 = new String[]{ "1", "2", "3" };
// only available when declaring an array
private final String[] values1 = { "1", "2", "3" };
}
List
Mutable List
public class Sample {
private final List<String> values0 = new ArrayList<>() {
{
add("1");
add("2");
add("3");
}
};
private final List<String> values1 = new ArrayList<>(Arrays.asList(new String[]{ "1", "2", "3" }));
// available in JDK 5 or later
private final List<String> values2 = new ArrayList<>(Arrays.asList("1", "2", "3"));
}
Immutable List
Follows are the how to make immutable List.
If something attempts to change an immutable List, UnsupportedOperationException will be thrown.
public class Sample {
private final List<String> values0 = Arrays.asList(new String[]{ "1", "2", "3" });
private final List<String> values1 = Collections.unmodifiableList(new ArrayList<>(values0));
// available in JDK 5 or later
private final List<String> values2 = Arrays.asList("1", "2", "3");
// available in JDK 9 or later
private final List<String> values3 = List.of("1", "2", "3");
}
Set
Mutable Set
public class Sample {
private final Set<String> values = new HashSet<>(Arrays.asList("1", "2", "3"));
}
Immutable Set
Follows are the how to make immutable Set.
If something attempts to change an immutable Set, UnsupportedOperationException will be thrown.
public class Sample {
private final Set<String> values0 = Collections.unmodifiableSet(new HashSet<String>(Arrays.asList("1", "2", "3")));
// available in JDK 9 or later
private final Set<String> values1 = Set.of("1", "2", "3");
}
Map
Mutable Map
public class Sample {
private final Map<String, String> map = new HashMap<String, String>() {
{
put("key01", "val01");
put("key02", "val02");
put("key03", "val03");
}
};
}
Immutable Map
Follows are the how to make immutable Map.
If something attempts to change an immutable Map, UnsupportedOperationException will be thrown.
Collections is a utility class for the Collection interface.
Although the Map interface does not extend the Collection interface, some feature of Collections class support Collection iterface.
public class Sample {
private final Map<String, String> map0 = Collections.unmodifiableMap(new HashMap<String, String>() {
{
put("key01", "val01");
put("key02", "val02");
put("key03", "val03");
}
});
// available in JDK 9 or later
// only 10 keys can be available
Map<String, String> map1 = Map.of("key01","value01", "key02", "value02");
}
Empty Immutable Collection
Follows are the how to make empty immutable List, Set, and Map.
These empty immutable things are very efficient from resources standpoint because only one exists in the process.
If something attempts to change these, UnsupportedOperationException will be thrown.
public class Sample {
// available in JDK 5 or later
private final List<String> emptyList0 = Collections.emptyList();
private final Set<String> emptySet0 = Collections.emptySet();
private final Map<String, String> emptyMap0 = Collections.emptyMap();
// available in JDK 9 or later
private final List<String> emptyList1 = List.of();
private final Set<String> emptySet1 = Set.of();
private final Map<String, String> emptyMap1 = Map.of();
}
Associated Posts
I have gathered articles with the same tag so that you can explore related topics. Please tap the title.-
유용한 표준 Java RuntimeException
( 이미지 출처 : https://openjdk.java.net )Java 프로그래밍을 하면서 예외처리를 발생시켜야하는 경우, 우리는 RuntimeException을 상속하는 예외를 사용하면 됩니다.
그리고 RuntimeException을 상속하는 예외를 새롭게 만드는 것보다는 JDK에서 제공하는 표준 RuntimeExcepton 상속 예외들을 사용하는 것이 바람직합니다.JDK 12 기준으로 RuntimeException을 직접 상속하는 예외는 총 58개입니다. (참고 - OpenJdk 12 RuntimeException)
그리고 그 58개의 예외들을 다시 상속하는 자식 예외들까지 개수를 세면 엄청나게 많습니다.그 중 자주 사용하게 되는 유용한 표준 RuntimeException 들을 기록합니다.
... more -
Java Thread Safe Collections - List, Queue, Set, Map
( 이미지 출처 : https://openjdk.java.net )Thread safe 한 Collection(List, Queue, Set) 그리고 Map의 구현체 사용법에 대해서 기술합니다.
... more -
Java Random - ThreadLocalRandom, SplittableRandom, SecureRandom
( 이미지 출처 : https://openjdk.java.net )Java에서 제공하는 Random 라이브러리에 대해서 알아봅니다.
... more -
Java Date - Instant, LocalDateTime, ZonedDateTime
-
Java Validation - null check, Optional
-
Java 변수 선언 & 초기화 방법 - Array, List, Set, Map
-
왜 Java 8 을 공부해야 하는가?