Sunday, August 4, 2019

TreeSet With User Defined Objects

Q- How to sort a TreeSet with user defined objects?

To store user defined objects in TreeSet you must either implements Comparable or Comparator in your class. if we do not do this then it we throw exception like below mentioned.

Exception in thread "main" java.lang.ClassCastException: com.shubh.example.User cannot be cast to java.lang.Comparable
 at java.util.TreeMap.compare(Unknown Source)
 at java.util.TreeMap.put(Unknown Source)
 at java.util.TreeSet.add(Unknown Source)
 at com.shubh.example.TreeSetExample.main(TreeSetExample.java:10)
Example-1: Using Comparable.

package com.shubh.example;

import java.util.SortedSet;
import java.util.TreeSet;

public class TreeSetExample {

 public static void main(String[] args) {
  SortedSet<User> Users = new TreeSet<>();
  Users.add(new User("Ramesh",3000));
  Users.add(new User("Joson",6000));
  Users.add(new User("Ravi",2000));
  Users.add(new User("Kamal",2400));
        for(User e:Users){
            System.out.println(e);
        }
 }
}
class User implements Comparable<User> {

 private String name;
 private int salary;

 public User(String name, int salary) {
  this.name = name;
  this.salary = salary;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public int getSalary() {
  return salary;
 }

 public void setSalary(int salary) {
  this.salary = salary;
 }

 public String toString() {
  return "Name: " + this.name + "-- Salary: " + this.salary;
 }

 @Override
 public int compareTo(User o) {
  if(this.getSalary() > o.getSalary()){
            return 1;
        } else {
            return -1;
        }
 }
}
Output:-

Name: Ravi-- Salary: 2000
Name: Kamal-- Salary: 2400
Name: Ramesh-- Salary: 3000
Name: Joson-- Salary: 6000
Example-2: Using Comparator.

package com.shubh.example;

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

public class TreeSetUserDefineObjectExample {

 public static void main(String[] args) {
  SortedSet<User> Users = new TreeSet<>(new UserSalaryCom());
  Users.add(new User("Ramesh", 3000));
  Users.add(new User("Joson", 6000));
  Users.add(new User("Ravi", 2000));
  Users.add(new User("Kamal", 2400));
  for (User u : Users) {
   System.out.println(u);
  }
 }
}

class User {

 private String name;
 private int salary;

 public User(String name, int salary) {
  this.name = name;
  this.salary = salary;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public int getSalary() {
  return salary;
 }

 public void setSalary(int salary) {
  this.salary = salary;
 }

 public String toString() {
  return "Name: " + this.name + "-- Salary: " + this.salary;
 }
}

class UserSalaryCom implements Comparator<User> {
 public int compare(User o1, User o2) {
  if (o1.getSalary() == o2.getSalary()) {
   return 0;
  }
  if (o1.getSalary() < o2.getSalary()) {
   return -1;
  } else {
   return 1;
  }
 }
}
Output:

Name: Ravi-- Salary: 2000
Name: Kamal-- Salary: 2400
Name: Ramesh-- Salary: 3000
Name: Joson-- Salary: 6000

No comments:

Post a Comment