获取 java.lang.ClassCastException:使用 FlatMap 时无法将类 lambda.Student 强制转换为类 java.lang.Comparable
Posted
技术标签:
【中文标题】获取 java.lang.ClassCastException:使用 FlatMap 时无法将类 lambda.Student 强制转换为类 java.lang.Comparable【英文标题】:Getting java.lang.ClassCastException: class lambda.Student cannot be cast to class java.lang.Comparable while using FlatMap 【发布时间】:2021-07-24 23:10:32 【问题描述】:stream.filter 返回多个结果时获取 java.lang.ClassCastException
package lambda;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
public class LambdaDemo
public static void main(String[] args)
List<Student> stList = new LinkedList<Student>();
stList.add(new Student("Ankit", 22));
stList.add(new Student("Soham", 32));
stList.add(new Student("Nitin", 42));
stList.add(new Student("Xoxo", 55));
stList.add(new Student("Chetan", 62));
Department scrDept = new Department("Science");
scrDept.getStList().add(new Student("Ankit", 22));
scrDept.getStList().add(new Student("Soham", 52));
Department artDept = new Department("Arts");
artDept.getStList().add(new Student("Xoxo", 42));
artDept.getStList().add(new Student("Chetan", 92));
List<Department> deptList = new ArrayList<Department>();
deptList.add(scrDept);
deptList.add(artDept);
deptList.stream().flatMap(dept -> dept.getStList().stream())
.forEach(employee -> System.out.println(employee.getName()));
List<Student> orderedPassedStd = deptList.stream().flatMap(dept -> dept.getStList().stream())
.filter(student -> student.getName().length() >= 4).sorted().collect(Collectors.toList());
for (Student std : orderedPassedStd)
System.out.println(std.getName() + " --------- " + std.getMarks());
class Department
String deptName;
List<Student> stList = new ArrayList<Student>();
public Department(String deptName)
this.deptName = deptName;
public Department(String deptName, List<Student> stList)
this.deptName = deptName;
this.stList = stList;
public String getDeptName()
return deptName;
public void setDeptName(String deptName)
this.deptName = deptName;
public List<Student> getStList()
return stList;
public void setStList(List<Student> stList)
this.stList = stList;
class Student
String name;
float marks;
public String getName()
return name;
public void setName(String name)
this.name = name;
public float getMarks()
return marks;
public void setMarks(float marks)
this.marks = marks;
public Student(String name, float marks)
this.name = name;
this.marks = marks;
输出:Ankit 索汉姆 Xoxo 车坦 线程“主”java.lang.ClassCastException 中的异常:无法将类 lambda.Student 强制转换为类 java.lang.Comparable(lambda.Student 在加载器“app”的模块 javaMasterClass 中;java.lang.Comparable 在模块 java.加载程序“引导程序”的基础) 在 java.base/java.util.Comparators$NaturalOrderComparator.compare(Comparators.java:47) 在 java.base/java.util.TimSort.countRunAndMakeAscending(TimSort.java:355) 在 java.base/java.util.TimSort.sort(TimSort.java:220) 在 java.base/java.util.Arrays.sort(Arrays.java:1515) 在 java.base/java.util.ArrayList.sort(ArrayList.java:1750) 在 java.base/java.util.stream.SortedOps$RefSortingSink.end(SortedOps.java:392) 在 java.base/java.util.stream.Sink$ChainedReference.end(Sink.java:258) 在 java.base/java.util.stream.Sink$ChainedReference.end(Sink.java:258) 在 java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:485) 在 java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474) 在 java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913) 在 java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) 在 java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578) 在 javaMasterClass/lambda.LambdaDemo.main(LambdaDemo.java:34)
【问题讨论】:
如果要对学生对象进行排序,学生类必须实现接口可比较:baeldung.com/java-comparator-comparable 【参考方案1】:在您尝试对学生进行排序的地方,根据您的排序需要,您可以通过以下方式提供比较器:
将包含.sorted()
的行更新为,
如果您想根据姓名对学生进行排序,
.sorted(Comparator.comparing(Student::getName))
或
.sorted((s1, s2) -> s1.getName().compareTo(s2.getName()))
如果您想根据分数对学生进行排序,
.sorted(Comparator.comparing(Student::getMarks))
或
.sorted((s1, s2) -> Float.compare(s1.getMarks(), s2.getMarks()))
如果你想对学生的分数进行降序排序,
.sorted(Comparator.comparing(Student::getMarks).reversed())
【讨论】:
以上是关于获取 java.lang.ClassCastException:使用 FlatMap 时无法将类 lambda.Student 强制转换为类 java.lang.Comparable的主要内容,如果未能解决你的问题,请参考以下文章