25.02.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 />

LinkedHashSet hs = new LinkedHashSet();<br />

// add elements to the hash set<br />

hs.add("B");<br />

hs.add("A");<br />

hs.add("D");<br />

hs.add("E");<br />

hs.add("C");<br />

hs.add("F");<br />

System.out.println(hs);<br />

} }<br />

This will produce the following result:<br />

[B, A, D, E, C<br />

The TreeSet Class<br />

TreeSet provides an implementation of the Set interface that uses a tree for storage.<br />

Objects are stored in a sorted and ascending order.<br />

Access and retrieval times are quite fast, which makes TreeSet an excellent choice when<br />

storing large amounts of sorted information that must be found quickly.<br />

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

Sr.<br />

No.<br />

Constructors with Description<br />

1<br />

2<br />

TreeSet( )<br />

This constructor constructs an empty tree set that will be sorted in an ascending<br />

order according to the natural order of its elements.<br />

TreeSet(Collection c)<br />

This constructor builds a tree set that contains the elements of the collection c.<br />

TreeSet(Comparator comp)<br />

3<br />

This constructor constructs an empty tree set that will be sorted according to<br />

the given comparator.<br />

403

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

Saved successfully!

Ooh no, something went wrong!