我有一群List以薪水为特征的员工。为什么这段代码不起作用?
String joined = employees.stream().collect(
Collectors.summingInt(Employee::getSalary),
Collectors.maxBy(Comparator.comparing(Employee::getSalary)),
Collectors.minBy(Comparator.comparing(Employee::getSalary)),
Collectors.averagingLong((Employee e) ->e.getSalary() * 2),
Collectors.counting(),
Collectors.joining(", "));
Run Code Online (Sandbox Code Playgroud)
我正在使用一套收集器。
我的 groupingBy 方法有问题。我举个例子:假设有一个List<Person>wherePerson.class和City.classare like this
class Person{
String name;
String surname;
City city;
}
class City{
String name;
String postalCode;
}
Run Code Online (Sandbox Code Playgroud)
我会将列表分组city.postalcode并执行如下操作:
List<Person> myList = ...some persons here...
Map<City,List<Person>> groupMap = myList.stream().collect(
Collectors.groupingBy(person -> person));
Run Code Online (Sandbox Code Playgroud)
显然这不起作用,因为groupingBy不知道如何按对象分组。我会做类似的事情Collectors.groupingBy(person -> person.city.postalcode),但是这样会产生一个类型的地图Map<String,List<Person>>,而不是 Map<City,List<Person>>
谢谢你的帮助。
更新
为了groupingBy在类上使用,它必须简单地正确实现 equals并且hashcode
List<Hosting> list = new ArrayList<>();
list.add(new Hosting(1, "liquidweb.com"));
list.add(new Hosting(2, "aws.amazon.com"));
list.add(new Hosting(3, "digitalocean.com"));
list.add(new Hosting(2, "aws.amazon.com"));
Run Code Online (Sandbox Code Playgroud)
我想将上面的列表转换为Map<Integer, Set<String>>
1 -> {"liquidweb.com"}
2 -> {"aws.amazon.com"}
3 -> {"digitalocean.com"}
Run Code Online (Sandbox Code Playgroud)
如何在 Java 8 中进行转换?
示例: 输入:abc565xyz 输出:16 哪种变体更好?谢谢。
public static int sumOfDigits(String s) {
return s.chars().filter(Character::isDigit).reduce(0, (i0, i1) -> i0 + Character.digit(i1, 10));
}
public static int sumOfDigits(String s) {
return s.chars().filter(Character::isDigit).mapToObj(a -> a - '0').reduce(0, (a, b) -> a + b);
}
public static int sumOfDigits(String s) {
return s.chars().filter(Character::isDigit).mapToObj(a -> Character.digit(a, 10)).reduce(0, Integer::sum);
}
Run Code Online (Sandbox Code Playgroud) 我知道它们有相同的效果,但为什么会Integer::intValue出错?
public class ResistorColorDuo {
Map<String, Integer> colValueMap;
public ResistorColorDuo(Map<String, Integer> colValueMap) {
colValueMap.put("Black", 0);
colValueMap.put("Brown", 1);
colValueMap.put("Red", 2);
colValueMap.put("Orange", 3);
colValueMap.put("Yellow", 4);
colValueMap.put("Green", 5);
colValueMap.put("Blue", 6);
colValueMap.put("Violet", 7);
colValueMap.put("Grey", 8);
colValueMap.put("White", 9);
this.colValueMap = colValueMap;
}
public int getResistorValue(String... colors) {
return Arrays.stream(colors).map(i -> colValueMap.get(i) *
Math.pow(10, colors.length - (Arrays.asList(colors).indexOf(i) + 1)))
.mapToInt(Integer::intValue) // Here occurs an exception
.sum();
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码产生了一个错误:
不能从静态上下文中引用非静态方法
虽然它像这样工作得很好:
public int getResistorValue(String... colors) {
return Arrays.stream(colors).map(i -> colValueMap.get(i) *
Math.pow(10, colors.length - …Run Code Online (Sandbox Code Playgroud) 基本上我想要做的是用java 8特性编写一个像下面这样的for循环,
private String createHashCode() {
for (int i = 0; i < MAX_TRY; i++) {
final String hashCode = createRandomString();
if (!ishashCodeExistence(hashCode)) {
return hashCode;
}
}
throw new HashCodeCollisonException();
}
Run Code Online (Sandbox Code Playgroud)
我试过的是;
private String createHashCode() {
IntStream.range(0, MAX_TRY).forEach($ -> {
final String hashCode = createRandomString();
if (!ishashCodeExistence(hashCode)) {
return hashCode;
}
});
throw new HashCodeCollisonException();
}
Run Code Online (Sandbox Code Playgroud)
但是,lambda 中的 foreach 方法返回 void,因此我无法返回字符串。
有什么方法可以编写我的方法而不是使用普通的 for 循环?
使用下面的代码,我可以复制二维数组,但是为什么我不需要在这里为 int[][]::new 指定数组大小?
int[][]source= {{0, 1, 0}, {0, 0, 1}, {1, 1, 1}, {0, 0, 0},{0, 0, 0}};
int[][] destination=Arrays.stream(source)
.map(a -> Arrays.copyOf(a, a.length))
.toArray(int[][]::new);
Run Code Online (Sandbox Code Playgroud) List<Long> trippletList= new ArrayList<>();
trippletList.add(1l);
trippletList.add(3l);
trippletList.add(9l);
trippletList.add(9l);
trippletList.add(27l);
trippletList.add(81l);
Map<Long,Long> arrangedMap=new LinkedHashMap();
arrangedMap=arr.stream().collect(Collectors.groupingBy(Function.identity(),
Collectors.counting()));
Run Code Online (Sandbox Code Playgroud)
尝试根据其频率收集列表以进行映射,其中键将是数组列表中存在的元素,值将是它在数组列表中的出现次数。打印时,第一个元素来了 81. 如何维护订单
所以答案应该是排列地图(1,1)排列地图(3,1)排列地图(9,2)排列地图(27,1)排列地图(81,1)
我OrderDTO在列表中有很多对象。
包含 2 个项目的示例:
final List<OrderDTO> orderList = new ArrayList<OrderDTO>();
final List<OrderDTO> orderList = new ArrayList<OrderDTO>();
final OrderDTO order1 = new OrderDTO("1", "100");
final OrderDTO order2 = new OrderDTO("2", "200");
orderList.add(order1);
orderList.add(order2);
Run Code Online (Sandbox Code Playgroud)
OrderDTO有字段,例如orderId和stateId。
我想在日志中显示:
"1", "2"
Run Code Online (Sandbox Code Playgroud)
或者
"100", "200"
Run Code Online (Sandbox Code Playgroud)
在 Java 11 中,迭代列表并显示每个对象的一个字段的值的最有效方法是什么?
我想将随机数流转换为列表:
public static void main(String[] args) {
System.out.println(
new Random().ints(10, 0, 20).
collect(Collectors.toList()));
}
Run Code Online (Sandbox Code Playgroud)
但这给出了一个例外
java: method collect in interface java.util.stream.IntStream cannot be applied to given types;
required: java.util.function.Supplier<R>,java.util.function.ObjIntConsumer<R>,java.util.function.BiConsumer<R,R>
found: java.util.stream.Collector<java.lang.Object,capture#1 of ?,java.util.List<java.lang.Object>>
reason: cannot infer type-variable(s) R
(actual and formal argument lists differ in length)
Run Code Online (Sandbox Code Playgroud)
那么如何在java中将流转换为列表?