
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Variable Types
- Java - Basic Datatypes
- Java - Basic Operators
- Java Control Statements
- Java - Loop Control
- Java - Decision Making
- Java - If-else
- Java - Switch
- Java - For Loops
- Java - For-Each Loops
- Java - While Loops
- Java - do-while Loops
- Java - Break
- Java - Continue
- Object Oriented Programming
- Java - Object & Classes
- Java - Methods
- Java - Constructors
- Java - Access Modifiers
- Java - Inheritance
- Java - Polymorphism
- Java - Overriding
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java - Inner classes
- Java Data Types
- Java - Characters
- Java File Handling
- Java - Files and I/O
- Java Error & Exceptions
- Java - Exceptions
- Java Multithreading
- Java - Multithreading
- Java Synchronization
- Java - Synchronization
- Java - Inter-thread Communication
- Java - Thread Deadlock
- Java - Thread Control
- Java Networking
- Java - Networking
- Java - URL Processing
- Java - Generics
- Java Collections
- Java - Collections
- Java List Interface
- Java - List Interface
- Java Queue Interface
- Java - Queue Interface
- Java Map Interface
- Java - Map Interface
- Java - SortedMap Interface
- Java Set Interface
- Java - Set Interface
- Java - SortedSet Interface
- Java Data Structures
- Java - Data Structures
- Java - Enumeration
- Java Collections Algorithms
- Java - Collections
- Java - Iterators
- Java - Comparators
- Java Miscellenous
- Java - Regular Expressions
- Java - Serialization
- Java - Sending Email
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java - The LinkedHashSet Class
This class extends HashSet, but adds no members of its own.
LinkedHashSet maintains a linked list of the entries in the set, in the order in which they were inserted. This allows insertion-order iteration over the set.
That is, when cycling through a LinkedHashSet using an iterator, the elements will be returned in the order in which they were inserted.
The hash code is then used as the index at which the data associated with the key is stored. The transformation of the key into its hash code is performed automatically.
Following is the list of constructors supported by the LinkedHashSet.
Sr.No. | Constructor & Description |
---|---|
1 | HashSet( ) This constructor constructs a default HashSet. |
2 | HashSet(Collection c) This constructor initializes the hash set by using the elements of the collection c. |
3 | LinkedHashSet(int capacity) This constructor initializes the capacity of the linkedhashset to the given integer value capacity. The capacity grows automatically as elements are added to the HashSet. |
4 | LinkedHashSet(int capacity, float fillRatio) This constructor initializes both the capacity and the fill ratio (also called load capacity) of the hash set from its arguments. |
Example
The following program illustrates several of the methods supported by LinkedHashSet −
import java.util.*; public class HashSetDemo { public static void main(String args[]) { // create a hash set LinkedHashSet hs = new LinkedHashSet(); // 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); } }
This will produce the following result −
Output
[B, A, D, E, C, F]