One Cup Black Coffee

Search Tutorial

Result Tutorial

Jumat, 13 Juni 2008

Sample Of Hash ( One of Type Collection In Java )

A Set is a Collection that contains unique elements (i.e., no duplicate elements). 
The collections framework contains several Set implementations,
including HashSet and TreeSet. HashSet stores its elements in a hash table, and TReeSet stores its elements in a tree.
U can uses a HashSet to remove duplicate strings from a List. Recall that both List and Collection are generic types,
so creates a List that contains String objects, and passes a Collection of Strings to method printNonDuplicates.

import
java.util.List;
import
java.util.Arrays;
import
java.util.HashSet;
import
java.util.Set;
import
java.util.Collection;
public class SetTest   {
private static final String colors[] = { "red", "white", "blue",
"green", "gray", "orange", "tan", "white", "cyan",
"peach"
, "gray", "orange" };
// create and output ArrayList
public SetTest() {
List list = Arrays.asList( colors );
System.out.printf( "ArrayList: %s\n", list );
printNonDuplicates( list );
}
    private void printNonDuplicates( Collection collection ){
// create a HashSet
Set set = new HashSet( collection );
System.out.println( "\nNonduplicates are: " );
        // Model Out Print From Java 6               
for
( String s : set )
System.out.printf( "%s ", s );
System.out.println();
}
    public static void main( String args[] )     {
new SetTest();
}
}

Result Is :

ArrayList: [red, white, blue, green, gray, orange, tan, white, cyan, peach, gray, orange]
Nonduplicates are:
red cyan white tan gray green orange blue peach

3 komentar:

Anonim mengatakan...

I think you wrote wrong title, you tried to explain about array or collection not a hash. :)
Here is sample of hash :

public class SetTest {
private static final String capitals[] = {"indonesia"=>"jakarta", "australia"=>"sydney", "japan" => "tokyo", "uk"=>"london"};

--------------------------
Here is in ruby languange

@capitals = {"indonesia"=>"jakarta", "australia"=>"sydney", "japan" => "tokyo", "uk"=>"london"}

print "#{@capitals['uk']}"
#result "london"


Reinhart
http://teapoci.blogspot.com

Anonim mengatakan...

klo kode kamu diatas versi rubynya kaya gini :


class IsengDoank

@warna = ["merah", "kuning", "hijau", "biru", "hijau", "merah", "coklat"]

print @warna.uniq.to_sentence

---------------
Hasil :
merah, kuning, hijau, biru and coklat
---------------

klo di sort pake :
@warna.sort.uniq.to_sentence

---------------
coklat, biru, hijau, kuning and merah
---------------

cuma mo tukar pikiran aja :D

Donnylie mengatakan...

Thanks Reinhat for sharing materi in my blog.. I just Show Sample About function of hash.. Which hash is Type of Collection data.. Just it..

Okay For any questions, please don't hesitate to ask me.

Best Regards,

~ Donny Lie ~

donnylie-coffee.blogspot.com