23.11.2017 Views

java_tutorial

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Java<br />

The LinkedHashSet Class<br />

This class extends HashSet, but adds no members of its own.<br />

LinkedHashSet maintains a linked list of the entries in the set, in the order in which they<br />

were inserted. This allows insertion-order iteration over the set.<br />

That is, when cycling through a LinkedHashSet using an iterator, the elements will be<br />

returned in the order in which they were inserted.<br />

The hash code is then used as the index at which the data associated with the key is<br />

stored. The transformation of the key into its hash code is performed automatically.<br />

Following is the list of constructors supported by the LinkedHashSet.<br />

Sr.<br />

No.<br />

Constructors and Description<br />

1<br />

HashSet( )<br />

This constructor constructs a default HashSet.<br />

HashSet(Collection c)<br />

2<br />

This constructor initializes the hash set by using the elements of the collection<br />

c.<br />

LinkedHashSet(int capacity)<br />

3<br />

This constructor initializes the capacity of the linkedhashset to the given integer<br />

value capacity. The capacity grows automatically as elements are added to the<br />

HashSet.<br />

LinkedHashSet(int capacity, float fillRatio)<br />

4<br />

This constructor initializes both the capacity and the fill ratio (also called load<br />

capacity) of the hash set from its arguments.<br />

Example<br />

The following program illustrates several of the methods supported by LinkedHashSet:<br />

import <strong>java</strong>.util.*;<br />

public class HashSetDemo {<br />

public static void main(String args[]) {<br />

// create a hash set<br />

402

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!