summaryrefslogtreecommitdiffhomepage
path: root/java.html.markdown
diff options
context:
space:
mode:
authorEgorBobrov <egorbobrov1776@gmail.com>2021-08-22 21:21:01 +0300
committerGitHub <noreply@github.com>2021-08-22 20:21:01 +0200
commit757ed9b49b8de4a3147fe0a44f91960095fdbc55 (patch)
treeee3fbba4a8ba9e60796171eb31e725f00c922029 /java.html.markdown
parent4eaafe08399ca1e95e2a3d6310fb655312385037 (diff)
[java/en] Added more info about Collections initialization (#4200)
Diffstat (limited to 'java.html.markdown')
-rw-r--r--java.html.markdown53
1 files changed, 46 insertions, 7 deletions
diff --git a/java.html.markdown b/java.html.markdown
index 79769352..4be940be 100644
--- a/java.html.markdown
+++ b/java.html.markdown
@@ -510,10 +510,12 @@ public class LearnJava {
// toString returns this Object's string representation.
System.out.println("trek info: " + trek.toString());
+ } // End main method
+ private static class TestInitialization {
// Double Brace Initialization
- // The Java Language has no syntax for how to create static Collections
- // in an easy way. Usually you end up in the following way:
+ // Before Java 11, the Java Language had no syntax for how to create
+ // static Collections in an easy way. Usually you end up like this:
private static final Set<String> COUNTRIES = new HashSet<String>();
static {
COUNTRIES.add("DENMARK");
@@ -521,10 +523,10 @@ public class LearnJava {
COUNTRIES.add("FINLAND");
}
- // But there's a nifty way to achieve the same thing in an
- // easier way, by using something that is called Double Brace
- // Initialization.
- private static final Set<String> COUNTRIES = new HashSet<String>() {{
+ // There's a nifty way to achieve the same thing,
+ // by using something that is called Double Brace Initialization.
+ private static final Set<String> COUNTRIES_DOUBLE_BRACE =
+ new HashSet<String>() {{
add("DENMARK");
add("SWEDEN");
add("FINLAND");
@@ -535,8 +537,45 @@ public class LearnJava {
// is called when the anonymous inner class is created.
// This does not only work for Collections, it works for all
// non-final classes.
+
+
+ // Another option was to initialize the Collection from an array,
+ // using Arrays.asList() method:
+ private static final List<String> COUNTRIES_AS_LIST =
+ Arrays.asList("SWEDEN", "DENMARK", "NORWAY");
+ // This has one catch: the list we get is internally backed by the array,
+ // and since arrays can't change their size, the list backed by the array
+ // is not resizeable, which means we can't add new elements to it:
+ public static void main(String[] args) {
+ COUNTRIES.add("FINLAND"); // throws UnsupportedOperationException!
+ // However, we can replace elements by index, just like in array:
+ COUNTRIES.set(1, "FINLAND");
+ System.out.println(COUNTRIES); // prints [SWEDEN, FINLAND, NORWAY]
+ }
+ // The resizing problem can be circumvented
+ // by creating another Collection from the List:
+ private static final Set<String> COUNTRIES_SET =
+ new HashSet<>(Arrays.asList("SWEDEN", "DENMARK", "NORWAY"));
+ // It's perfectly fine to add anything to the Set of COUNTRIES now.
+ } // End TestInitialization class
+
+ private static class TestJava11Initialization {
+ // Since Java 11, there is a convenient option to initialize Collections:
+ // Set.of() and List.of() methods.
+ private static final Set<String> COUNTRIES =
+ Set.of("SWEDEN", "DENMARK", "NORWAY");
+ // There is a massive catch, though: Lists and Sets initialized like this
+ // 1) are immutable
+ // 2) can't contain null elements (even check for null elements fails)!
+ public static void main(String[] args) {
+ COUNTRIES.add("FINLAND"); // throws UnsupportedOperationException
+ COUNTRIES.remove("NORWAY"); // throws UnsupportedOperationException
+ COUNTRIES.contains(null); // throws NullPointerException
+ }
+ private static final Set<String> COUNTRIES_WITH_NULL =
+ Set.of("SWEDEN", null, "NORWAY"); // throws NullPointerException
- } // End main method
+ } // End TestJava11Initialization class
} // End LearnJava class
// You can include other, non-public outer-level classes in a .java file,