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

Inner classes

Inner Classes:- class inside class or interface called inner class or nested class. Advantage of inner classes is grouping multiple classes and interfaces into a single class. Another big advantage of inner class is that inner class can happily access all the data or variables of outer class directly. Types of Nested classes:- There are two types of nested classes static and non-static nested classes. (A)Non –static inner classes:- 1)     Member inner class 2)     Anonymous inner class 3)     Local inner class (B)Static nested class Example1:-Outer.java class Outer { class Inner { void disp() { System.out.println("Hello This is Inner class Method"); } } public static void main(String[]args) { Inner in=new Inner(); in.disp(); } } Output:- C:\JAVATECH>javac Outer.java Outer.java:12: error: non-static variable this cannot be referenced from a static context Inner in=new Inner(); ...

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...

Object,Data Hiding

Object:- Object is real time entity exist in the real world , the basic purpose of object is to transfer data from one location to another location.object is small memory , allocated inside primary memory(RAM).in java object created inside heap memory,actually in java RAM memory  is divided into some segment according to the type of variables for example. RAM==>Heap area+Stack area+String constant pool+class area  etc. when object created then inside object only non static class variables occupied memory and initialize with default values. non static class variables also known as instance variable reside into object in heap area,static variables occupied memory in class area at the time of class loading from HARD disk to RAM and all local variables occupied memory in stack area when reaching to variables. object  created from the class then what object contains is depend on class structure or class members.             ...