Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Java collection HashSet
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Naulej kumar
Java collection HashSet
1 Answer(s)      2 years and 3 months ago
Posted in : Java Beginners


How can we use HashSet in java program?


Advertisement
View Answers

June 21, 2012 at 5:34 PM


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;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

  public class SetExample {
    public static void main(String[] args) {
        Set set = new HashSet();
        boolean[] flags = new boolean[10];
        flags[0] = set.add("Naulej");
        flags[1] = set.add("Rose");
        flags[2] = set.add("Naulej");
        flags[3] = set.add(2376326);
        flags[4] = set.add("India");
        flags[5] = set.add(new Integer(10));
        flags[6] = set.add(new Long(5));
        flags[7] = set.add('T');
        flags[8] = set.add("India");

        for (int i = 0; i < flags.length; i++) {
            boolean b = flags[i];
            System.out.println("value at " + i + " - " + b);
        }
        System.out.println(set);
    }
}

Output:-

value at 0 - true
value at 1 - true
value at 2 - false
value at 3 - true
value at 4 - true
value at 5 - true
value at 6 - true
value at 7 - true
value at 8 - false
value at 9 ?? false
[T, 5, Naulej, 10, Rose, 2376326, India]

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 subclass HashSet. Set does not accept duplicate values. So here we have check whether the added elements are duplicate or not. We have created a Boolean array and store the Set elements to it. If the elements is previously exists in Set, it will display false, otherwise it will display true.



Related Tutorials/Questions & Answers:
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
Advertisements
Java collection HashSet and TreeSet
Java collection HashSet and TreeSet   How can we used HashSet... 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
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
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
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
HASHSET - Java Interview Questions
HASHSET  How To Sort HashSet?I Want Source Code?   Hi Friend, Try the following code: import java.util.*; public class HashSetExample{ public static void main (String args[]){ Set set = new 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:  HashSetRemoveElement.java 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  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
java collection  how do I use a treeset to print unique words
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 garbage collection
Java garbage collection  What is Garbage Collection and how to call it explicitly
Collection classes in java
is the reason using java collection classes saved/stored the data/content.I don't understand, what is the idea using java collection classes in project. Or the data is stored in both database and java collection classes
Collection classes in java
is the reason using java collection classes saved/stored the data/content.I don't understand, what is the idea using java collection classes in project. Or the data is stored in both database and java collection classes
Java collection -Hashtable
Java collection -Hashtable   What is Hashtable in java collection?   Java collection -Hashtable;- The hashtable is used to store value in the form of map key with value. import java.util.Hashtable; import
Advertisements
 

 

 

DMCA.com