Java collection HashSet and TreeSet



Share on Google+Share on Google+

Naulej kumar
Java collection HashSet and TreeSet
1 Answer(s)      4 years and 11 months ago
Posted in : Java Beginners

How can we used HashSet and TreeSet in the both Example?

View Answers

June 22, 2012 at 6:11 PM

import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

 public class SetsExample{
 public static void main(String args[]){
     Set<String> set1 = new HashSet<String>();
     set1.add("A"); //A is a duplicate value
     System.out.println("Set is " + set1);
     System.out.println("Set1 after removing c is " + set1);
     int size=set1.size();
     System.out.println("Size of set1 after adding duplicate item is " + size);
     System.out.println("Is set1 contains a " + set1);
     System.out.println("Is set1 contains c " + set1.contains("C"));
     Set<String> set2 = new TreeSet<String>();
     System.out.println("Set2 is " + set2);
     System.out.println("Set2 is after mergine set1 element " + set2);
     System.out.println("Set is after deleting set1 element " + set2);
     System.out.println("Set2 is after deleting all element except set1 element " + set2);



Set is [A, B, C]
Set1 after removing c is [A, B]
Size of set1 after adding duplicate item is 2
Is set1 contains a [A, B]
Is set1 contains c false
Set2 is [E, F, G]
Set2 is after mergine set1 element [A, B, E, F, G]
Set is after deleting set1 element [E, F, G]
Set2 is after deleting all element except set1 element [A, B]

Description:- The above example demonstrates you the Set interface. Since Set is an interface, you need to instantiate a concrete implementation of the interface in order to use it. Here Set is implemented by its two subclasses HashSet and TreeSet. Now using the add() method, we have added elements to HashSet. Set does not accept duplicate values. So here we have checked whether the added elements are duplicate or not. Then we have removed one Element from the HashSet by using the method remove(). Now we have created another object by implementing Set-using TreeSet and add elements to the tree set. Using the addAll() method, we have added the elements of set1 to set2 and using the method removeAll(), we have removed all elements from set2.


Related Tutorials/Questions & Answers:
Java collection HashSet and TreeSet
Java collection HashSet and TreeSet   How can we used HashSet and TreeSet in the both Example?   import java.util.HashSet; import... two subclasses HashSet and TreeSet. Now using the add() method, we have added
Java Collection-TreeSet
Java Collection-TreeSet  What is TreeSet in Java Collection?  ...){ TreeSet ts = new TreeSet(); ts.add("B"); ts.add("D...:- The above program demonstrates you the concept of TreeSet. Here we have created
Java collection HashSet
Java collection HashSet  How can we use HashSet in java program?   The hashSet class is used to create a collection and store it in a hash table. Each collection refer to a unique value. import java.util.Collections
Java HashSet Collection problem
Java HashSet Collection problem  Here's how the HashSet works... the concept behind this??   In your code, HashSet stores the values...;Prac>(); So, while adding the object of Prac using add method of hashset, it get
Java Collection : TreeSet Example
Java Collection : TreeSet Example This tutorial contains description of  TreeSet with example. TreeSet  : TreeSet class is defined in java.util... constructor - TreeSet(), TreeSet(Collection c), TreeSet(Comparator c
program for HashSet - Java Beginners
of HashSet. can u please suggest me a way out.  Hi friend, A set is a collection interface. It is connot contain any dublicate element. The Set and SortedSet intefaces describe the properties of sets and the HashSet
What is HashSet in java
In following example we will discuss about HashSet in Java. The HashSet class... add(), remove(),size() and test the element of the collection. The HashSet...( ) HashSet(Collection x) HashSet(int capacity) The HashSet() constructor
Example of HashSet class in java
Example of HashSet class in java. In this part of tutorial, we will introduce you to about the HashSet class.  It is collection. It stores only unique. You can not store duplicate value. Java hashset example. How
Java HashSet  - Java Tutorials
Java Collections Framework- HashSet Class A HashSet is a collection set...;  HashSet(Collection c) HashSet(int capacity) HashSet(int capacity, float... a collection that uses a hash table for storage. Hash table stores information by using
Java hashset example.
Java hashset example.     HashSet is a collection. You can not store duplicate value in HashSet. In this java hashset exmple, you will see how to create HashSet in java application and how to store value in Hashset
Java TreeSet
Java TreeSet       The Collection Framework provides two general-purpose implementations of the set... elements from a collection in a sorted manner.  TreeSet is not synchronized
HASHSET - Java Interview Questions
HASHSET  How To Sort HashSet?I Want Source Code?   Hi... HashSetExample{ public static void main (String args[]){ Set set = new HashSet..."); set.add("Simran"); Set sortedSet= new TreeSet(set); System.out.println(sortedSet
TreeSet We will discus about treeSet() method. The treeSet implement the Set interface. we have stored collection of data and data order of element. We...(), comparator(),Object toElement), tailSet(Object fromElement). TreeSet method
Collection : HashSet Example
Collection : HashSet Example This tutorial contains description of HashSet with example. HashSet  : HashSet class extends AbstractSet class.... Example : Here is HashSet() example - package collection; import
Example of Hashset size() method in java.
Example of Hashset size() method in java. The size() is a method of HashSet class. It is used to calculte the size of HashSet. The size of method is equal to total number of elements in HashSet Code: 
Example of contains method of hashset in java.
Example of contains method of hashset in java. The contains() is a method of hashset. It is used for checking that the given number is available in hashset or not. It returns only boolean value.If number is present in hashset
HashMap/HashSet - Java Beginners
HashMap/HashSet  im working on a game, and i want to know how does the code for HashMap and HashSet work or can you give me the code that needs to be included in the game engine. I have the exam on monday. Can you please help
Example of toArray() method of hashset in java.
Example of toArray() method of hashset in java. The toArray() is a method of HashSet in java. It is used to create array of object. It is also used to copy all element of hashset into array of object. Code: 
Example of Hashset iterator method in java.
Example of Hashset iterator method in java. In this exmple of HashSet class... element from HashSet. Code: package... RemoveAllElement { public static void main(String[] arg) { HashSet obHashSet = new
HashSet In Java
HashSet In Java In this section we will read about HashSet in Java. HashSet...(Collection c); HashSet hs2 = new HashSet(int intialCapacity); HashSet hs3 = new...(); HashSet hs1 = new HashSet(Arrays.asList(?Java?,?C++?)); // HashSet from Array
java collection
java collection  how do I use a treeset to print unique words
Java Collection
Java Collection  What is the Collection interface
HashSet Example
the methods to add, remove and iterate the values of collection. A HashSet... are performing various operations on HashSet collection. We have explained the steps to add...; To insert an element in the HashSet collection add() method is used. The size
Java collection
Java collection  What are differences between Enumeration, ArrayList, Hashtable and Collections and Collection
Collection in java
Collection in java  What are the meaning of the letters E,T,K and V that come in the collection concept
Java collection LinkedHashSet
Java collection LinkedHashSet  How can we use LinkedHashSet in java collection?   import java.util.Iterator; import...("HashSet"); ht.add('R'); ht.add("List"); ht.remove
Java collection
Java collection  What is relation between map and set
Java Collection
Java Collection  What are Vector, Hashtable, LinkedList and Enumeration
Java collection
Java collection  What are differences between Enumeration, ArrayList, Hashtable and Collections
java collection - Development process
java collection - creating and comparing lists using Java  How to compare two list in java? and i have to create two more list from old two list. Comparing and Creating new list in Java Java Example code for creating
Java Collection API - Java Tutorials
Java Collection API Collection was added to Java with J2SE 1.2 release. Collection framework is provided in 'java.util.package'. All collections... of the collection interfaces. Also, they are reusable data structures. Algorithms
Java Spring Hibernate Struts Training What type of programming language is Java? How do beginners learn about Java? ClassNotFoundException HttpRequestInterceptor java.lang.noclassdeffounderror: org/apache/http/httprequest noclassdeffounderror: org/apache/http/client/methods/httpurirequest java.lang.NoClassDefFoundError: org/apache/http/client/HttpClient How do I resolve this Java Class not found exception? httpclient java.lang.NoClassDefFoundError Apache Commons ioutils maven dependency Read/Convert an inputStream to a String What is the meaning of Java Platform? Why Java is a platform independent language? What is the benefits of learning Core Java? Which technology should I learn after Java? What is array in java with example? How to Convert ArrayList to Array? How to substring in Java? How to format number in Java? What is instance variable in Java? How to download MySQL JDBC driver? What is Calendar class in Java? Which is the best Java tutorials for beginners? How to rename a file in Java? How to delete file in Java code? How to get day from date in Java using Calendar? How to get day of week in Java? How to calculate Date Difference in Java? How to compare date in Java? How to declare array in Java? How to calculate average of array in Java? What is Array in Java? write a java program to find the summation of all the integers entered on command line Sum of two numbers using command line arguments in Java How to create and use Array in Java? How to pass command line arguments in Java? How to create Applet Hello World? Appending String efficiently in Java How to append String in Java? How to list even numbers between 1 and 100? How to add BigDecimal in Java? What is Abstraction In Java? Which is best Beginners Java Tutorial? What is java.util package? Create list from array in Java Filter collection in Java 8 What is the best way to filter a Java Collection? Easy way to transform Collection to Array? How to convert Collection to Array in Java? What are Basic Java Language Elements? Advanced Java Tutorials in 2017


Advertisement null