我有一份清单Employee。
public class Employee {
private String department;
private double salary;
// other properties
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试查找平均工资最高的部门的名称。
我尝试过的代码:
List<Employee> employeeList = new ArrayList<Employee>();
employeeList.add(new Employee("abc", 38, "M", "QA", 115000.00d, 2016));
employeeList.add(new Employee("def", 23, "M", "Sports", 5000000.00d, 2011));
employeeList.add(new Employee("ghi", 38, "M", "QA", 120000.00d, 2007));
employeeList.add(new Employee("jkl", 35, "M", "DEV", 50000.00d, 2016));
employeeList.add(new Employee("mno", 30, "F", "QA", 80000.00d, 206));
employeeList.add(new Employee("pqr", 32, "F", "DEV", 75000.00d, 2014));
Map<String, Double> highestAvgSalary = employeeList.stream()
.collect(Collectors.groupingBy(
emp -> emp.getDepartment(),
Collectors.averagingDouble(emp -> emp.getSalary(),
Collectors.maxBy(Comparator.comparingDouble(emp -> …Run Code Online (Sandbox Code Playgroud) 给定一个类 Object1
class Object1 {
private Object2 object2;
private List<Object3> object3s;
}
Run Code Online (Sandbox Code Playgroud)
和一个List<Object1>
当前实施
Map<Object3, Object2> accountMap = new HashMap<>();
for (Object1 dto : accounts) {
dto.getObject3s()
.forEach(dto -> accountMap.put(dto, dto.getObject2()));
}
Run Code Online (Sandbox Code Playgroud)
如何Map<Object3, Object2>在流中创建preferred?
使用流API; 一旦相关数据被过滤,我想编辑正在收集的数据.这是迄今为止的代码:
String wordUp = word.substring(0,1).toUpperCase() + word.substring(1);
String wordDown = word.toLowerCase();
ArrayList<String> text = Files.lines(path)
.parallel() // Perform filtering in parallel
.filter(s -> s.contains(wordUp) || s.contains(wordDown) && Arrays.asList(s.split(" ")).contains(word))
.sequential()
.collect(Collectors.toCollection(ArrayList::new));
Run Code Online (Sandbox Code Playgroud)
编辑下面的代码很糟糕,我试图避免它.(它也没有完全奏效.它是在凌晨4点完成的,请原谅.)
for (int i = 0; i < text.size(); i++) {
String set = "";
List temp = Arrays.asList(text.get(i).split(" "));
int wordPos = temp.indexOf(word);
List<String> com1 = (wordPos >= limit) ? temp.subList(wordPos - limit, wordPos) : new ArrayList<String>();
List<String> com2 = (wordPos + limit < text.get(i).length() -1) …Run Code Online (Sandbox Code Playgroud) 寻找一种简单的方法来收集KeyAndValues having fields - String and List<T>到ImmutableListMultimap<String,T>?试图做一些像,
Collector.of(
ImmutableListMultimap.Builder::new,
ImmutableListMultimap.Builder<String,List<T>>::putAll,
(b1, b2) -> b1.putAll(b2.build()),
(builder) -> builder.build());
Run Code Online (Sandbox Code Playgroud)
putAll需要Key,List<T>.我不知道如何在组合器中实现这一点.
编辑:
@Value(staticConstructor = "of")
private static class KeyAndValues<T> {
private final String key;
private final List<T> values;
}
Run Code Online (Sandbox Code Playgroud) 我需要计算每个字符串的出现次数
ArrayList<ArrayList<String>>
Run Code Online (Sandbox Code Playgroud)
我知道如何使用旧的java 7及以下方法,但是想要使用流和收集器来实现它
理想情况下,我会将结果保存在HashMap<String, Integer>,其中integer是多次出现而String是相应的字符串.
反之亦然 - 这不是什么大问题.
Map<String, Long> counts =
list.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
Run Code Online (Sandbox Code Playgroud)
但我的情况更复杂,因为它是一个ArrayList的ArrayList秒.
结构如下
String itemId = list.get(i).getProducts().get(j).getItemId();
我需要转换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)
我怎样才能做到这一点?你能帮助我吗?
我有以下嵌套的 for 循环,我想转换为使用流,因为我目前正在学习使用流,我该怎么做?
我在下面添加了我当前的尝试,但目前尚不完整。
Part part = getPart();
List<Machines> machines = new ArrayList<>();
List<String> identities = getMachineIdentities();
Set<MachinePart> machineParts = new HashSet<>();
//create machines
for (String identity : identities) {
Machine machine = getMachine(identity);
machines.add(machine);
}
//map to MachineParts
for (Machine machines : machines) {
MachinePart machinePart = MachinePartCreator.new(machines, part);
machineParts.add(machinePart);
}
Run Code Online (Sandbox Code Playgroud)
流尝试:
Set<MachinePart > machineParts = identities.stream()
.map(identity-> ??? ).collectors.collect(Collectors.toSet()));
Run Code Online (Sandbox Code Playgroud) 我有一个 Foo 列表,在每个 Foo 上,我应用了一个处理器方法来获取ValidItem.
如果处理中出现错误,那么我返回ErrorItem.
现在如何通过 Java 8 流处理它以得到 2 个不同列表形式的结果
List<Foo> FooList = someList....;
class ValidItem extend Item{......}
class ErrorItem extend Item{......}
Item processItem(Foo foo){
return either an object of ValidItem or ErrorItem;
}
Run Code Online (Sandbox Code Playgroud)
我相信我能做到
Map<Class,List<Item>> itemsMap =
FooList
.stream()
.map(processItem)
.collect(Collectors.groupingBy(Object::getClass));
Run Code Online (Sandbox Code Playgroud)
但作为List<Parent>不是一个List<Child>让我无法强制转换地图结果变成List<ValidItem>
现实ErrorItem,并ValidItem在所有,没有关系只是为这种蒸汽处理和processItem方法我着想两个完全不同的类让他们在同一层次通过扩展标记项目班级,。
在代码中的许多其他地方,我不能/不应该将 ValidItem 称为 Item ,因为它给出了它也可以是 ErroItem 的想法。
是否有使用流的正确方法,最后我得到 2 个列表。和 ErrorItem 和 ValidItem 没有扩展相同的 Item 类?
############## 更新##############
正如我所说的 ValidItem 和 …
有没有办法为流的每个元素创建 2 个不同的对象并最后收集它们?
例如,如果我有 aList<String> stringList并且有一个GoddClass带有默认值和 a 的类customConstructor,我想在一个流中创建 2 个对象并最后收集
stringList
.stream()
.map(GoddClass::new)
.addAnothrObject(GoddClass::customConstructor) // Not a valid line, Just to depict what is needed
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
一个流可能不是实现我正在尝试的目标的正确解决方案。但这个问题留给专家们来解答。
我有一个Map<String, List<MyObject> myMap。如何将所有 List 值连接到一个List<MyObject>(不包括 String 键)?
我尝试过这个
List<MyObject> list = new ArrayList<MyObject>(myMap.values())
Run Code Online (Sandbox Code Playgroud)
但这不适用于集合。
还考虑过迭代地图并将每个列表加入到新列表中,但希望有更好的方法。
collectors ×10
java ×9
java-stream ×7
java-8 ×5
stream ×2
collections ×1
for-loop ×1
grouping ×1
guava ×1
hashmap ×1