From 757ed9b49b8de4a3147fe0a44f91960095fdbc55 Mon Sep 17 00:00:00 2001 From: EgorBobrov Date: Sun, 22 Aug 2021 21:21:01 +0300 Subject: [java/en] Added more info about Collections initialization (#4200) --- java.html.markdown | 53 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 file 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 COUNTRIES = new HashSet(); 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 COUNTRIES = new HashSet() {{ + // There's a nifty way to achieve the same thing, + // by using something that is called Double Brace Initialization. + private static final Set COUNTRIES_DOUBLE_BRACE = + new HashSet() {{ 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 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 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 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 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, -- cgit v1.2.3