mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 09:41:36 +00:00
[java/en] Added more info about Collections initialization (#4200)
This commit is contained in:
parent
4eaafe0839
commit
757ed9b49b
@ -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");
|
||||
@ -536,7 +538,44 @@ public class LearnJava {
|
||||
// This does not only work for Collections, it works for all
|
||||
// non-final classes.
|
||||
|
||||
} // End main method
|
||||
|
||||
// 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 TestJava11Initialization class
|
||||
} // End LearnJava class
|
||||
|
||||
// You can include other, non-public outer-level classes in a .java file,
|
||||
|
Loading…
Reference in New Issue
Block a user