The Artima Developer Community
Sponsored Link

Java Buzz Forum
Sort employee object in descending order using comparator and TreesSet

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Sort employee object in descending order using comparator and TreesSet Posted: Mar 8, 2016 2:11 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Sort employee object in descending order using comparator and TreesSet
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
1.Basic Java example program sort student object descending order by id using treeset

  1. package com.TreeSetExamplePrograms;

  2. public Class Employee Implements Comparable<Employee>{ 
  3. String name;
  4.     int id;
  5.     
  6.     public String getName() {
  7.         return name;
  8.     }
  9.  
  10.     public void setName(String name) {
  11.         this.name = name;
  12.     }
  13.  
  14.     public int getId() {
  15.         return id;
  16.     }
  17.  
  18.     public void setId(int id) {
  19.         this.id = id;
  20.     }
  21.  
  22.     Student(String name, int id){
  23.         this.name=name;
  24.         this.id=id;
  25.     }
  26.  
  27.     @Override    public int compareTo(Employee obj) {
  28.         if (this.getId() == obj.getId()) return 0;
  29.         else
  30.         
  31.         if (this.getId() < obj.getId()) return 1;
  32.         else return -1;
  33.             
  34. }
  35.  
  36. }


  1. package com.TreeSetExamplePrograms;

  2. public Class Test{ 
  3. public static void main(String args[]) {
  4. //create TreeSet object
  5.  
  6. TreeSet<Employee> treeSet = new TreeSet<Employee>();     
  7.      
  8.        //add elements to TreeSet
  9.     treeSet.add(new Employee("abc",1));
  10.     treeSet.add(new Employee("xyz",2));
  11.     treeSet.add(new Employee("ssd",3));
  12.     treeSet.add(new Employee("ert",4));
  13.          
  14.     Iterator itr = treeSet.iterator();
  15.  
  16.  while(itr.hasNext()){
  17.  
  18.      Employee obj= (Employee)itr.next();
  19.      System.out.println(obj.getName()+"  "+obj.getId()); 

  20.   }         

  21. }
  22. }

Output:

  1. ert  4
  2. ssd  3
  3. xyz  2
  4. abc  1 

Read: Sort employee object in descending order using comparator and TreesSet

Topic: Kids : Animals_v9 Previous Topic   Next Topic Topic: JAXB Schema Validation Example

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use