In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly talks about "what are the single-line code programming in Java". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what are the single-line code programming in Java?"
1. Multiply each element in the list / array by 2
/ / Range is a half-open interval int [] ia = range (1,10) .map (I-> I * 2) .toArray (); List result = range (1,10) .map (I-> I * 2) .boxed () .collect (toList ())
2. Calculate the sum of numbers in the set / array
Range (1, 1000). Sum (); range (1, 1000). Reduce (0, Integer::sum); Stream.iterate (0, I-> I + 1). Limit (1000). Reduce (0, Integer::sum); IntStream.iterate (0, I-> I + 1). Limit (1000). Reduce (0, Integer::sum)
3. Verify whether the string contains a string in the collection
Final List keywords = Arrays.asList ("brown", "fox", "dog", "pangram"); final String tweet = "The quick brown fox jumps over a lazy dog. # pangram http://www.rinkworks.com/words/pangrams.shtml"; keywords.stream (). AnyMatch (tweet::contains); keywords.stream (). Reduce (false, (b, keyword)-> b | | tweet.contains (keyword), (l, r)-> l | r)
4. Read the file contents
The original author believes that try with resources is also a single-line code programming.
Try (BufferedReader reader = new BufferedReader (new FileReader ("data.txt")) {String fileText = reader.lines () .reduce ("", String::concat);} try (BufferedReader reader = new BufferedReader (new FileReader ("data.txt") {List fileLines = reader.lines () .collect (toCollection (LinkedList::new)) } try (Stream lines = Files.lines (new File ("data.txt"). ToPath (), Charset.defaultCharset ()) {List fileLines = lines.collect (toCollection (LinkedList::new));}
5. Output song "Happy Birthday to You!"-output different strings according to different elements in the collection
Range (1,5) .boxed () .map (I-> {out.print ("Happy Birthday"); if (I = = 3) return "dear NAME"; else return "to You";}) .forEach (out::println)
6. Filter and group the numbers in the collection
Map > result = Stream.of (49, 58, 76, 82, 88, 90). GroupingBy (forPredicate (I-> I > 60, "passed", "failed")
7. Obtain and parse the Web Service of xml protocol
FeedType feed = JAXB.unmarshal (new URL ("http://search.twitter.com/search.atom?&q=java8"), FeedType.class); JAXB.marshal (feed, System.out)
8. Get the smallest / * * number in the set
Int min = Stream.of (14, 35,-7, 46, 98). Reduce (Integer::min). Get (); min = Stream.of (14, 35,-7, 46, 98) .min (Integer::compare). Get (); min = Stream.of (14, 35,-7, 46, 98) .mapToInt (Integer::new). Min (); int max = Stream.of (14, 35,-7, 46, 98). Reduce (Integer::max). Get () Max = Stream.of (14,35,-7,46,98) .max (Integer::compare). Get (); max = Stream.of (14,35,-7,46,98) .mapToInt (Integer::new). Max ()
9. Parallel processing
Long result = dataList.parallelStream () .mapToInt (line-> processItem (line)) .sum ()
10. Various queries on the collection (LINQ in Java)
List albums = Arrays.asList (unapologetic, tailgates, red); / / filter out at least one album with a track rating of more than 4, sort it by name and print it out. Albums.stream () .filter (a-> a.tracks.stream (). AnyMatch (t-> (t.rating > = 4)) .sorted (comparing (album-> album.name)) .forEach (album-> System.out.println (album.name)); / / merge all albums track List allTracks = albums.stream () .flatMap (album-> album.tracks.stream ()) .flatMap (toList ()) / / all track are grouped according to the score of track Map tracksByRating = allTracks.stream () .programming (groupingBy (Track::getRating)). At this point, I believe you have a better understanding of "what is single-line code programming in Java". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.