JAVA-8 流收集高级用法
JAVA-8 streams collect advanced usage
package streams;
import java.util.Arrays;
import java.util.List;
class Student{
String name;
int age;
String type;
public Student(){}
public Student(String name, int age, String type) {
this.name = name;
this.age = age;
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", type='" + type + '\'' +
'}';
}
public static List<Student> generateData() {
List<Student> st = Arrays.asList(new Student("Ashish", 27, "College"),
new Student("Aman", 24, "School"),
new Student("Rahul", 18, "School"),
new Student("Ajay", 29, "College"),
new Student("Mathur", 25, "College"),
new Student("Modi", 28, "College"),
new Student("Prem", 15, "School"),
new Student("Akash", 17, "School"));
return st;
}
}
//AdvancedStreamingP2 class uses the Student class
package streams;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.*;
public class AdvancedStreamingP2 {
public static void main(String[] args) {
List<Student> studentList = Student.generateData();
System.out.println("\n---------- Extracting Student Name with Max Age by Type -----------");
Map<String, Optional<Student>> stuMax = studentList.stream().collect(groupingBy(Student::getType, maxBy(comparing(Student::getAge))));
stuMax.forEach((k, v) -> System.out.println("Key : " + k + ", Value :" + v.get()));
}
}
我想提取学生姓名,最大年龄,按类型分组学生。 "collect"本身可以使用任意组合吗?
我想要这样的输出:
---------- Extracting Student Name with Max Age by Type -----------
Key : School, Value : Aman
Key : College, Value : Ajay
是的,您可以使用 Collectors.collectingAndThen
。此收集器调整现有收集器以执行额外的整理器操作。在这种情况下,完成操作只是 returns 学生的名字。
Map<String, String> stuMax =
studentList.stream()
.collect(groupingBy(
Student::getType,
collectingAndThen(maxBy(comparing(Student::getAge)), v -> v.get().getName())
));
输出:
---------- Extracting Student Name with Max Age by Type -----------
Key : School, Value :Aman
Key : College, Value :Ajay
旁注:您可以使用 comparingInt
而不是 comparing
,因为 Person.getAge()
returns 和 int
:这避免了不必要的装箱。
Checkout below advancjava8 中的消费者示例:-
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Java8ConsumerExample1 {
private static List<Integer> QUERY_RESULTSET_INTEGER = Arrays.asList(new Integer(1), new Integer(10), new Integer(200), new Integer(101), new Integer(-10), new Integer(0));
private static List<String> QUERY_RESULTSET_STRING = Stream.of("A", "B", "C", "D", "E", "F").collect(Collectors.toList());
public static void main(String[] args) {
// EXAMPLE : 1
/**
* Iterator over the Query generated integer list and print on console.
*/
StubsUtils.forEach(new ExecutorContext() {
List<Integer> consumerList = new ArrayList<Integer>();
/**
* Let's assume that our query returns set of Integer that would be consume by some caller class or print on console.
* @return
*/
@Override
public List<Integer> getQuery() {
return QUERY_RESULTSET_INTEGER;
}
@Override
public Consumer<Integer> getConsumer() {
return x -> {
System.out.println(x);
consumerList.add(x);
};
}
});
// EXAMPLE : 2
/**
* Iterator over the Query generated String list and print on console.
*/
StubsUtils.forEach(new ExecutorContext() {
List<String> consumerList = new ArrayList<String>();
/**
* Let's assume that our query returns set of Integer that would be consume by some caller class or print on console.
* @return
*/
@Override
public List<String> getQuery() {
return QUERY_RESULTSET_STRING;
}
@Override
public Consumer<String> getConsumer() {
return x -> {
System.out.println(x);
consumerList.add(x);
};
}
});
}
}
StubsUtils.Java
/**
* Utility class
*/
class StubsUtils {
public static void forEach(ExecutorContext executorContext) {
executorContext.getQuery().forEach(executorContext.getConsumer());
}
}
ExecutorContext.Java
interface ExecutorContext<E> {
List<Integer> getQuery();
Consumer<E> getConsumer();
}
这是另一个消费者示例:-
package com;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
public class AdvanceConsumerTest {
public static void main(String[] args) {
TestLookupService testLookupService = new TestLookupService();
testLookupService.forEach("A",val->{
System.out.println(" Count of 'A' is "+ val);
});
System.out.println("******************************************");
testLookupService.forEach("B",val->{
System.out.println(" Count of 'B' is "+ val);
});
System.out.println("******************************************");
testLookupService.forEach("C",val->{
System.out.println(" Count of 'C' is "+ val);
});
}
}
class TestLookupService {
void forEach(String parameter, Consumer<Long> stringConsumer) {
LocalRepository.forEach(new QueryExecutionContext() {
@Override
public String getQuery() {
return parameter;
}
@Override
public Consumer<Long> getConsumer() {
return stringConsumer;
}
});
};
}
class LocalRepository {
static DataSetRepo dataSetRepo = new DataSetRepo();
static void forEach(QueryExecutionContext executionContext) {
executionContext.getConsumer().accept(dataSetRepo.queryResults(executionContext));
}
}
interface QueryExecutionContext {
String getQuery();
Consumer<Long> getConsumer();
}
class DataSetRepo {
List<String> cacheOf = Arrays.asList("A", "B", "C", "A", "C", "C", "B");
long queryResults(QueryExecutionContext context) {
return cacheOf.stream().filter(s -> s.equalsIgnoreCase(context.getQuery())).count();
}
}
package streams;
import java.util.Arrays;
import java.util.List;
class Student{
String name;
int age;
String type;
public Student(){}
public Student(String name, int age, String type) {
this.name = name;
this.age = age;
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", type='" + type + '\'' +
'}';
}
public static List<Student> generateData() {
List<Student> st = Arrays.asList(new Student("Ashish", 27, "College"),
new Student("Aman", 24, "School"),
new Student("Rahul", 18, "School"),
new Student("Ajay", 29, "College"),
new Student("Mathur", 25, "College"),
new Student("Modi", 28, "College"),
new Student("Prem", 15, "School"),
new Student("Akash", 17, "School"));
return st;
}
}
//AdvancedStreamingP2 class uses the Student class
package streams;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.*;
public class AdvancedStreamingP2 {
public static void main(String[] args) {
List<Student> studentList = Student.generateData();
System.out.println("\n---------- Extracting Student Name with Max Age by Type -----------");
Map<String, Optional<Student>> stuMax = studentList.stream().collect(groupingBy(Student::getType, maxBy(comparing(Student::getAge))));
stuMax.forEach((k, v) -> System.out.println("Key : " + k + ", Value :" + v.get()));
}
}
我想提取学生姓名,最大年龄,按类型分组学生。 "collect"本身可以使用任意组合吗?
我想要这样的输出:
---------- Extracting Student Name with Max Age by Type -----------
Key : School, Value : Aman
Key : College, Value : Ajay
是的,您可以使用 Collectors.collectingAndThen
。此收集器调整现有收集器以执行额外的整理器操作。在这种情况下,完成操作只是 returns 学生的名字。
Map<String, String> stuMax =
studentList.stream()
.collect(groupingBy(
Student::getType,
collectingAndThen(maxBy(comparing(Student::getAge)), v -> v.get().getName())
));
输出:
---------- Extracting Student Name with Max Age by Type -----------
Key : School, Value :Aman
Key : College, Value :Ajay
旁注:您可以使用 comparingInt
而不是 comparing
,因为 Person.getAge()
returns 和 int
:这避免了不必要的装箱。
Checkout below advancjava8 中的消费者示例:-
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Java8ConsumerExample1 {
private static List<Integer> QUERY_RESULTSET_INTEGER = Arrays.asList(new Integer(1), new Integer(10), new Integer(200), new Integer(101), new Integer(-10), new Integer(0));
private static List<String> QUERY_RESULTSET_STRING = Stream.of("A", "B", "C", "D", "E", "F").collect(Collectors.toList());
public static void main(String[] args) {
// EXAMPLE : 1
/**
* Iterator over the Query generated integer list and print on console.
*/
StubsUtils.forEach(new ExecutorContext() {
List<Integer> consumerList = new ArrayList<Integer>();
/**
* Let's assume that our query returns set of Integer that would be consume by some caller class or print on console.
* @return
*/
@Override
public List<Integer> getQuery() {
return QUERY_RESULTSET_INTEGER;
}
@Override
public Consumer<Integer> getConsumer() {
return x -> {
System.out.println(x);
consumerList.add(x);
};
}
});
// EXAMPLE : 2
/**
* Iterator over the Query generated String list and print on console.
*/
StubsUtils.forEach(new ExecutorContext() {
List<String> consumerList = new ArrayList<String>();
/**
* Let's assume that our query returns set of Integer that would be consume by some caller class or print on console.
* @return
*/
@Override
public List<String> getQuery() {
return QUERY_RESULTSET_STRING;
}
@Override
public Consumer<String> getConsumer() {
return x -> {
System.out.println(x);
consumerList.add(x);
};
}
});
}
}
StubsUtils.Java
/**
* Utility class
*/
class StubsUtils {
public static void forEach(ExecutorContext executorContext) {
executorContext.getQuery().forEach(executorContext.getConsumer());
}
}
ExecutorContext.Java
interface ExecutorContext<E> {
List<Integer> getQuery();
Consumer<E> getConsumer();
}
这是另一个消费者示例:-
package com;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
public class AdvanceConsumerTest {
public static void main(String[] args) {
TestLookupService testLookupService = new TestLookupService();
testLookupService.forEach("A",val->{
System.out.println(" Count of 'A' is "+ val);
});
System.out.println("******************************************");
testLookupService.forEach("B",val->{
System.out.println(" Count of 'B' is "+ val);
});
System.out.println("******************************************");
testLookupService.forEach("C",val->{
System.out.println(" Count of 'C' is "+ val);
});
}
}
class TestLookupService {
void forEach(String parameter, Consumer<Long> stringConsumer) {
LocalRepository.forEach(new QueryExecutionContext() {
@Override
public String getQuery() {
return parameter;
}
@Override
public Consumer<Long> getConsumer() {
return stringConsumer;
}
});
};
}
class LocalRepository {
static DataSetRepo dataSetRepo = new DataSetRepo();
static void forEach(QueryExecutionContext executionContext) {
executionContext.getConsumer().accept(dataSetRepo.queryResults(executionContext));
}
}
interface QueryExecutionContext {
String getQuery();
Consumer<Long> getConsumer();
}
class DataSetRepo {
List<String> cacheOf = Arrays.asList("A", "B", "C", "A", "C", "C", "B");
long queryResults(QueryExecutionContext context) {
return cacheOf.stream().filter(s -> s.equalsIgnoreCase(context.getQuery())).count();
}
}