Home > other >  Unable to print distinct values from list of object type in java
Unable to print distinct values from list of object type in java

Time:07-05

Unable to print distinct values from list of object type in java. This is my current parser code and am checking for the duplicate values.

In the below code I am trying to fetch unique values from 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>

Below is the student pojo class and similarly there is one address pojo class having addrress details and Company class having list of students

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();
      }
 
    }
}




CodePudding user response:

Lets say your Student class has name & sex fields, then to use distinct() of streams you have to override equals , something like below:

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);
    }
}

Then while using 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()));

will give you 3 records instead of 4 as one Student object is duplicate as per equals.

So please check if you have equals method present in your POJO & if it is not, then please have one & then use distict within streams.

  • Related