JAVA-8流收集高级使用
是的,你可以用。此收集器调整现有收集器以执行附加的修整器操作。在这种情况下,finisher操作只返回学生的名字JAVA-8流收集高级使用,java,lambda,java-8,java-stream,collectors,Java,Lambda,Java 8,Java Stream,Collectors,是的,你可以用。此收集器调整现有收集器以执行附加的修整器操作。在这种情况下,finisher操作只返回学生的名字 ---------- Extracting Student Name with Max Age by Type ----------- Key : School, Value : Aman Key : College, Value : Ajay 旁注:您可以使用因为Person.getAge()返回一个int:这避免了不必要的装箱。java 8中的消费者示例:- --------
---------- Extracting Student Name with Max Age by Type -----------
Key : School, Value : Aman
Key : College, Value : Ajay
旁注:您可以使用因为Person.getAge()
返回一个int
:这避免了不必要的装箱。java 8中的消费者示例:-
---------- Extracting Student Name with Max Age by Type -----------
Key : School, Value :Aman
Key : College, Value :Ajay
ExecutorContext.Java
/**
* Utility class
*/
class StubsUtils {
public static void forEach(ExecutorContext executorContext) {
executorContext.getQuery().forEach(executorContext.getConsumer());
}
}
接口上下文{
List getQuery();
消费者获取消费者();
}
以下是另一个消费者示例:-
interface ExecutorContext<E> {
List<Integer> getQuery();
Consumer<E> getConsumer();
}
package-com;
导入java.util.array;
导入java.util.List;
导入java.util.function.Consumer;
公共课进阶消费测试{
公共静态void main(字符串[]args){
TestLookupService TestLookupService=新建TestLookupService();
testLookupService.forEach(“A”,val->{
System.out.println(“A”的计数为“+val”);
});
System.out.println(“*************************************************************”);
testLookupService.forEach(“B”,val->{
System.out.println(“B”的计数为“+val”);
});
System.out.println(“*************************************************************”);
testLookupService.forEach(“C”,val->{
System.out.println(“C”的计数为“+val”);
});
}
}
类TestLookupService{
void forEach(字符串参数,使用者stringConsumer){
forEach(新的QueryExecutionContext(){
@凌驾
公共字符串getQuery(){
返回参数;
}
@凌驾
公共消费者{
消费者退货;
}
});
};
}
类LocalRepository{
静态DataSetRepo DataSetRepo=新DataSetRepo();
静态void forEach(QueryExecutionContext executionContext){
executionContext.getConsumer().accept(dataSetRepo.queryResults(executionContext));
}
}
接口QueryExecutionContext{
字符串getQuery();
消费者获取消费者();
}
类DataSetRepo{
列表cacheOf=数组。asList(“A”、“B”、“C”、“A”、“C”、“C”、“B”);
长查询结果(QueryExecutionContext上下文){
返回cacheOf.stream().filter(s->s.equalsIgnoreCase(context.getQuery()).count();
}
}
忘了提到我不想使用这个显而易见的解决方案:)stuMax.forEach((k,v)->System.out.println(“Key:+k+”,Value:+v.get().getName())
@Ashu Nope我们不能在这里使用映射
,因为此收集器将流元素映射到某个对象,并根据该映射的结果执行下游收集器。但是在这里,我们不需要收集学生的名字,我们需要按年龄找到最大的名字。如果收集器
界面包含和默认方法:maxBy(comparing(Student::getAge)),然后(v->v.get().getName())
,那就太好了。梦想,梦想…@TagirValeev,尤其是消费者
和功能
拥有和。如果一个人必须先写然后再写。。。顺便说一句,我发现关于和收集器的这一点很好。显然,由于“类型推断问题”而没有这样做。@Tunaki,谢谢你的链接。我认为,现在类型推断应该工作得更好。至少邮件列表中的示例应该有效。。。需要检查。@Ashu的目的是组成。
---------- Extracting Student Name with Max Age by Type -----------
Key : School, Value :Aman
Key : College, Value :Ajay
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);
};
}
});
}
}
/**
* Utility class
*/
class StubsUtils {
public static void forEach(ExecutorContext executorContext) {
executorContext.getQuery().forEach(executorContext.getConsumer());
}
}
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();
}
}