我已经阅读了一本关于Java 8的书,该书说使用并行流来获取数字的范围IntStream.range(0,someNumber)可能比连续的慢...为什么?
我有一个自定义类PDFDataItem,提供getValue()方法,该方法返回一个double值(请参阅附加的代码).我还有一个包含多个PDFDataItem实例的ArrayList,每个实例都有一个特定的值:不同的实例可以具有相同的值.我想要做的是创建一个LinkedHashMap,其中单独存储实例的值,并且对于找到的每个值,存储的数量(即它比较的实例的数量).当然,我可以找到许多技巧来实现我的目标,但我想知道是否存在快速方法(可能使用lambda).
public class PDFDataItem{
double value;
public PDFDataItem(double value){
this.value = value;
}
public double getValue(){
return value;
}
}
Run Code Online (Sandbox Code Playgroud) 有一节课
public class Event {
public boolean isActive() {
//...
}
//...
}
Run Code Online (Sandbox Code Playgroud)
有一个TreeSet<Event> events持有数千个元素.我需要有效地找到任何活动Event并获得元素.
我想用a parallelStream来利用多线程.
是否可以像anyMatch()仅返回找到的元素一样进行操作?
boolean hasActiveEvent = events.parallelStream().anyMatch(event -> event.isActive());
Run Code Online (Sandbox Code Playgroud) 我得到了字符串数组列表,如何将它们减少为哈希映射
输入:[a, b, a, a, c, x]
输出:{(a: 3), (b: 1), (c: 1), (x: 1)}
附言。我搜索了这个。我需要这样做,reduce而不是像其他问题那样使用频率计数,因为我的问题是一个简化的实际任务。
List<String> flattened = Stream
.of(Arrays.asList("aa", "bb", "cc"), Arrays.asList("ddd", "eee", "fff"), Arrays.asList("uuuuu", "vvvvv", "kkkkkk"))
.flatMap(Collection::stream).collect(Collectors.toList());
System.out.println(flattened);
Run Code Online (Sandbox Code Playgroud)
现在打印出来[aa, bb, cc, ddd, eee, fff, uuuuu, vvvvv, kkkkkk]
问题:
如何反转 flatMap 操作?转换此流
[kkkkk, eee, uuuuu, bb, aa, vvvvv, fff, ddd, bb]
Run Code Online (Sandbox Code Playgroud)
回到这个
[[cc, bb, aa], [eee, fff, ddd], [kkkkk, vvvvv, uuuuu]]
Run Code Online (Sandbox Code Playgroud) 我在这里没有找到任何关于这个问题的线索。我正在尝试使用 java 流删除一个列表 (cars1) 中存在于另一个列表 (cars2) 中的元素(在我的例子中为汽车)。我尝试使用removeIf,但后来感觉它更适合字符串列表等。
Car c1 = new Car();
c1.id = 1;
c1.name = "C1";
Car c2 = new Car();
c2.id = 2;
c2.name = "C2";
List<Car> cars1 = new ArrayList<Car>();
cars1.add(c1);
cars1.add(c2);
List<Car> cars2 = new ArrayList<Car>();
cars2.add(c2);
// TODO : Remove all the cars from cars1 list that are in cars2 list using java streams
Run Code Online (Sandbox Code Playgroud) 我有以下函数,它尝试逐步缩小输入集合的范围,直到找到单个元素,即,当找到单个项目时,过滤应该停止,因为应用附加过滤器可能会导致根本不匹配。
public List<MyObject> determinePotentialCandidates(List<MyObject> allCandidates) {
List<MyObject> candidates = allCandidates.stream()
.filter(this::firstCondition)
.toList();
if (candidates.size() > 1) {
candidates = candidates.stream()
.filter(this::secondCondition)
.toList();
if (candidates.size() > 1) {
candidates = candidates.stream()
.filter(this::thirdCondition)
.collect(Collectors.toList());
}
// ... and so on
}
logResult(candidates);
return candidates;
}
Run Code Online (Sandbox Code Playgroud)
由于随着每个附加嵌套级别的增加,这变得更难阅读,我想知道是否有更简洁的方法来编写它。
优选地,该方法应该最多执行每个过滤步骤一次(尽管输入大小很小且过滤成本低廉——这可能可以对同一输入执行多次)并且包含单个出口点。
我正在尝试将此 for 循环和 if 条件转换为 Java 流、过滤和收集
final List<Optional<String>> students = listofstudents.get();
final List<String> finalist = new ArrayList<>(students.size());
for(int i=0; i<students.size(); i++) {
if(students.get(i).isPresent()) {
finalist.add(students.get(i).get());
}
else {
finalist.add(null);
}
}
Run Code Online (Sandbox Code Playgroud)
我试过这个:
List<String>result = null;
students.stream()
.filter(e -> e.isPresent())
.map(e->result.add(e))
.orElse(result.add(null)
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
然而,这是完全错误的。我无法让它按预期工作
有人可以告诉我应该如何使用流、过滤器、收集来做到这一点吗?
如果可能的话,我想用 Java 流来解决一个问题。这是我的代码。
class Foo {
private String name;
private String surname;
// getters and setters
}
enum FooError {
NO_NAME("Name cannot be null."), NO_SURNAME("Surname cannot be null");
private final String message;
FooError(final String message) {
this.message = message;
}
public String message() {
return message;
}
}
public FooError validate(final Foo foo) {
if (StringUtils.isBlank(foo.getName())) {
return FooError.NO_NAME;
}
if (StringUtils.isBlank(foo.getSurname())) {
return FooError.NO_SURNAME;
}
return null;
}
public FooError validateFooList(final List<Foo> fooList) {
FooError = fooList.stream()....
}
Run Code Online (Sandbox Code Playgroud)
我想使用流返回列表中第一个 …
我有 ssns 列表Users(id,ssn,name)和 ids 列表的列表,需要转换为 ssns 列表和 id 列表的映射。你能帮忙解决这个问题吗?
List.of(new User(1, "ssn1", "user1"), new User(2, "ssn2", "user2"), new User(3, "ssn3", "user3"))
Run Code Online (Sandbox Code Playgroud)
Map with 2 keys("SSNs","IDs") and list as value
"SSNs" - List("ssn1","ssn2","ssn3")
"IDs" - List(1,2,3)
Run Code Online (Sandbox Code Playgroud)
public class Test {
public static void main(String[] args) {
List<User> users = List.of(new User(1, "ssn1", "user1"), new User(2, "ssn2", "user2"),
new User(3, "ssn3", "user3"));
Map<String, List<Object>> userMap = users.stream().collect(Collectors.groupingBy(User::getSsn));
}
}
class User {
public int getId() {
return id;
} …Run Code Online (Sandbox Code Playgroud)