我试图找到如何在一个衬板中收集对象列表中每个字段的平均值。
这是我要执行的操作:
public class Value {
int a;
int b;
int c;
// rest of the class
}
Run Code Online (Sandbox Code Playgroud)
现在假设我有 List<Value> values = getMillionValues();
我知道要获得一个字段的平均值,我可以执行以下操作:
int averageOfA = values.stream().mapToInt(Value::getA).average()
Run Code Online (Sandbox Code Playgroud)
为了获得所有值的平均值而又没有上面每个变量的重复行,我该怎么办?
也许还有其他一些库(例如Guava)可以帮助执行此类操作?
我搜索了类似的问题,但我只发现了不适用于这种情况的对象字符串:
想要转换List<List<Integer>> list为int[][] array使用流
到目前为止我得到了这个:
int[][] array= list.stream().map(List::toArray)...
Run Code Online (Sandbox Code Playgroud)
我以我发现的其他类似问题为基础。但这些使用 String 并且不能使其适用于 Integers->int:
// Example with String 1:
String[][] array = list.stream()
.map(l -> l.stream().toArray(String[]::new))
.toArray(String[][]::new);
// Example with String 2:
final List<List<String>> list = ...;
final String[][] array = list.stream().map(List::toArray).toArray(String[][]::new);
Run Code Online (Sandbox Code Playgroud) 我正在尝试生成 a 的所有排列,List<List<String>>但我没有得到独特的排列。
我的List<List<String>>看起来像
[
[Test, Test 1, Test 2],
[Apple, Sandwich, Banana],
[Cake, Ice Cream, Fruit]
]
Run Code Online (Sandbox Code Playgroud)
我试图List<String为父级中的每个组合提供所有可能的组合List<String>。因此,例如,第一个实例应该具有:
[Test, Test 1, Test 2]
[Test, Test 2, Test 1]
[Test 2, Test, Test 1]
[Test 2, Test 1, Test]
[Test 1, Test, Test 2]
[Test 1, Test 2, Test]
Run Code Online (Sandbox Code Playgroud)
现在,我的尝试将只是迭代并重复元素而不更改顺序,因此[Test, Test 1, Test 2]只会重复父级的大小List<String>。这是我的方法。任何帮助表示赞赏:
List<List<String>> allPerms = parentList.stream().map(line -> parentList.stream()).flatMap(l -> l.filter(j -> !j.equals(l))).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud) 我需要转换List的POJO,以Map<Integer, List<MyClass>>其中的关键是要从价值MyClass也就是下面的代码如下所示:
public class MyClass {
public int id; // this field value must be key of map.
public String name;
}
public static void main(String... args) {
List<MyClass> lst = new ArrayList();
lst.add(new MyClass(1, "John"));
lst.add(new MyClass(1, "Peter"));
lst.add(new MyClass(2, "Sarah"));
Map<Integer, List<MyClass>> res = lst.collect(..);
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?你能帮助我吗?
鉴于以下代码:
private enum TheA {
AA,
}
private class TheB {
String b;
}
private enum TheC {
CC,
}
private class Rule {
TheA a;
TheB b;
TheC c;
public TheA getA() {
return a;
}
public TheB getB() {
return b;
}
public TheC getC() {
return c;
}
}
private Map<TheA, Map<TheB, Set<TheC>>> createView(Set<Rule> rules) {
Map<TheA, Map<TheB, List<Rule>>> value =
rules.stream().collect(groupingBy(Rule::getA, groupingBy(Rule::getB)));
return value;
}
Run Code Online (Sandbox Code Playgroud)
我想要createView类型检查的类型。目前,我可以根据需要获取嵌套地图,但缺少的是从Set<Rule>到Set<TheC>. 作为额外的奖励,我还希望整个结构是不可变的,因为它仅代表我的数据的特定视图。
鉴于以下classand trait,我想要实现的是创建一个具有特定类型的子类 ie Engageand Event。这是我目前所拥有的:
trait ATrait extends scala.AnyRef {
val test: Option[String]
}
Run Code Online (Sandbox Code Playgroud)
我能够实现以下目标但没有太大用处(因为课程abstract仍然存在)
// this is made 'abstract' by me (can be edited)
abstract class BaseSchema[T, P] extends ATrait {
val test: Option[String]
val data: T
val parent: P
}
abstract class SubClass(override val data: Engage,
override val parent: Event
) extends BaseSchema [Engage, Event]
Run Code Online (Sandbox Code Playgroud)
如果我将 SubClass 转换为 aclass或 a,case class我将不得不实现所有抽象成员。
在 Scala 中处理这个问题的正确方法是什么?在 Java 中,你可以做到
abstract …Run Code Online (Sandbox Code Playgroud) 我有一个这样的班...
class Data { int id, int parent}
Run Code Online (Sandbox Code Playgroud)
还有像这样的对象列表......
List<Data> dataList = new ArrayList();
dataList.add(new Data(1,0));
dataList.add(new Data(2,1));
dataList.add(new Data(3,1));
dataList.add(new Data(4,2));
dataList.add(new Data(5,2));
Run Code Online (Sandbox Code Playgroud)
我想使用流媒体 API 将我的列表收集到一个
Map<Data, List<Data>>
Run Code Online (Sandbox Code Playgroud)
其中地图的键是父级,值是子级。
任何帮助将不胜感激。谢谢
该void accept(T t)方法的目的是什么?它似乎与Stream.Builder<T> add(T t)方法除了返回 void 并且因此不能链接。不返回 Builder 对象是否更有效(似乎次要)?是否存在一些您不希望返回 Builder 的用例?
我在 JDK 文档中没有看到对这个决定的任何解释,也没有在 stackoverflow 上找到这个问题。如果我错过了一些明显的东西,请道歉。
我是一个相对新手的 Stream 用户,我觉得应该有一种更简洁的方法来完成我下面的操作。是否可以在单个 Stream 中完成以下代码的全部操作(消除底部的 if/else)?
谢谢!
Optional<SomeMapping> mapping = allMappings.stream()
.filter(m -> category.toUpperCase().trim().equalsIgnoreCase(m.getCategory().toUpperCase().trim()))
.findAny();
if (mapping.isPresent()) {
return mapping.get();
} else {
throw new SomeException("No mapping found for category \"" + category + "\.");
}
Run Code Online (Sandbox Code Playgroud) 我有一些城市的房屋清单。我正在尝试使用过滤器来生成每个城市最昂贵的房子的列表。我无法使用传统循环。
//This returns unique City Names
List unique = obList.stream()
.map(x -> x.getCity())
.distinct()
.sorted()
.collect(Collectors.toList());
//This returns the house with the highest price in that city
RAddress obReturn = obList.stream()
.filter(x -> x.getCity().equalsIgnoreCase(sName))
.sorted((x,y) -> (int)(y.getPrice() - x.getPrice()))
.findFirst()
.get();
Run Code Online (Sandbox Code Playgroud)
我知道以某种方式将这些结合起来对于解决这个问题是必要的,但我一生都无法弄清楚如何......
任何和所有的帮助表示赞赏。