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

Regarding class files

Class files:- Regarding class file or dot class file also known as byte code of class, in a java programming language we can use any number of classes in our program. After compilation all classes convert into individual   .class   file. Example:- class A { } class B { } class C { } class Student { public static void main(String[]args) { System.out.println("Well-come to my java blog"); } } We can save above program to any   classname.java   or we can use any other   name.java,   there is no compulsion for java file name. But recommended to save by that class name which having main method. I saved the above file to   Student.java   name .   Before compilation my   C:\JAVATECH   folder contain only one java file. Student.java For  compilation. C:\JAVATECH>javac  Student.java After compilation   my   C:\JAVATECH   folder contains five files, on...

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