Thursday 19 November 2015

How to sort a TreeSet with user defined objects In Java


TreeSet have element in their natural ordering. If we make a TreeSet of Sting it have elements in alphabetical ascending order visit example here . If we want to own sorting with TreeSet on user defined objects , have to pass Comparator object along with TreeSet constructor call. The Comparator implementation holds the sorting logic. You have to override compare() method to provide the sorting logic on user defined objects. I have given examples sorted TreeSet of user defined object based on String(name) and Integer(rollNo).

package com.treesetsort;

import java.util.Comparator;
import java.util.TreeSet;

public class TreeSetSort {
public static void main(String[] args) {
TreeSet<Stu> nameSort = new TreeSet<Stu>( new StuNameComp());
nameSort.add(new Stu("yogesh", 10));
nameSort.add(new Stu("shivam", 50));
nameSort.add(new Stu("amit", 30));
nameSort.add(new Stu("pankaj", 35));
System.out.println("printed sorted treeset base on name of student type object");
for (Stu stu : nameSort) {
System.out.println("name ="+ stu.getName()+ " roll No ="+ stu.getRollNo());
}
System.out.println("+++++++++++++++++");
TreeSet<Stu> rollNoSort = new TreeSet<Stu>( new StuRollComp());
rollNoSort.add(new Stu("yogesh", 10));
rollNoSort.add(new Stu("shivam", 50));
rollNoSort.add(new Stu("amit", 30));
rollNoSort.add(new Stu("pankaj", 35));

System.out.println("printed sorted treeset based on roll no of student type object");
for (Stu stu : rollNoSort) {
System.out.println("name ="+ stu.getName()+ " roll No ="+ stu.getRollNo());
}
}
}

class StuNameComp implements Comparator<Stu>{
@Override
public int compare(Stu s1, Stu s2) {
return s1.getName().compareTo(s2.getName());
}
}
class StuRollComp implements Comparator<Stu>{

@Override
public int compare(Stu s1, Stu s2) {
if(s1.getRollNo() > s2.getRollNo()){
return 1;
} else {
return -1;
}
}
}

class Stu {
private String name;
private int rollNo;
public Stu(String name, int rollNo) {
super();
this.name = name;
this.rollNo = rollNo;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRollNo() {
return rollNo;
}
public void setRollNo(int rollNo) {
this.rollNo = rollNo;
}
}



Output:

printed sorted treeset base on name of student type object
name =amit roll No =30
name =pankaj roll No =35
name =shivam roll No =50
name =yogesh roll No =10

+++++++++++++++++

printed sorted treeset based on roll no of student type object
name =yogesh roll No =10
name =amit roll No =30
name =pankaj roll No =35
name =shivam roll No =50



Related Posts: 

How to create TreeSet In Java

How to Convert a TreeSet to ArrayList

How to convert array to TreeSet

How to sort TreeSet with user defined objects.

How to copy all element of a Treeset to other 

How To Remove Element of Treeset 

How to remove all element from TreeSet

How to Remove Set From TreeSet 

How To Convert Treeset To Array 

How to find does TreeSet contains elements or not 

How to count number of element in TreeSet

How to check TreeSet Empty Or not


No comments:

Post a Comment