Java 类型推断在Eclipse Luna中不起作用
我刚开始使用Java8,我正在尝试一些关于收集器的示例。我正在关注《Java8在行动》一书。我对类型推断有疑问: 我使用基本模型类Student,Name来创建示例。PFB详情:Java 类型推断在Eclipse Luna中不起作用,java,lambda,java-8,type-inference,collectors,Java,Lambda,Java 8,Type Inference,Collectors,我刚开始使用Java8,我正在尝试一些关于收集器的示例。我正在关注《Java8在行动》一书。我对类型推断有疑问: 我使用基本模型类Student,Name来创建示例。PFB详情: package com.learning.fundamentals; import java.util.ArrayList; import java.util.List; public class Student { public enum Gender { MALE, FEMALE;
package com.learning.fundamentals;
import java.util.ArrayList;
import java.util.List;
public class Student {
public enum Gender {
MALE, FEMALE;
}
private String id;
private Name name;
private Gender gender;
public Student(String id, Name name, Gender gender) {
this.id = id;
this.name = name;
this.gender = gender;
}
public String getId() {
return id;
}
public Name getName() {
return name;
}
public Gender getGender() {
return gender;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((gender == null) ? 0 : gender.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (gender != other.gender)
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", gender=" + gender
+ "]";
}
}
package com.learning.fundamentals;
public class Name {
private String firstName;
private String middleName;
private String lastName;
public Name(String firstName, String middleName, String lastName) {
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public String getMiddleName() {
return middleName;
}
public String getLastName() {
return lastName;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((firstName == null) ? 0 : firstName.hashCode());
result = prime * result
+ ((lastName == null) ? 0 : lastName.hashCode());
result = prime * result
+ ((middleName == null) ? 0 : middleName.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Name other = (Name) obj;
if (firstName == null) {
if (other.firstName != null)
return false;
} else if (!firstName.equals(other.firstName))
return false;
if (lastName == null) {
if (other.lastName != null)
return false;
} else if (!lastName.equals(other.lastName))
return false;
if (middleName == null) {
if (other.middleName != null)
return false;
} else if (!middleName.equals(other.middleName))
return false;
return true;
}
@Override
public String toString() {
return "Name [firstName=" + firstName + ", middleName=" + middleName
+ ", lastName=" + lastName + "]";
}
}
现在我有一张这样的学生名单
List<Student> studentList = getStudents(); //some method to create the list
List studentList=getStudents()//创建列表的一些方法
现在,让我们尝试一个分组示例。这是一个多层次的分组,首先我使用性别对它进行分组,然后使用一些关于名字的标准(不重要)。代码如下:
Map<Student.Gender, Map<String, List<Student>>> studentsByGenderName =
studentList.stream()
.collect(Collectors.groupingBy(Student::getGender,
Collectors.groupingBy(std -> std.getName().getFirstName().substring(0, 4))));
Map studentsByGenderName=
studentList.stream()
.collect(收集器).groupingBy(学生::getGender,
Collectors.groupby(std->std.getName().getFirstName().substring(0,4));
这在第二个收集器中给了我一个错误,指出“类型对象的方法getName()未定义”。现在,我可以通过提供如下类型的“std”来解决问题:
Map<Student.Gender, Map<String, List<Student>>> studentsByGenderName =
studentList.stream()
.collect(Collectors.groupingBy(Student::getGender,
Collectors.groupingBy((Student std) -> std.getName().getFirstName().substring(0, 4))));
Map studentsByGenderName=
studentList.stream()
.collect(收集器).groupingBy(学生::getGender,
Collectors.groupby((学生标准)->std.getName().getFirstName().substring(0,4));
我的问题是,为什么java不能推断第二个收集器中使用的lambda表达式中的参数类型,而它却能够推断第一个收集器中使用的参数类型 嗯,我无法用EclipseMars.2或JDK1.8.0_74重现您的编译错误。我使用的是JDK1.8.0_65 Eclipse。当使用JDK1.8.0_65显式编译时是否有错误?如果没有,我想你应该升级你的Eclipse。Luna在类型推断方面有很多缺陷。我没有尝试过任何其他版本。但是,您是否能够在不提供“std”类型的情况下运行代码?奇怪:(