前言
4、forEach
@Test
public void stream(){
distinctPrimary("1","2","3");
}
public void distinctPrimary(String... numbers) {
List<String> l = Arrays.asList(numbers);
List<Integer> r = l.stream()
.map(e -> new Integer(e))
.distinct()
.collect(Collectors.toList());
System.out.println("distinctPrimary result is: " + r);
}
package com.hlj.Lambda;
import com.hlj.Lambda.bean.Student;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.util.*;
import java.util.stream.Collectors;
import static java.util.stream.Collectors.*;
public class IntroduceCollectors {
static List<Student> students = null;
static IntroduceCollectors collectors = null;
@BeforeAll
public static void init() {
students = Arrays.asList(
new Student("Ice", "1234", new Date(), null, "Shannxi", "123456", "", "M", null, 170.5, 65.0),
new Student("Leo", "2344", new Date(), null, "Liaoning", "123456", "", "M", null, 175.5, 50.0),
new Student("Mark", "4345", new Date(), null, "Shannxi", "123456", "", "M", null, 169.5, 70.0),
new Student("Will", "4552", new Date(), null, "Fujian", "123456", "", "M", null, 176.5, 45.0),
new Student("Yuan", "4554", new Date(), null, "Fujian", "123456", "", "M", null, 180.5, 56.0),
new Student("Bing", "5677", new Date(), null, "Shannxi", "123456", "", "M", null, 166.5, 67.0),
new Student("Amy", "5675", new Date(), null, "Shannxi", "123456", "", "F", null, 156.5, 78.0),
new Student("Lily", "7567", new Date(), null, "Shannxi", "123456", "", "F", null, 167.5, 66.0),
new Student("Timiy", "4677", new Date(), null, "Shannxi", "123456", "", "F", null, 182.5, 68.0),
new Student("Eline", "4697", new Date(), null, "Liaoning", "123456", "", "F", null, 188.5, 54.0),
new Student("Chrich", "8799", new Date(), null, "Liaoning", "123456", "", "F", null, 155.5, 75.0));
collectors = new IntroduceCollectors();
}
public double averagingDoubleTest(List<Student> students) {
Objects.requireNonNull(students, "The parameter cannot be empty");
return students.stream()
.collect(Collectors.averagingDouble(student -> student.getHeight()));
}
@Test
public void averagingDoubleTestCase() {
System.out.println(collectors.averagingDoubleTest(students));
}
public List<String> collectingAndThenTest(List<Student> students){
Objects.requireNonNull(students, "The parameter cannot be empty");
return students.stream()
.map(student -> student.getName())
.collect(Collectors.collectingAndThen(toList(), Collections::unmodifiableList));
}
@Test
public void collectingAndThenTestCase() {
System.out.println(collectors.collectingAndThenTest(students));
}
public List<String> toCollectionToList(List<Student> students){
return students.stream()
.map(Student::getName)
.collect(Collectors.toCollection(ArrayList::new));
}
public long countingTest(List<Student> students) {
Objects.requireNonNull(students, "The parameter cannot be empty");
return students.stream()
.collect(Collectors.counting());
}
@Test
public void countingTestCase() {
System.out.println(collectors.countingTest(students));
}
public Map<String, List<Student>> groupingByToList(List<Student> students){
Objects.requireNonNull(students, "The parameter cannot be empty");
return students.stream()
.collect(Collectors.groupingBy(student -> student.getFrom()));
}
@Test
public void groupingByToListCase() {
System.out.println(collectors.groupingByToList(students));
}
public Map<String, Set<Student>> groupingByToSet(List<Student> students){
Objects.requireNonNull(students, "The parameter cannot be empty");
return students.stream()
.collect(groupingBy(Student::getFrom, toSet()));
}
@Test
public void groupingByToSetCase() {
System.out.println(collectors.groupingByToSet(students));
}
public Map<String, Set<Student>> groupingByToTreeMap(List<Student> students){
Objects.requireNonNull(students, "The parameter cannot be empty");
return students.stream()
.collect(groupingBy(Student::getFrom, TreeMap::new, toSet()));
}
@Test
public void groupingByToTreeMapCase() {
System.out.println(collectors.groupingByToTreeMap(students));
}
public String joiningTest(List<Student> students) {
Objects.requireNonNull(students, "The parameter cannot be empty");
return students.stream()
.map(Student::getName)
.collect(Collectors.joining());
}
public String joiningDelimiter(List<Student> students) {
Objects.requireNonNull(students, "The parameter cannot be empty");
return students.stream()
.map(Student::getName)
.collect(Collectors.joining(", "));
}
public String joiningDelimiterPreAndSuf(List<Student> students) {
Objects.requireNonNull(students, "The parameter cannot be empty");
return students.stream()
.map(Student::getName)
.collect(joining(", ", "[", "]"));
}
@Test
public void joiningTestCase() {
System.out.println(collectors.joiningTest(students));
System.out.println(collectors.joiningDelimiter(students));
System.out.println(collectors.joiningDelimiterPreAndSuf(students));
}
public Map<String, List<String>> mappingTest(List<Student> students){
Objects.requireNonNull(students, "The parameter cannot be empty");
return students.stream()
.collect(groupingBy(Student::getFrom, Collectors.mapping(Student::getName, toList())));
}
public Student maxByTest(List<Student> students) {
Objects.requireNonNull(students, "The parameter cannot be empty");
return students.stream()
.collect(maxBy(Comparator.comparingDouble(Student::getHeight)))
.get();
}
public Student minByTest(List<Student> students) {
Objects.requireNonNull(students, "The parameter cannot be empty");
return students.stream()
.collect(minBy(Comparator.comparingDouble(Student::getWeight)))
.get();
}
public Map<String, Map<String, Double>> toConcurrentMapTest(List<Student> students){
return students.stream()
.collect(Collectors.toConcurrentMap(Student::getName, Student::getScores));
}
@Test
public void toConcurrentMapTestCase() {
System.out.println(collectors.toConcurrentMapTest(students));
}
public Map<String, Date> toMapTest(List<Student> students){
return students.stream()
.collect(toMap(Student::getName, Student::getBirthday));
}
}