Monday, November 7, 2016

How to Remove Duplicates and Sorting in ArrayList using Comparator


 MainComparator class : 


import java.util.ArrayList;
import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;

public class MainComparator {

    public static void main(String args[]) {

        ArrayList studentList = new ArrayList();
        studentList.add(new Student(1, "ram", "HYD"));
        studentList.add(new Student(2, "raj", "HYD"));
        studentList.add(new Student(4, "ram", "SC"));
        studentList.add(new Student(3, "ram", "SC"));

        System.out.println("************************");
        System.out.println("Unsorted list :");
        for (Student liststud : studentList) {
            System.out.println("Student Unsorted List : " + liststud);
        }

        Collections.sort(studentList, new Sortbyroll());
        System.out.println("************************");
        System.out.println("Sorted by rollno :");
        for (Student liststud : studentList) {
            System.out.println("Student sorted List : " + liststud);
        }

        Collections.sort(studentList, new Sortbyname());
        System.out.println("************************");
        System.out.println("Sorted by name:");
        for (Student liststud : studentList) {
            System.out.println("Student sorted List : " + liststud);
        }
       
        System.out.println("************************");

        System.out.println("Remove dublicates based on name and address");
        Set set = new TreeSet(new Removeduplicates());
        set.addAll(studentList);
        studentList.clear();
        studentList = new ArrayList(set);
        for (Student liststud : studentList) {
            System.out.println("Student List : " + liststud);
        }
    }

}


Student class :


public class Student {

    int rollno;
    String name, address;

    public Student(int rollno, String name, String address) {
        this.rollno = rollno;
        this.name = name;
        this.address = address;
    }

    public String toString()
    {
        return this.rollno + " " + this.name +
                           " " + this.address;
    }

}


Sortbyroll class : 

import java.util.Comparator;


public class Sortbyroll implements Comparator {
   
    public int compare(Student s1, Student s2)
    {
        return s1.rollno - s2.rollno;
    }

}

Sortbyname class :

import java.util.Comparator;


public class Sortbyname implements Comparator {

     public int compare(Student s1, Student s2)
        {
            return s1.name.compareTo(s2.name);
        }
}

Removeduplicates class :

import java.util.Comparator;

public class Removeduplicates implements Comparator {

    @Override
    public int compare(Student s1, Student s2) {

        if (s1.address.equalsIgnoreCase(s2.address)
                && s1.name.equalsIgnoreCase(s2.name)) {
            return 0;
        } else {
            return 1;
        }

    }

}

output : 


************************
Unsorted list :
Student Unsorted List : 1 ram HYD
Student Unsorted List : 2 raj HYD
Student Unsorted List : 4 ram SC
Student Unsorted List : 3 ram SC
************************
Sorted by rollno :
Student sorted List : 1 ram HYD
Student sorted List : 2 raj HYD
Student sorted List : 3 ram SC
Student sorted List : 4 ram SC
************************
Sorted by name:
Student sorted List : 2 raj HYD
Student sorted List : 1 ram HYD
Student sorted List : 3 ram SC
Student sorted List : 4 ram SC
************************
Remove dublicates based on name and address
Student List : 2 raj HYD
Student List : 1 ram HYD
Student List : 3 ram SC

No comments:

Post a Comment