我想使用Java 8将此while循环转换为等效代码Stream,但我不知道如何从中流式传输List 和删除元素.
private List<String> nameList = new ArrayList<>();
while (nameList.size() > 0) {
String nameListFirstEntry = nameList.get(0);
nameList.remove(0);
setNameCombinations(nameListFirstEntry);
}
Run Code Online (Sandbox Code Playgroud) 我有一组整数,我想计算max()我的流包含的整数数量.该max()方法来自Stream API.
我想要这样的事情
int count = Arrays.stream(myIntArray)
.filter(i -> i == max())
.count();
System.out.printf("Count: %d", count);
Run Code Online (Sandbox Code Playgroud)
我无法max()从我的forEach()方法中调用该方法,因为这不是Streams的功能 - 所以我该怎么做才能使它工作?
我编写了这个类,可以使用构建器模式构建类型为T的数组,将值存储在闭包中,直到实际构造数组.
public class ArrayBuilder<T> {
final Class<T> type;
public ArrayBuilder(Class<T> type){
this.type = type;
}
private Supplier<Supplier<Store>> start = () -> {
final Store element = new Store(-1, null, null);
return () -> element;
};
private class Store {
final Integer index;
final T val;
final Supplier<Store> getNextVal;
public Store(Integer index, T val, Supplier<Store> getNextVal) {
this.index = index;
this.val = val;
this.getNextVal = getNextVal;
}
}
private Supplier<Store> queue(Integer index, T value, Supplier<Store> next) {
final Store element = …Run Code Online (Sandbox Code Playgroud) 我有以下课程:
public class MyClassTest {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void testMyMethod() throws Exception {
// Test 1
String[] args = ...;
MyClass.myMethod(args);
// Test 2
thrown.expect(InvalidParamException.class);
thrown.expectMessage("exceptionString 1");
args = ...;
MyClass.myMethod(args);
// test 3
thrown.expect(InvalidParamException.class);
thrown.expectMessage("exceptionString 2");
args = ...;
MyClass.myMethod(args);
}
Run Code Online (Sandbox Code Playgroud)
问题是测试2中的expectMessage是错误的,为了讨论,实际预期的消息是exceptionString 3,但测试没有失败,尽管预期的消息与实际的消息不同.它变得更奇怪 - 如果我向expectMessage提供诸如"fdsfsdfsdfsdfsdfsdthe"之类的乱码信息 - 那么测试确实会失败:
java.lang.AssertionError:预期:( InvalidParamException和异常的实例,消息包含一个包含"fdsfsdfsdfsdfsdfsdthe"的字符串)但是:带有消息的异常,包含"fdsfsdfsdfsdfsdfsdthe"消息的字符串是"exceptionMessage3"
这是预期的行为 - 但是当我更改exceptionMessage3一点点,字母或两个,甚至发送一个空字符串时 - 测试根本不会失败.
我错过了什么吗?
我正在尝试排序和打印ArrayList按ID排序的内容.
clientArr.sort( (p1, p2) -> p1.getAccount().getID().compareTo(p2.getAccount().getID()) );
for(Client client : clientArr)
System.out.println(client);
Run Code Online (Sandbox Code Playgroud)
我相信我的问题是因为p1并p2期望返回对象,但它们正在获得int价值.我该如何解决这个问题?
ArrayList存储Client对象.客户端类包含String值(在此示例中不重要),它创建Account对象的实例.在Account类中存储了int ID值.这就是我需要的价值
我有一个简单的类,有三个值:
class Book{
private String title;
private String author;
private double price;
}
Run Code Online (Sandbox Code Playgroud)
title是独一无二的(永远).有一个选定的标题列表(as List<String>)和每个Book系统的列表.我希望Book从标题列表中获取所有密钥.我的代码是:
List<Book> allBooks = getAllBooks();
List<String> selectedTitles = getSelectedTitles();
List<Book> selectedBooks = allBooks.stream().
filter(x -> selectedTitles.contains(x.getTitle())).
collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
它的工作原理,但是选择的allBook书籍是通过insted 来订购的selectedTitles.例如:
allBooks(only title) = {"A","B","C","D"}
selectedTitles = {"B","A","D"}
selectedBooks(only title) = {"A","B","D"}
Run Code Online (Sandbox Code Playgroud)
怎么可能(通过stream()订购selectedTitles?)(这个例子应该是{"B","D","A"}?
我们开发了一个使用Java8并行流的API调用,并且我们获得了非常好的性能,与进行压力测试时的顺序处理相比几乎翻了一番.
我知道这取决于用例,但我将它用于加密操作,所以我认为这是一个很好的用例.
但是,我读了很多鼓励他们非常小心的文章.还有文章讨论它们内部设计不是很好,就像这里一样.
因此:准备并行流生产; 它们被广泛用于生产系统吗?
我有一个包含几个选项的对象,其中可选项不为null,我想将其值作为参数传递给需要多个参数的方法.
目前我有一个代码块,如下所示:
if (dealerRequest.getIsApproved().isPresent()) {
repository.updateDealerPartnerFinanceIsApproved(dealerRequest.getDealerId(), dealerRequest.getIsApproved().get());
}
if (dealerRequest.getIsOptedIn().isPresent()) {
repository.updateDealerPartnerFinanceOptedIn(dealerRequest.getDealerId(), dealerRequest.getIsOptedIn().get());
}
Run Code Online (Sandbox Code Playgroud)
我知道检查该值是存在的,然后稍后获取它比以前的空检查更有用; 但是我不知道在这种情况下如何使用它们呢?
理想情况下,我会将.map()作为我仓库中方法的可选项,但后来我不知道如何传递(如果可以的话)第二个参数?有更简洁的方法吗?
我有以下字段声明:
@Entity
public class TransactionStateHistory {
...
@Column(nullable = false)
private LocalDateTime dateTime;
...
}
Run Code Online (Sandbox Code Playgroud)
和以下依赖项:
compile group: 'org.hibernate', name: 'hibernate-java8', version: '5.2.10.Final'
Run Code Online (Sandbox Code Playgroud)
我哪里错了?
我目前正在使用Immutable库从我的Web应用程序生成JSON对象.
看一下这一章,第一行说:
不鼓励使用可空属性.
所以我的问题是:
1)为什么?null对象有什么问题?
2)如果我使用的是第三个对象的包装器并且我不知道item是否为null,那么使用分类构建器代码将失败:
MyImmutableWrapperObject
.builder().
.mobile(input.getMobile()) // don't know if null or not
.build();
Run Code Online (Sandbox Code Playgroud)
有没有最佳解决方案?
编辑:
@JsonProperty("mobile")
public abstract Optional<String> mobile();
...
// composing builder
if (input.getMobile() != null)
builder.mobile(input.getMobile());
Run Code Online (Sandbox Code Playgroud)
制作的json是:
"mobile": {
"present": false
},
Run Code Online (Sandbox Code Playgroud)
我怎样才能完全删除空白字段?
我读了这个,但它使用gson.toJson返回一个String对象,这不是我想要的方式.
POSTEDIT:
我刚刚发现即使存在,Optional也没有显示实际值,但它只显示true/false值,所以我不需要它.
java ×10
java-8 ×10
java-stream ×3
arraylist ×1
closures ×1
exception ×1
forkjoinpool ×1
guava ×1
hibernate ×1
junit ×1
lambda ×1
list ×1
nullable ×1
optional ×1
production ×1
sorting ×1
spring-boot ×1
spring-data ×1