High Fashioners Issue # 2 | Page 234

J. E. N. I.
HashSet merupakan sebuah implementasi dari Set interface yang berguna pada hash table. Penggunaan suatu hash table lebih mudah dan cepat untuk melihat lebih detail elemen-elemen yang ada. Table menggunakan suatu rumusan untuk menentukan dimana suatu objek disimpan. Teliti program ini, yang menggunakan class HashSet.
import java. util.*;
class HashSetDemo { public static void main( String args []) { HashSet hs = new HashSet( 5, 0.5f); System. out. println( hs. add(" one ")); System. out. println( hs. add(" two ")); System. out. println( hs. add(" one ")); System. out. println( hs. add(" three ")); System. out. println( hs. add(" four ")); System. out. println( hs. add(" five ")); System. out. println( hs);
}
}
TreeSet merupakan sebuah implementasi dari Set interface yang menggunakan tree. Class ini memastikan bahwa yang disortir akan diurutkan secara ascending. Pertimbangkan, bagaimana class TreeSet telah digunakan dalam listing program berikut ini. import java. util.*;
class TreeSetDemo { public static void main( String args []) { TreeSet ts = new TreeSet(); ts. add(" one "); ts. add(" two "); ts. add(" three "); ts. add(" four "); System. out. println( ts);
}
}
Gambar 1.2.8: Contoh TreeSet
Pengenalan Pemrograman 2 13