public class myService {
@Autowired
ExecutorService executor;
public Result getServiceResult(Token token, Profile profile){
//validate token and profile
return getResult(token, profile).get();
}
private getResult (Token token, Profile profile){
Future<Result> = threadPoolExecutor.submit(
() -> myManager.createAccount(token, profile));
}
}
Run Code Online (Sandbox Code Playgroud)
这段代码在我目前的工作中运行良好.我无法理解如何threadPoolExecutor.submit通过"功能/方法"但不能通过callable?
我正在使用Java 8和Spring框架.
鉴于此代码:
class PositionValue {
Object value;
boolean deleted;
}
class Position {
Optional<PositionValue> value = Optional.empty();
}
Position getPosition() {
return positionRepository.findSomePosition();
}
Position findCorrectPosition() {
Position position = getPosition();
return position.value.map(this::finalizeOrNext).orElse(position);
}
Position finalizeOrNext(PositionValue positionValue) {
if (positionValue.deleted) {
return moveToNextPosition(positionValue);
} else {
return finalPosition(positionValue);
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法避免明确声明position变量findCorrectPosition?换句话说,我想orElse函数来引用他的getPosition()调用结果.
我在我们的项目中遇到了以下代码:
MyInterface var = MyClass::new;
Run Code Online (Sandbox Code Playgroud)
有没有区别
MyInterface var = new MyClass();
Run Code Online (Sandbox Code Playgroud)
懒?
java constructor variable-assignment java-8 method-reference
传统上认为术语"流"与I/O操作有关.Java为其功能式操作选择术语"流"的原因是什么?
使用Guava可以通过这种方式确保升序排序:
import com.google.common.collect.Ordering;
import io.predictor.dao.ohlcv.OhlcvHm;
import static java.util.stream.Collectors.toList;
assertThat("Ordered by age", Ordering.natural().isOrdered(
employees.stream().map(Employee::getAge).collect(toList())));
Run Code Online (Sandbox Code Playgroud)
对我来说,Guava(因为它与Java lambdas密切相关)并不能为这种情况提供简单的解决方案.当然,我可以写一些帮助方法并包装它,但也许有人已经在库中完成了它.有一种最简单的方法吗?就像是:
XLibrary.isOrdered(employees, Employee::getAge);
Run Code Online (Sandbox Code Playgroud) 我想知道如何使用Java 8流以及如何使用不同类型的可用流操作.例如,我写了这部分代码:
ArrayList<State> toBeRemoved = new ArrayList<>();
for (State s : newStates)
if (path.contains(s)) // path is a stack of State
toBeRemoved.add(s);
for (State s : toBeRemoved)
newStates.remove(s);
Run Code Online (Sandbox Code Playgroud)
我想用它重写它java 8 stream api calls.我该怎么做?
给定一个List下面的Transaction类,使用Java 8个Lambda表达式,我想获得List的ResultantDTO,每一个帐户类型.
public class Transaction {
private final BigDecimal amount;
private final String accountType;
private final String accountNumber;
}
public class ResultantDTO {
private final List<Transaction> transactionsForAccount;
public ResultantDTO(List<Transaction> transactionsForAccount){
this.transactionsForAccount = transactionsForAccount;
}
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,我使用以下代码对List<Transaction>by 进行分组accountType.
Map<String, List<Transaction>> transactionsGroupedByAccountType = transactions
.stream()
.collect(groupingBy(Transaction::getAccountType));
Run Code Online (Sandbox Code Playgroud)
如何返回a List<ResultantDTO>,将List从每个map键传递到构造函数中,ResultantDTO每个包含一个accountType?
我已将系统数据时间设置为亚洲/加尔各答,它是:
Date: 2015-06-12
Time: 12:07:43.548
Run Code Online (Sandbox Code Playgroud)
现在我在Java 8中编写了以下程序
ZoneId paris = ZoneId.of("Europe/Paris");
LocalDateTime localtDateAndTime = LocalDateTime.now();
ZonedDateTime dateAndTimeInParis = ZonedDateTime.of(localtDateAndTime, paris );
System.out.println("Current date and time in a particular timezone : " + dateAndTimeInParis );
Run Code Online (Sandbox Code Playgroud)
当我运行代码时,它会显示巴黎时间:
Current date and time in a particular timezone :
2015-06-12T12:07:43.548+02:00[Europe/Paris]
Run Code Online (Sandbox Code Playgroud)
上述欧洲/巴黎与亚洲/加尔各答相同.有谁能解释我做错了什么?
更新:我不喜欢使用其他Java包中的类; 因为我听说这个包java.time有足够的功能来处理最大日期时间工作,我希望这个包括:)
我有一个简单Stream的意思是在字节上工作:
List<Byte> byteList = Arrays.stream(new Byte[]{0x1, 0x2, 0x3, 0x4})
.map(b -> b >> 1)
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
编译器给出:
错误:不兼容的类型:推理变量
T具有不兼容的边界平等约束:
java.lang.Byte下界:
java.lang.Integer
这也行不通:
Optional<Byte> aByte = Arrays.stream(new Byte[]{0x1, 0x2, 0x3, 0x4})
.map(b -> b >> 1)
.findFirst();
Run Code Online (Sandbox Code Playgroud)
错误:不兼容的类型:
java.util.Optional<java.lang.Integer>无法转换为java.util.Optional<java.lang.Byte>
我没有找到任何说明流不支持的文档Byte.有什么指针吗?
我正在尝试使用Java-8 lambdas来解决以下问题:
给定一个List<Transaction>,对于每一个Category.minorCategory我需要的总和Transaction.amount每Category.minorCategory和Map的Transaction.accountNumber与总和Transaction.amount每Transaction.accountNumber.根据下面的代码,我有这个工作.我现在要求分组Category.majorCategory,基本上返回一个Map<String, Map<String, MinorCategorySummary>>键入Category.majorCategory.
我一直在努力,直到分组的阶段,Category.majorCategory但很难看到解决方案; 编程与lambdas的范式转换证明了一个陡峭的学习曲线.
TransactionBreakdown是行动发生的地方,我想回到哪里Map<String, Map<String, MinorCategorySummary>>.
public class Transaction {
private final String accountNumber;
private final BigDecimal amount;
private final Category category;
}
public class Category {
private final String majorCategory;
private final String minorCategory;
}
public class MinorCategorySummary {
private final BigDecimal sumOfAmountPerMinorCategory;
private final Map<String, BigDecimal> accountNumberSumOfAmountMap; …Run Code Online (Sandbox Code Playgroud) java-8 ×10
java ×8
java-stream ×5
byte ×1
callable ×1
collections ×1
collectors ×1
constructor ×1
foreach ×1
future ×1
guava ×1
java-time ×1
lambda ×1