我有一个 forEach 循环,对于性能问题,我被告知使用 Java 流而不是这个。我有多个案例,我的代码如下。我找不到任何包含多个案例的流示例。有人可以帮我转换这个吗?非常感谢。
name = "Serena";
for(Integer age : ages){
if(age>10 && age<20)
methodA(name);
else if(age>20 && age<30)
methodB(name);
else
methodC(name);
Run Code Online (Sandbox Code Playgroud) 我有一封List<String> emails包含电子邮件,长度为n,另一个List<String> keywords包含关键字,长度相同。这些列表应满足以下条件: 对于每个索引i emails.get(i).contains(keywords.get(i))
所以,如果emails.get(0) == "quick brown fox",那么keywords.get(0) == "fox"。
如果emails.get(5) == "foo bar",那么keywords.get(5) == "foo"。
如何检查(除了for循环之外)每封电子邮件都包含关键字?
我将如何使用流来实现与下面相同的结果?我试图通过首先迭代 TickQueue (队列实现)并对 tot 求和,然后除以计数器值来找到平均值,从而找到此“tot”值的平均值。
int counter = 0;
double tot = 0;
for (Tick t: TickQueue)
{
if ((t.getAskPrice() == 0 && t.getBidPrice() == 0) || (t.getAskPrice() == 0) || (t.getBidPrice() == 0))
{
tot += 0;
}
else
{
tot += (t.getAskPrice() - t.getBidPrice());
counter++;
}
}
double avg = tot/counter;
Run Code Online (Sandbox Code Playgroud) 所以我试图通过这个测试:
public void testCountWithCondition() {
List<Integer> data = Arrays.asList(1, 3, 8, 4, 6);
int count = streamer.count(data, x -> x % 3 == 0);
assertEquals(2, count);
}
Run Code Online (Sandbox Code Playgroud)
我很难理解如何创建一个count可以将 lambda 表达式作为参数的方法。我看过其他帖子说您需要创建一个接口类,但是如何对其进行编程才能接受任何 lambda 表达式?另外,作业特别指出我需要使用流。谢谢!
如何使用 java Lambdas 获取数组的乘积。我知道在 C# 中它是这样的:
result = array.Aggregate((a, b) => b * a);
编辑:使问题更清楚。
有没有办法Map<Long, String>直接从groupingByJava 8 中的函数创建,而不是创建Map<Long, Set<String>>?
我有以下代码:
List<Object[]> list = new ArrayList<>();
//row[0] is the primary key and row[1] is the name of the employee
Object[] input1 = { 1l, "employee1", null};
Object[] input2 = { 1l, "employee1", "vorw2"};
Object[] input3 = { 2l, "employee2", "vorw3"};
Object[] input4 = { 3l, "employee3", "vorw1"};
list.add(input1);
list.add(input2);
list.add(input3);
list.add(input4);
//code to replaced
Map<String, Set<String>> byPK= list.stream().collect(Collectors.groupingBy(row -> row[0].longValue(), Collectors.mapping(row -> row[1].toString(),Collectors.toSet() )));
Run Code Online (Sandbox Code Playgroud)
我需要像这样的 o/p:
List ==> (1l, …Run Code Online (Sandbox Code Playgroud) 我希望将 a 转换Collection<Placement>为Map<String, Collection<String>>使用 java 流,其中Placement类如下
public class Placement {
private String id;
/* Links two placements, may be same for atleast 2 placements. */
private String futureLinkId;
}
Run Code Online (Sandbox Code Playgroud)
地图的键是未来链接 id,值是放置 id。例如,如果有 2 个展示位置
p1 { id=1, futureLinkId=f1 }, p2 { id=2, futureLinkId=f1 },则输出应为Map { f1 - { id1, id2 } }
我有一个List<List<String>>如何使用 Java 8 流迭代并将其存储到 Set 中?
我正在沿着这条线尝试一些东西,但我无法完全编译
List <List<String>> itemLists = ...
Set <String> codes = itemLists.stream()
.flatMap(itemList - > {
items.stream()
.collect(Collectors.toSet());
});
Run Code Online (Sandbox Code Playgroud) 这个Java代码可以用Stream API重写吗?
while (true) {
...
if (isTrue()) break;
}
private boolean isTrue() {
...
Run Code Online (Sandbox Code Playgroud) 显然我不擅长 Java 8。有人能告诉我为什么我的集合在迭代过程中没有保留我的所有值吗?如果我放置一个断点,代码会迭代但只返回最后一个值。
Set<Group> groups = new Hashset<>();
Group group = new Group();
List my = model.getValues();
list.stream().forEach(gr -> {
group.setName(gr.getDescription());
group.setDescription(gr.getDescription());
groups.add(group);
});
System.Out.Println(groups);
Run Code Online (Sandbox Code Playgroud) java ×10
java-stream ×10
java-8 ×4
lambda ×4
arraylist ×2
collections ×2
for-loop ×1
foreach ×1
list ×1
loops ×1