无法从java中的对象类型列表中打印不同的值。这是我当前的解析器代码,我正在检查重复的值。
在下面的代码中,我试图从newList中获取唯一的值。
XML - sample
<?xml version="1.0"?>
<company>
<student>
<fname>aaa</fname>
<lname>aaa</lname>
<gender>M<gender>
<Address>
<City>Auckland</City>
<Zipcode>2310`enter code here`</Zipcode>
</Address>
<student/>
<student>
<fname>abc</fname>
<lname>aaa</lname>
<gender>f</gender>
<Address>
<City>Wellington</City>
<Zipcode>2310</Zipcode>
</Address>
</student>
<student>
<fname>aaa</fname>
<lname>aaa</lname>
<gender>f</gender>
<Address>
<City>NorthIsland</City>
<Zipcode>5671</Zipcode>
</Address>
</student>
</company>
下面是学生pojo类,类似地,有一个地址pojo类具有地址详细信息,公司类具有学生列表
public class Student {
private String FirstName;
private String LastName;
private String Gender;
private List<Address> address;
public Object student;
public Student() {}
public Student(String FirstName, String LastName, String Gender,List<Address> address) {
super();
this.FirstName = FirstName;
.
.
.
.
}
getters/setter
@Override
public String toString() {
return "<" + FirstName + ", " + LastName + ", " + Gender + ", " + address + ">";
}
@Override
public int hashCode() {
return Objects.hash(student);
}
@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;
return Objects.equals(student, other.student);
}
}
public static void main(String[] args) {
try {
List<Student> student = new ArrayList<Student>();
File file = new File("/Downloads/student.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Company.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Student com= (Student) jaxbUnmarshaller.unmarshal(file);
List<Student> list = com.getStudent();
for(Student stu:list) {
student.add(new Student(stu.getFirstName()+" "+stu.getLastName()+" "+stu.getContactNo()+" "+stu.getGender());
}
List<Student> newList = list.stream().distinct().collect(Collectors.toList());
System.out.println(newList);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
假设您的Student
类名称为&性别字段,然后使用distinct()
的流,你必须覆盖equals
,如下所示:
public class Student {
private String name;
private String sex;
// getters & setters
@Override
public int hashCode() {
return Objects.hash(name, sex);
}
@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;
return Objects.equals(name, other.name) && Objects.equals(sex, other.sex);
}
}
当使用list时:
Student stu= new Student();
stu.setName("A");
stu.setSex("M");
Student stu1= new Student();
stu1.setName("B");
stu1.setSex("F");
Student stu2= new Student();
stu2.setName("C");
stu2.setSex("F");
Student stu3= new Student();
stu3.setName("A");
stu3.setSex("M");
List<Student> list =new ArrayList<Student>();
System.out.println(list.stream().distinct().collect(Collectors.toList()));
会给你3记录而不是4作为一个按equals
Student
对象复制。
所以请检查你的POJO中是否有equals
方法&如果不是,那么请喝一杯。然后在流中使用distict