Mohammed Khasim P Mohammed Khasim P -4 years ago 195
Java Question

How to avoid duplicate in ArrayList?


I have used a for loop to add the student details to ArrayList. When I give second student details, it overwrites the first data.
Stud class


import java.util.Scanner;
public class Stud {
private int rollNumber, age;
private String firstName, lastName, gender;
public Stud(int rollNumber, String firstName, String lastName, int age, String gender) {

}
public void setRollnumber(int rollNumber) {
this.rollNumber=rollNumber;
}
public int getRollnumber() {
return rollNumber;
}
public void setFirstname(String firstName) {
this.firstName=firstName;
}
public String getFirstname() {
return firstName;
}
public void setLastname(String lastName) {
this.lastName=lastName;
}
public String getLastname() {
return lastName;
}
public void setAge(int age) {
this.age=age;
}
public int getAge() {
return age;
}
public void setGender(String gender) {
this.gender=gender;
}
public String getGender() {
return gender;
}
}



Main class to add the data to ArrayList.


import java.util.ArrayList;
import java.util.Scanner;


public class MainClass {

public static void main(String[] args) {
ArrayList<Stud> al=new ArrayList<Stud>();
System.out.println("Enter the details:");
for(int i=1;i<=10;i++) {
int rollNumber;
String firstName;
String lastName;
int age;
String gender;
Scanner in=new Scanner(System.in);
rollNumber=in.nextInt();
firstName=in.next();
lastName=in.next();
age=in.nextInt();
gender=in.next();

Stud s=new Stud(rollNumber, firstName, lastName, age, gender);

al.add(s);

for (Stud stud : al) {
stud.setRollnumber(rollNumber);
System.out.println(stud.getRollnumber());
}
}

}

}



Output: actual output for roll number [1] after second data [2,2].
Expected output should be Roll Number: A1
Name: F1 L1
Gender: Male
Age: 11
Sports Event: Javelin

Eligibility: No (Since Javelin is open for the students 15 and above)




Roll Number: A2
Name: F2 L2
Gender: Female
Age: 14
Sports Event: 100 meter run

Eligibility: Yes



Roll Number: A3
Name: F3 L3
Gender: Male
Age: 11
Sports Event: 50 meter run
Eligibility: No (Only for girls ageing 10 to 12)

Answer Source

That should work:

public class MainClass {

   public static void main(String[] args) {
       Set<Stud> al=new HashSet<>(); // use a set
       Scanner in=new Scanner(System.in); // init scanner here!
       System.out.println("Enter the details:");
       for(int i=1;i<=10;i++) {
           int rollNumber;
           String firstName;
           String lastName;
           int age;
           String gender;

           rollNumber=in.nextInt();
           firstName=in.next();
           lastName=in.next();
           age=in.nextInt();
           gender=in.next();

           Stud s=new Stud(rollNumber, firstName, lastName, age, gender);

           al.add(s);

           for (Stud stud : al) {
               System.out.println(stud.getRollnumber());
           }
       }
   }

private static class Stud {
    private final int rollNumber, age;
    private final String firstName, lastName;
    private final String gender;

    // assign the values you get in constructor
    public Stud(int rollNumber, String firstName, String lastName, int age, String gender) {
        this.rollNumber = rollNumber;
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
        this.gender = gender;
    }

    public int getRollnumber() {
        return this.rollNumber;
    }
    public String getFirstname() {
        return this.firstName;
    }
    public String getLastname() {
        return this.lastName;
    }
    public int getAge() {
        return this.rollNumber;
    }
    public String getGender() {
        return this.gender;
    }

    // needed for Set
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + this.age;
        result = prime * result + ((this.firstName == null) ? 0 : this.firstName.hashCode());
        result = prime * result + ((this.gender == null) ? 0 : this.gender.hashCode());
        result = prime * result + ((this.lastName == null) ? 0 : this.lastName.hashCode());
        result = prime * result + this.rollNumber;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Stud other = (Stud) obj;
        if (this.age != other.age)
            return false;
        if (this.firstName == null) {
            if (other.firstName != null)
                return false;
        } else if (!this.firstName.equals(other.firstName))
            return false;
        if (this.gender == null) {
            if (other.gender != null)
                return false;
        } else if (!this.gender.equals(other.gender))
            return false;
        if (this.lastName == null) {
            if (other.lastName != null)
                return false;
        } else if (!this.lastName.equals(other.lastName))
            return false;
        if (this.rollNumber != other.rollNumber)
            return false;
        return true;
        }
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download