In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article shares the content of an example analysis of the collection factory method in Java9. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.
Factories approach to making collection frameworks more convenient
JEP269 proposes adding factory methods to the collection framework to make it easier to create immutable collection classes and maps with a small number of elements. The following is a detailed explanation of why they came into being.
The set framework adds factory methods as a corollary
Java has been criticized for its bloated syntax, for example, creating a small, deterministic collection class (such as a List) that requires using its constructor methods, then storing its references in local variables, calling the add() method multiple times by reference, and finally encapsulating the collection to obtain an immutable view.
List list = new ArrayList();list.add("a");list. add("b"); list.add("c");list = Collections. modifiableList (list);
The above example of syntax so bloated that it could not be simplified in previous versions, immutable static collections had to be filled in static initializers instead of using more convenient field expressions. But,
I also have to mention the following single-statement expressions List list1 = Collections.unmodifiableList(new ArrayList(Arrays.asList("a", "b", "c")));List list2 = Collections.unmodifiableList(new ArrayList() {{ add("a"); add("b"); add("c"); }});List list3 = Collections.unmodifiableList(Stream.of("a", "b", "c").collect(toList()));
The first way is more nonsense, you travel thousands of mountains, you cross weak water, just to get a melon scoop drink, yes, you are not wrong, you go to great lengths to generate a List containing three elements a, b, c, and you have to build an ArrayList also rely on Arrays.asList("a", "b", "c") this black and white way, it is not easy to use, the key is that it is GC after a short life cycle, the process is still invisible...
The second, which may seem like less nonsense, is to use an instance initializer for anonymous inner classes to reduce code bloat. It looks perfect, but can cause memory leaks or serialization problems because it consumes extra resources each time it is used, and contains hidden references to closed instances and any captured objects.
The third way is done using Java 8's Streams API, which is less bloated but involves unnecessary object creation. In addition, the Streams API cannot be used to construct Maps unless the values are computed by keys or the elements of the stream contain key-value pairs.
To solve these problems, JEP186 proposes the concept of a set literal, which is a syntactic expression that creates a List, Map, or other set class in the form of a class array.
List list = #[ "a", "b", "c" ];
There are no new language features, everything is as simple as we think, but why isn't this collection literal integrated into Java9? Instead, Java9 uses the factory approach to replace it, which is actually to minimize language changes, using existing methods to produce syntax sugar to achieve this purpose.
Thus, the set-factory method was born.
Let's take a look at the assembly factory method
JEP 269's factory methods are inspired by factory-like methods in the java.util.Collection and java.util.EnumSet classes. Collections provide factory methods for creating empty Lists, java.util. Sets, and Maps, as well as singleton Lists, Sets, and Maps with one element or key-value pair. EnumSets provide several overloaded of(…) factory methods that take a fixed or variable number of arguments to make it easier to create EnumSets of specified elements. The of() method of the EnumSet model in Java 9 provides a consistent and generic way to create Lists, Sets, and Maps containing objects of any type.
The following factory methods have been added to the List interface static List of()static List of(E e1)static List of (E e1, E e2)static List of (E e1, E e2, E e3)static List of (E e1, E e2, E e3, E e4)static List of (E e1, E e2, E e3, E e4, E e5)static List of (E e1, E e2, E e3, E e4, E e5, E e6)static List of (E e1, E e2, E e3, E e4, E e5, E e6, E e7)static List of (E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8)static List of (E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9) static List of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10)static List of(E... elements) The following factory methods have been added to the Set interface static Set of()static Set of(E e1)static Set of (E e1, E e2)static Set of (E e1, E e2, E e3)static Set of (E e1, E e2, E e3, E e4)static Set of (E e1, E e2, E e3, E e4, E e5)static Set of (E e1, E e2, E e3, E e4, E e5, E e6)static Set of (E e1, E e2, E e3, E e4, E e5, E e6, E e7)static Set of (E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8)static Set of (E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9)static Set of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10)static Set of(E... elements)
In each method list, the first method creates an empty, non-modifiable collection. The next 10 methods create an unmodifiable collection of 1-10 elements. Although these methods are messy, they avoid the array allocation, initialization, and garbage collection overhead of variable-parameter methods of final types, which also support arbitrarily sized collections.
Here are examples of List and Set import java.util.List;import java.util.Set;public class ColDemo{ public static void main(String[] args) { List fruits = List.of("apple", "orange", "banana"); for (String fruit: fruits) System.out.println(fruit); try { fruits.add("pear"); } catch (UnsupportedOperationException uoe) { System.err.println("unable to modify fruits list"); } Set marbles = Set.of("aggie", "alley", "steely"); for (String marble: marbles) System.out.println(marble); try { marbles.add("swirly"); } catch (UnsupportedOperationException uoe) { System.err.println("unable to modify marbles set"); } }}
Output after operation:
apple
orange
banana
unable to modify fruits list
steely
alley
aggie
unable to modify marbles set
The following factory methods are added to the static Map interface of()static Map of(K k1, V v1)static Map of(K k1, V v1, K k2, V v2)static Map of(K k1, V v1, K k2, V v2, K k3, V v3)static Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4)static Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5 static Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6)static Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7 static Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8)static Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9)static Map of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10)static Map ofEntries(Map.Entry
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.