Created
May 28, 2024 02:43
-
-
Save pgtwitter/aad7ad3ee53b1e67fa8579c0910f910c to your computer and use it in GitHub Desktop.
Stream, Collectors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.List; | |
import java.util.stream.Collectors; | |
import java.util.Arrays; | |
import java.util.Collections; | |
class a { | |
public static void main(String[] args) { | |
Integer[] array= new Integer[] {1, 2, 3, 4, 5}; | |
List<Integer> list= Arrays.asList(array); | |
System.err.println("sum:"+list.stream().mapToInt(Integer::intValue).sum()); | |
System.err.println("average:"+list.stream().mapToInt(Integer::intValue).average().getAsDouble()); | |
// | |
System.err.print("rotate:"+list); | |
Collections.rotate(list, 2); | |
System.err.println(" -> "+list); | |
// | |
System.err.println("sum:"+list.stream().collect(Collectors.summingInt(Integer::intValue))); | |
System.err.println("ave:"+list.stream().collect(Collectors.averagingInt(Integer::intValue))); | |
System.err.println("min:"+Collections.min(list)); | |
System.err.println("max:"+Collections.max(list)); | |
System.err.println(list.stream().sorted((o1, o2)->o1.compareTo(o2)).collect(Collectors.toList())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment