Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the single-line code programming in Java

Shulou Source: shulou.com Published: 2022-06-02 09:09:54 09月19日 Update

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!

Tags: Code programming content characters strings numbers differences albums elements arrays grouping learning query output practical deeper minimum sum interest original Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Xiaomi Shulou Technology Docker Apple