HashSet Sample program in Java
By: Kamini Printer Friendly Format
The following constructors are defined:
HashSet( )
HashSet(Collection c)
HashSet(int capacity)
HashSet(int capacity, float fillRatio)
The first form constructs a default hash set. The second form initializes the hash set by using the elements of c. The third form initializes the capacity of the hash set to capacity. The fourth form initializes both the capacity and the fill ratio (also called load capacity) of the hash set from its arguments. The fill ratio must be between 0.0 and 1.0, and it determines how full the hash set can be before it is resized upward. Specifically, when the number of elements is greater than the capacity of the hash set multiplied by its fill ratio, the hash set is expanded. For constructors that do not take a fill ratio, 0.75 is used.
HashSet does not define any additional methods beyond those
provided by its superclasses and interfaces. Importantly, note that a hash set does not guarantee the order
of its elements, because
the process of hashing doesn't usually lend itself to the
creation of sorted sets. If you need sorted storage, then another collection, such as TreeSet,
is a better choice.
Here is an example that demonstrates HashSet:
// Demonstrate HashSet.
import java.util.*;
class HashSetDemo {
public static void main(String args[]) {
// create a hash set
HashSet hs = new HashSet();
// add elements to the hash set
hs.add("B");
hs.add("A");
hs.add("D");
hs.add("E");
hs.add("C");
hs.add("F");
System.out.println(hs);
}
}
The following is the output from this program:
[F, E, D, C, B, A]
As explained, the elements are not stored in sorted order.
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Subscribe to Tutorials
Related Tutorials
Program using concept of byte long short and int in java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Count number of vowels, consonants and digits in a String in Java
Student marks calculation program in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java
Archived Comments
1. it's very usefull to the users who were in startin
View Tutorial By: Harichandana at 2013-02-28 04:37:17
2. http://www.leathernxg.com/womens-leather-bombers/5
View Tutorial By: Venkat at 2013-04-11 06:40:45
3. Good
View Tutorial By: Siva Sreekanth at 2013-10-01 16:29:55
4. Your output is not correct.. Give the correct out
View Tutorial By: Surendrakumar at 2014-06-04 07:00:16
5. CaseyOffer
View Tutorial By: CaseyOffer at 2017-03-15 18:27:37
6. CaseyOffer
View Tutorial By: CaseyOffer at 2017-04-10 22:16:46