GMgKe586q6suSQnyqZLlGCooeWM

Pages

Search

Friday, December 17, 2010

Java Collection - Set

There are two implementation of Set: HashSet and TreeSet. TreeSet implement sortable order in values.
To initialize, use:

>> Set tsNames = new TreeSet();

To add an element, use:
>> tsNames.add("luna maya");
>> tsNames.add("aura kasih");
>> tsNames.add("lady gaga");

There's no method to get access for an element of the Set, because the elements be reordered again automatically at runtime.
If you want to remove an element, use:

>> tsNames.remove("luna maya");

To get the size, use:
>> int count = tsNames.size();

While to get it as an array, use:
>> Object obj[] = tsNames.toArray();

Finally, to iterate to all elements of the Set, use conventional method:

>> Iterator ite = tsNames.iterator();
>> while(ite.hasNext()){
>>    String value = ite.next();
>> }



Share/Bookmark

No comments:

Post a Comment