Skip to main content

Collection(TreeSet)

TreeSet:-TreeSet is an implemented class of Set interface where TreeSet is a group of individual objects as a single entity where duplicates are not allowed but insertion should be according to some sorting order and elements must be comparable. If we are not writing some additional code for sorting than automatically elements insert according to the default natural sorting (ascending order for numbers), if we want to customize sorting than we have to implements Comparator interface present in java.util package and implements compare method. Default natural sorting is done by the comparable interface which is present in java.lang package, having only one method compareTo.

Example1:-TestTreeSet.java
import java.util.*;
class TestTreeSet
{
public static void main(String[]args)
{
TreeSet ts=new TreeSet();
ts.add(50);
ts.add(40);
ts.add(60);
ts.add(30);
ts.add(70);
ts.add(20);
ts.add(80);
ts.add(10);
System.out.println(ts);
Iterator itr=ts.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}
Output:-
C:\JAVATECH>javac TestTreeSet.java
C:\JAVATECH>java TestTreeSet
[10, 20, 30, 40, 50, 60, 70, 80]
10
20
30
40
50
60
70
80

Example2:- TestTreeSet.java
import java.util.*;
class TestTreeSet
{
public static void main(String[]args)
{
TreeSet ts=new TreeSet();
ts.add("Mango");
ts.add("Orange");
ts.add("Graps");
ts.add("Apple");
ts.add("Bhupendra");
ts.add("bhupendra");
ts.add("apple");
System.out.println(ts);
Iterator itr=ts.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}
Output:-
C:\JAVATECH>javac TestTreeSet.java
C:\JAVATECH>java TestTreeSet
[Apple, Bhupendra, Graps, Mango, Orange, apple, bhupendra]
Apple
Bhupendra
Graps
Mango
Orange
apple
bhupendra


If we want to customize sorting then we have to implements Comparator interface and implements compare method.
Example3:- TreeSort.java
import java.util.*;
class Student
{
int id;
String name,course;
Student(int id,String name,String course)
{
this.id=id;
this.name=name;
this.course=course;
}
}
class MyComp implements Comparator<Student>
{
public int compare(Student p,Student q)
{
if(p.id>q.id)
{
return -1;
}
else
{
return 1;
}
}
}
class TreeSort
{
public static void main(String[]args)
{
TreeSet <Student>tr=new TreeSet<Student>(new MyComp());
Student s1=new Student(101,"amit","MCA");
Student s2=new Student(102,"sumit","MA");
Student s3=new Student(103,"sachin","BCA");
Student s4=new Student(104,"neha","M-Tech");
tr.add(s1);
tr.add(s2);
tr.add(s3);
tr.add(s4);
Iterator itr=tr.iterator();
while(itr.hasNext())
{
Student s=(Student)itr.next();
System.out.println(s.id+"     "+s.name+"     "+s.course);
}
}
}
Output:-
C:\JAVATECH>javac TreeSort.java
C:\JAVATECH>java TreeSort
101     amit     MCA
102     sumit     MA
103     sachin    BCA
104     neha     M-Tech
Example4:- TreeSort.java
import java.util.*;
class Student
{
int id;
String name,course;
Student(int id,String name,String course)
{
this.id=id;
this.name=name;
this.course=course;
}
}
class MyComp implements Comparator<Student>
{
public int compare(Student p,Student q)
{
if(p.id>q.id)
{
return -1;
}
else
{
return 1;
}
}
}
class TreeSort
{
public static void main(String[]args)
{
TreeSet <Student>tr=new TreeSet<Student>(new MyComp());
Student s1=new Student(101,"amit","MCA");
Student s2=new Student(102,"sumit","MA");
Student s3=new Student(103,"sachin","BCA");
Student s4=new Student(104,"neha","M-Tech");
tr.add(s1);
tr.add(s2);
tr.add(s3);
tr.add(s4);
Iterator itr=tr.iterator();
while(itr.hasNext())
{
Student s=(Student)itr.next();
System.out.println(s.id+"     "+s.name+"     "+s.course);
}
}
}
Output:-
C:\JAVATECH>javac TreeSort.java
C:\JAVATECH>java TreeSort
104     neha    M-Tech
103     sachin     BCA
102     sumit     MA

101     amit     MCA

Comments

Popular posts from this blog

inheritance

INHERITANCE:- Inheritance is the process of code re-usability, using this property software or applications can develop in less time or can develop rapidly. Inheritance increases the readability of code and decrease redundancy or repetition of code or reduces size of code. Use of previously developed code in future any number of times is called inheritance. Through inheritance variables and methods of class can inherited. Types of Inheritance:- 1)         Single or Single level inheritance 2)         Multilevel inheritance 3)         Hierarchical inheritance Java does not support to multiple inheritance. Single or single level inheritance:- In a single inheritance a class is inherited by a single class. In a single or single level inheritance a sub class having only one super class directly or indirectly. Syntax: -        class Subclass ex...

Inheritance-4

Example10:- class Student { Student() { System.out.println("This is super class Zero argument constructor"); } Student(String s) { System.out.println("This is super class One argument constructor"); } } class Test extends Student { Test() { super(null); System.out.println("This is sub class Zero argument constructor"); } public static void main(String[]args) { Test t=new Test(); } } Output:- C:\JAVATECH>javac Test.java C:\JAVATECH>java Test This is super class One argument constructor This is sub class Zero argument constructor Example11:- class Student { Student() { System.out.println("This is super class Zero argument constructor"); } Student(String s) { System.out.println("This is super class One argument constructor"); } } class Test extends Student { Test() { super(); super(null); System.out.println("This is su...

Abstraction,Encapsulation And Inheritance

Abstraction  is a one of the great feature of Oops,in this feature hide entire details of product, show only requirement specifications(what product will do). The main purpose behind abstraction is to increase productivity or use of product without knowledge of product implementation. in this case the person don't know how product  was implemented but know how product works or action of product. In java abstraction is achieved by abstract class and interface,here interface provide 100% abstraction and abstract class provide partial abstraction. For example: - ATM machine is example of abstraction.  every technical and non-technical person use facility of ATM without knowing the entire process. they know if they click on withdraw button then they can withdraw money from ATM machine but they don't know when they click on withdraw button the request is send to the server and after go into database and so on. Here ATM machine show only requirement specificat...