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

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(); ...