In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you the "Java9 collection class extension of method example analysis", the content is easy to understand, well-organized, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "Java9 collection class extension of method example analysis" this article.
Java9 collection class extension of method package com.jd.collections;import org.junit.Test;import java.util.List;import java.util.Map;import java.util.Optional;import java.util.Set;import java.util.stream.IntStream;import java.util.stream.Stream;public class StreamTest {@ Test public void testSet () {Set integerSet = Set.of (1, 2, 3, 4, 5, 6, 7, 8); System.out.println (integerSet) } @ Test public void testList () {List integerSet = List.of (1,2,3,4,5,6,7,8); System.out.println (integerSet);} @ Test public void testMap () {Map stringMap = Map.of ("K1", "v1", "K2", "v2", "K3", "v3"); System.out.println (stringMap) Map.Entry entry1 = Map.entry ("K1", "v1"); Map.Entry entry2 = Map.entry ("K11", "v11"); Map.Entry entry3 = Map.entry ("K12", "v12"); Map mapOfEntries = Map.ofEntries (entry1, entry2, entry3); System.out.println (mapOfEntries) } @ Test public void testStream1 () {Optional integerOptional = Stream.ofNullable (Integer.valueOf ("1232")) .findAny (); System.out.println (integerOptional.get ());} @ Test public void testStream2 () {Stream.of (1,2,3,4,5,6) .dropWhile (x-> x = = 6) / * .forEach (System.out::println) } @ Test public void testStream3 () {IntStream.of (1,2,3,4,5,6) .forEach (System.out::println);} @ Test public void testStream4 () {IntStream.iterate (1, I-> I)
< 10, i ->I + 2) .forEach (System.out::println);} / @ Test// public void testFlow () {/ / Flow.Processor//}} reasons for overloading multiple of methods in the Java9 collection class
In java9 api's collection class, there are many seemingly identical overloaded of methods:
So the question here is why there are so many overloaded methods that need to be defined when there are VarArgs (variable length parameters) methods? You can find it in the official update log.
It is described as follows
Http://openjdk.java.net/jeps/269
These will include varargs overloads, so that there is no fixed limit on the collection size. However, the collection instances so created may be tuned for smaller sizes. Special-case APIs (fixed-argument overloads) for up to ten of elements will be provided. While this introduces some clutter in the API, it avoids array allocation, initialization, and garbage collection overhead that is incurred by varargs calls. Significantly, the source code of the call site is the same regardless of whether a fixed-arg or varargs overload is called.
It roughly means that although overloading so many of methods can cause api confusion, it avoids the array allocation, initialization, and garbage collection overhead caused by varargs calls. Because of overloaded methods with fixed parameters, an immutable list (immutable set) is returned.
The above is all the content of the article "sample Analysis of of methods extended by Collection classes in Java9". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.