Methods in HashSet in Java

—MethodsHashSet.java—

OUTPUT
methods in hashset

TreeSet in Java

—MyTreeSet.java—

OUTPUT
treeset

HastSet in Java

—MyHashSet.java—

OUTPUT
hastset

TreeSet in Java

In TreeSet binary tree is used. Duplicate cannot be inserted in the tree.
TreeSet implements SortedSet that means ordering is guranteed so any element if it is to be added inside a SortedSet must implement Comparable interface otherwise adding such a element inside the TreeSet will result in class cast exception.some examples implementing TreeSet:

example1:

package collection.set.treeset;

import java.util.Iterator;
import java.util.TreeSet;

/**
*
* @author Abhishek
*/
public class treeset {
public static void main(String args[])
{
TreeSet ts=new TreeSet();
ts.add(“a”);
ts.add(“b”);
ts.add(“c”);
Iterator itr=ts.iterator();
while(itr.hasNext())
{
System.out.println(“”+itr.next());
}
}
}

example2:
package collection.set.treeset;
import java.util.Iterator;
import java.util.TreeSet;
class a implements Comparable
{
    @Override
    public int compareTo(Object o) {
        throw new UnsupportedOperationException(“Not supported yet.”);
    }
}
/**
 *
 * @author Abhishek
 */
public class treeset1 {
    public static void main(String args[])
    {
        a a1=new a();
        TreeSet ts=new TreeSet();
        ts.add(a1);
        a1=new a();
        ts.add(a1);
        Iterator itr=ts.iterator();
        while(itr.hasNext())
        {
            System.out.println(itr.next());
        }
    }
}
/*
 * output
 * Exception in thread “main” java.lang.UnsupportedOperationException: Not supported yet.
at collection.set.treeset.a.compareTo(treeset1.java:15)
at java.util.TreeMap.put(TreeMap.java:545)
at java.util.TreeSet.add(TreeSet.java:238)
at collection.set.treeset.treeset1.main(treeset1.java:30)
Java Result: 1
 */
/*
 * you have to implement compareTo(Object o) then only this error is removed. and this is done how it is done in example4:
 */
example3:
package collection.set.treeset;
import java.util.Iterator;
import java.util.TreeSet;
class aa implements Comparable
{
    @Override
    public int compareTo(Object o) {
        throw new UnsupportedOperationException(“Not supported yet.”);
    }
}
/**
 *
 * @author Abhishek
 */
public class treeset2 {
    public static void main(String args[])
    {
        aa a1=new aa();
        TreeSet ts=new TreeSet();
        ts.add(a1);
        a1=new aa();
        ts.add(a1);
        Iterator itr=ts.iterator();
        while(itr.hasNext())
        {
            System.out.println(itr.next());
        }
    }
}
/*
 * output
 * Exception in thread “main” java.lang.UnsupportedOperationException: Not supported yet.
at collection.set.treeset.aa.compareTo(treeset2.java:15)
at java.util.TreeMap.put(TreeMap.java:545)
at java.util.TreeSet.add(TreeSet.java:238)
at collection.set.treeset.treeset2.main(treeset2.java:30)
Java Result: 1
 */
/*
 * you have to implement compareTo(Object o) then only this error is removed. and this is done how it is done in example4:
 */
example4:
package collection.set.treeset;
import java.util.Iterator;
import java.util.TreeSet;
class employee implements Comparable
{
int empid;
    @Override
    public int compareTo(Object o) {
        System.out.println(“1”);
        employee e=(employee)o;
        return empid -e.empid;  //
    }
    employee(int empid)
    {
        this.empid=empid;
        System.out.println(“empid is “+empid);
    }
 /*   public String toString()
    {
        return “empid is  “+empid;
    }*/
}
/**
 *
 * @author Abhishek
 */
public class treeset3 {
    public static void main(String args[])
    {
        TreeSet ts=new TreeSet();
        employee e=new employee(1234);
        ts.add(e);
        e=new employee(2345);
        ts.add(e);
        e=new employee(3456);
        ts.add(e);
        Iterator itr=ts.iterator();
        while(itr.hasNext())
        {
            System.out.println(itr.next());
        }
    }
}
/*
 * output
 * empid is 1234
empid is 2345
1
empid is 3456
1
1
collection.set.treeset.employee@1fc4bec
collection.set.treeset.employee@dc8569
collection.set.treeset.employee@1bab50a
 */

 


What to hire some one for software projects?
MVC, MySQL, Struts2 & JSP expert ready to complete your work on Freelancer.com
Click on the button

or
Hire Me at https://www.freelancer.com/users/3341136.html?ext=1&action=hireme