gpt4 book ai didi

java - Java 中的 lambda 表达式 ClassCastException

转载 作者:行者123 更新时间:2023-11-29 08:21:50 25 4
gpt4 key购买 nike

我正在尝试学习 java 8 中的流式处理。以下是我的代码:

主.java

public class Main {
public static void main(String[] args) {

Person person = new Person("FirstName", "LastName");
List<Person> personList = new ArrayList<>();
personList.add(person);

Place place = new Place("name", "country");
List<Place> placeList = new ArrayList<>();
placeList.add(place);


List<List<Object>> objects = new ArrayList<>();
objects.add(Collections.singletonList(personList));
objects.add(Collections.singletonList(placeList));

List<Object> persons = objects.get(0);
List<String> firstNames = persons.stream()
.map(o -> ((Person)o).getFirstName())
.collect(Collectors.toList());

firstNames.forEach(System.out::println);
}
}

Person.java

@Data
public class Person {
String firstName;
String lastName;

public Person(String firstName, String lastName) {
setFirstName(firstName);
setLastName(lastName);
}
}

Place.java

@Data
public class Place {
String name;
String country;

public Place(String name, String country) {
setName(name);
setCountry(country);
}
}

异常:

Exception in thread "main" java.lang.ClassCastException: java.util.ArrayList cannot be cast to Person
at Main.lambda$main$0(Main.java:28)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.Collections$2.tryAdvance(Collections.java:4717)
at java.util.Collections$2.forEachRemaining(Collections.java:4725)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at Main.main(Main.java:29)

我有 ListList 持有 Object (我正在使用 Object 因为我想使用要存储到集合中的不同类型的对象)。我正在将 Person 的集合和 Place 的集合存储到 List 集合的 List 中。

在流媒体中,我试图获得所有人中唯一的 firstName。但是,当我使用遍历每个元素并获取 firstName 的 lamba 表达式时,它不适用于转换。

问题:

  1. 我做错了什么吗?
  2. 有没有其他方法(除了 map in streaming)通过流 API 获取 Person 对象的所有 FirstName?

最佳答案

personList 是一个人员列表

Collections.singletonList(personList) 是一个 List of Person

objects 是人员/地点列表的列表。

    List<Object> persons = objects.get(0);   // persons is a List of List of Person
List<String> firstNames = persons.stream()
//each element in the stream is a List of Person, so you cannot cast it to Person.
.map(o -> ((Person)o).getFirstName())
.collect(Collectors.toList());

您可以删除 singletonList 函数以减少列表的级别:

    List<List<?>> objects = new ArrayList<>();
objects.add(personList);
objects.add(placeList);

或者在做 map 时更深入地列出一个列表:

    List<String> firstNames = persons.stream() 
//Because persons is declared as List of Objects, so you need to cast each element into a List before calling get
.map(o -> ((Person)((List)o).get(0))).getFirstName())
.collect(Collectors.toList());

关于java - Java 中的 lambda 表达式 ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57084731/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com