例如,是否可以将字符str.chars()流转换为字符串流,其中每个字符串包含 5 个字符?
我想在过滤后将输入列表转换为不同的列表。请知道如何在java8流中实现。
我尝试了类似下面的方法,它在 collect() 中的 getOutput() 中给出了编译错误“p 无法解析为变量”。
List<Output> outputList= inputList.stream()
.filter(p -> p.param1==10)
.collect(Collectors.toList(getOutput(p)));
private Output getOutput(Input inp){
Output out = new Output();
out.value1= inp.value1;
---
---
}
Run Code Online (Sandbox Code Playgroud) 我有一个方法getBalance,其中我有一个增强的 for 循环,并在我返回的循环内customer.getBalance();
我现在想将该方法更改为 Java 8 中的 Stream,但我不知道该怎么做,因为您无法在流中返回某些内容。
方法:
public double getBalance(String personalNumber, int accountNumber) {
for (Customer customer : customers) {
if (customer.getPersonalNumber().equals(personalNumber)) {
return customer.getBalance(accountNumber);
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我有一个这样的字符串
String input = "abc|label1 cde|label2 xyz|label1 mno|label3 pqr|label2";
Run Code Online (Sandbox Code Playgroud)
我想创建一个看起来像的地图(过滤掉 label3} label1 -> {abc,xyz} label2 -> {cde,pqr} label3 -> {mno}
这是我目前能做的
Map<String, List<String>> result = Arrays.stream(inputString.split(" "))
.filter(i -> !i.contains("label3"))
.map(i -> i.split("//|"))
Run Code Online (Sandbox Code Playgroud)
还有第二个用例:我如何只收集一个字符串中的所有令牌
abc|label1 cde|label2 xyz|label1 mno|label3 pqr|label2 => "abc cde xyz mno pqr"
假设有如下图所示:
Map<String, List<String>> myMap =
{
k1: [ v1, v2, v3, v3],
k2: [ v1, v2],
k3: [ v1, v2, v6, v7]
}
Run Code Online (Sandbox Code Playgroud)
我想找到所有键通用的值。
List<String> commonValues = {v1,v2}
Run Code Online (Sandbox Code Playgroud)
我想了解使用 java 8 的有效方法。我可以在 java 5 中使用 for 循环实现相同的效果,但我确信 Java 8 有更好的方法来做到这一点。
我有 2 节课A和B
class A
{
int id,
List<B>
}
Run Code Online (Sandbox Code Playgroud)
class B
{
int id
}
Run Code Online (Sandbox Code Playgroud)
现在给出List<A>,我想过滤掉所有B.id> 3
前任。
List<A>:
{
[
id: 1,
Bs: [1, 2, 3, 4, 5]
],
[
id: 2,
Bs: [1, 2, 3, 4, 5]
]
}
Run Code Online (Sandbox Code Playgroud)
预期结果是
{
[
id: 1,
Bs: [1, 2, 3]
],
[
id: 2,
Bs: [1, 2, 3]
]
}
Run Code Online (Sandbox Code Playgroud)
怎么办stream, filter?谢谢!!!
编码时使用java流显示错误
Optional.ofNullable(product.getStudents())
.orElseGet(Collections.emptyList())
.stream().map(x->x.getId)
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
此代码显示以下错误 错误
不兼容的类型,必需的供应商> 但空列表被干扰到 List : 不存在 T 类型变量的实例,因此 List 符合供应商>
但是如果我替换Collections.emptyList()WITH Collections::emptyList
It 就完美了。
Collections.emptyList() 与 Collections::emptyList 有什么区别?
考虑以下代码:
public class StreamDemo {
public static void main(String[] args) {
StreamObject obj = new StreamObject();
obj.setName("mystream");
List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
list.parallelStream().forEach(l -> {
obj.setId(l);
System.out.println(obj + Thread.currentThread().getName());
});
}
static public class StreamObject {
private String name;
private Integer id;
// getters, setters, toString()
}
}
Run Code Online (Sandbox Code Playgroud)
当使用 java 11 编译并运行时,它返回以下内容:
StreamObject{name='mystream', id=4}ForkJoinPool.commonPool-worker-23
StreamObject{name='mystream', id=4}main
StreamObject{name='mystream', id=4}ForkJoinPool.commonPool-worker-9
StreamObject{name='mystream', id=4}ForkJoinPool.commonPool-worker-5
StreamObject{name='mystream', id=4}ForkJoinPool.commonPool-worker-19
Run Code Online (Sandbox Code Playgroud)
但对于 java 1.8,它返回不同的结果:
StreamObject{name='mystream', id=3}main
StreamObject{name='mystream', id=5}ForkJoinPool.commonPool-worker-2
StreamObject{name='mystream', id=2}ForkJoinPool.commonPool-worker-9
StreamObject{name='mystream', id=1}ForkJoinPool.commonPool-worker-11
StreamObject{name='mystream', id=4}ForkJoinPool.commonPool-worker-4
Run Code Online (Sandbox Code Playgroud)
为什么结果不同?
如何将此for循环更改为流?
public int calculateForSpliterator(String[] matchTab, String spliterator, RulesChecker rulesChecker) {
int finalScore = 0;
for (String element : matchTab) {
String[] splitScroes = element.split(spliterator);
int ourPoints = Integer.parseInt(splitScroes[0]);
int theirPoints = Integer.parseInt(splitScroes[1]);
finalScore += rulesChecker.checkRules(ourPoints, theirPoints);
}
return finalScore;
}
Run Code Online (Sandbox Code Playgroud) 我正在对对象列表进行分组,如下面的代码所示
Map<String, List<InventoryAdjustmentsModel>> buildDrawNumEquipmentMap = equipmentsAndCargoDetails.stream().
collect(Collectors.groupingBy(InventoryAdjustmentsModel :: getBuildDrawNum));
Run Code Online (Sandbox Code Playgroud)
现在我知道所有键的值只有一个元素,所以我怎样才能将它减少到只有一个元素
Map<String, InventoryAdjustmentsModel>
Run Code Online (Sandbox Code Playgroud)
而不是必须遍历或获取所有键的第 0 个元素。