GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Java/Programs/JavaStreams.java at main · PrajaktaSathe/Java Expand file tree/ Copy pathJavaStreams.javaMore file actions 48 lines (38 loc) · 1.64 KB / Copy pathJavaStreams.javaFile metadata and controls48 lines (38 loc) · 1.64 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 import java.util.List; import java.util.stream.Collectors; /* A demo for the Java Streams API Includes examples with lists of numbers and words */ public class JavaStreams { private static final List<Integer> numbers = List.of(10, 2, 21, 23, 22, 14, 16, 18, 19, 9, 4, 6, 7); private static final List<String> words = List.of("apple", "bread", "tree", "computer", "name", "brain", "arm"); //Use stream to find all numbers that are greater than 10 public static List<Integer> findAllNumbersGreaterThanTen(List<Integer> numbers){ return numbers.stream() .filter(number -> number > 10) .sorted() .collect(Collectors.toList()); } //Use stream to find all numbers that contain the letter a public static List<String> findAllWordsThatContainLetterA(List<String> words){ return words.stream() .filter(word -> word.contains("a")) .collect(Collectors.toList()); } //print out the origin lists, and then the changed lists public static void main(String[] args) { System.out.println("The unsorted array is: " + numbers); System.out.println("The sorted array is: " + numbers.stream().sorted().toList()); System.out.println("All numbers in the list greater than 10: " + findAllNumbersGreaterThanTen(numbers)); System.out.println(); System.out.println("A list of words: " + words); System.out.println("All words in the list that contain the letter A: " + findAllWordsThatContainLetterA(words)); } } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |