Bhanu
Bhanu

Reputation: 155

How to remove the element from list if its key is duplicated but value must be appended to single key in java?

I have list ,

But , what i want to do is , In the list ,key is duplicated, so i have to remove that record but the value should be appended to single key

My approach:

ArrayList<Students> students = new ArrayList<Students>();
student = getDetails();
    for(int i=0;i<students.size();i++)
     {
       for(int j=i+1;j<students.size();j++)
        {
           if(students.get(i).getName().equals(students.get(j).getName()))
             {
                string name=students.get(j).getName();
                students.set(i).setName(students.get(i).getName()+","+name"");
                students.remove(j);
             }
        }
     }

here in this i am only able to append the index 1 value to index 0..The loop is getting out for next iteration.

Input:
Name=Student1   Id=1000, 
Name=Student1   Id=1003, 
Name=Student1   Id=1004,


Expected output:
Name=Student1   Id=1000, 1003,1004

Actual getting output:
Name=student1 Id=1000,1003
Name=student1 Id=1004

Upvotes: 1

Views: 51

Answers (2)

Jonny Henly
Jonny Henly

Reputation: 4223

The code in your question does not produce the output you say you are getting, the following code produces the expected output.

class Foo {

    public static void main (String[] args) throws java.lang.Exception
    {
        ArrayList<Student> students = new ArrayList<Student>();
        // student = getDetails();

        // I don't have your database so I added the students manually
        students.add(new Student("Student1", "1000"));
        students.add(new Student("Student1", "1002"));
        students.add(new Student("Student1", "1003"));

        for(int i = 0; i < students.size(); i++) {
            for(int j= i+1; j < students.size(); j++) {
                if(students.get(i).getName().equals(students.get(j).getName())) {

                    // altered this line to produce expected output
                    String id = students.get(j).getId();

                    // also altered this line to produce expected output
                    students.get(i).setId(students.get(i).getId() + "," + id);

                    students.remove(j);

                    j--; // apply bandaid
                }
            }
        }

        for(Student s : students) {
            System.out.println(s.toString());
        }
    }

    private static class Student {
        private String name;
        private String id;

        public Student(String n, String i) {
            name = n;
            id = i;
        }

        public void setName(String n) {
            name = n;
        }

        public void setId(String i) {
            id = i;
        }

        public String getName() {
            return name;
        }

        public String getId() {
            return id;
        }

        public String toString() {
            return name + " " + id;
        }
    }

}

Output:

Student1 1000,1002,1003

Upvotes: 0

joel314
joel314

Reputation: 1080

You are trying to assign values (Student) to keys (Name), where there can be several values for a given key. You should use a Multimap structure instead of an array (cf. Oracle documentation).

That can be done automatically using the Java 8 Streams:

    Student s1 = new Student("Student1", 1000);
    Student s2 = new Student("Student1", 1003);
    Student s3 = new Student("Student1", 1004);
    List<Student> l = Arrays.asList(s1,s2,s3); 
    Map<String, List<Student>> z = l.stream().collect(Collectors.groupingBy(Student::getName));
    System.out.println(z);

The output is as expected: {Student1=[Id=1000, Id=1003, Id=1004]}

To have this result, I had to override the toString() method of Student to print the Id. I hope it helps.

Upvotes: 3

Related Questions