X
    Categories: java

Collection sort using objects || Java Example

We will get to know how to use collection framework in java. In this blog we will learn how to use collections sorting, collection in java using comparable object.

 

Collection in java

A collection is a object holding the reference to the objects and using interfaces we can perform operations on them.

java provides interfaces for set, List, Queue,Deque and also for classes like Arraylist, LinkedList, HashSet, Vector, TreeSet,PriorityQueue.

 

ArrayList sorting using Objects:

Now we will sort a list which has student result details.

List<Results> resultsList=new ArrayList<Results>();

 

Here you can see we are using  List<Results>, now lets see Results class

public class Results implements Comparable<Object>{
    private int id;
    private String name;
    private double percentage;

    Results(int id, String name, double percentage){
        this.id=id;
        this.name=name;
        this.percentage=percentage;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }


    public double getPercentage() {
        return percentage;
    }

    @Override
    public int compareTo(Object o) {
        Results result = (Results) o;
        return (int) (this.percentage - result.percentage);
    }
}

 

Add student details according to the Results object class

Results student1=new Results(1, "Sam", 60.57);
Results student2=new Results(2, "Michel", 70.12);
Results student3=new Results(4, "Abhishek", 75.10);
Results student4=new Results(3, "Rachel", 55.43);

 

add details into the arraylist 

resultsList.add(student1);
resultsList.add(student2);
resultsList.add(student3);
resultsList.add(student4);

 

sort the list and print

Collections.sort(resultsList);

 

resultsList.forEach(results -> {
    System.out.println(results.getId() + " " + results.getName() + " " +
            results.getPercentage());
});

 

 

now sort the list in Descending order

Collections.sort(resultsList,Collections.reverseOrder());

 

print list

resultsList.forEach(results -> {
    System.out.println(results.getId() + " " + results.getName() + " " +
            results.getPercentage());
});

 

 

now we will sort using name in the object comparable way need to make a small change in the Results class.

 

As we have to compare strings and sort them introduce compareTo()

@Override
public int compareTo(Object o) {
    Results result = (Results) o;
    return (this.name.compareTo(result.name));
}

 

now pass the same list of elements and remember we are sorting based on name not precentage.

Collections.sort(resultsList);
resultsList.forEach(results -> {
    System.out.println(results.getId() + " " + results.getName() + " " +
            results.getPercentage());
});

 

as we can see name starting with A came up

 

now print in Descending order

Collections.sort(resultsList,Collections.reverseOrder());

 

print list

resultsList.forEach(results -> {
    System.out.println(results.getId() + " " + results.getName() + " " +
            results.getPercentage());
});

 

 

abhishek:
Related Post

This website uses cookies.