私は SCJP (現在は Oracle Certified Professional Java SE Programmer 試験) のために勉強しています。
さまざまなコレクションすべてと、それらをいつ使用するかについて頭を悩ますのに苦労しました。フラッシュカードも好きです。そこで、使用しているコレクションを除いて本質的に同じクラスのセットを作成しようとしました。出力がどのように出力されるか、および各コレクションの主な「機能」は何かを特定する必要があります。
残念ながら、私は自分自身を信頼していません。すべての情報が正確であるか、または不足している情報があるかどうかを誰かに確認してもらいたいです。その後、いくつかのフィードバック/修正を行った後、Java コレクションを理解しようとしている他の人にとっては素晴らしい演習になると思います。
対象となるコレクションは、HashMap、Hashtable、TreeMap、LinkedHashMap、HashSet、TreeSet、LinkedHashSet、ArrayList、Vector、LinkedList、PriorityQueue です。
また、すべてのファイルを分けてあります。ここからダウンロードできます: http://www.allgo.com/personal/MyCollections.zip
前もって感謝します
import java.util.*; java.lang.* をインポートします。 class MyItem は Comparable を実装します{ プライベート文字列名; MyItem(文字列 n){ 名前 = n; } public String toString(){戻り値;} public String getName(){戻り値;} public boolean equals(Object obj){ if(this==obj) は true を返します。 それ以外の場合 (obj==null) は false を返します。 それ以外の場合 (getName() != ((MyItem)obj).getName()) は false を返します。 それ以外の場合は true を返します。 } public int hashCode(){ return 5; } public int compareTo(MyItem b){return b.getName().compareTo(getName());} } パブリッククラスMyCollections{ public static void main(String[] args){ MyHashMap.main(args); System.out.println("HashMap: Hash=Unsorted, Unordered.Map=キーと値のペア\n##\n"); MyHashtable.main(args); System.out.println("Hashtable: スレッド セーフ。Hash=Unsorted、Unordered。Map=key/value ペア\n##\n"); MyTreeMap.main(args); System.out.println("TreeMap: Tree=sorted.Map=key/value.\n##\n"); MyLinkedHashMap.main(引数); System.out.println("LinkedHashMap: Linked=挿入順序を維持。Hash=unsorted、unordered。Map=キー/値ペア。\n##\n"); MyHashSet.main(引数); System.out.println("HashSet: Hash=Unsorted, Unordered. Set=Unique. Define=equals/hashCode\n##\n"); MyTreeSet.main(引数); System.out.println("TreeSet: Tree=Sorted. Set=Unique. Define=Comparable/Comparator\n##\n"); MyLinkedHashSet.main(引数); System.out.println("LinkedHashSet: Liniked=挿入順序を維持。Hash=ソートなし。Set=一意。Define=equals/hashCode\n##\n"); MyArrayList.main(引数); System.out.println("ArrayList: List=Queue。挿入順序を維持します。重複を許可します\n##\n"); MyVector.main(引数); System.out.println("Vector: スレッド セーフ。ArrayList。挿入順序を維持し、重複を許可\n##\n"); MyLinkedList.main(引数); System.out.println("LinkedList: Linked=Maintaines Insertion Order. List=Queue. Advanced ArrayList with more methods.\n##\n"); MyPriorityQueue.main(引数); System.out.println("PriorityQueue: Define=Comparable/comparator\n##\n"); } } クラスMyHashMap{ public static void main(String[] args){ HashMap c = new HashMap(); MyItem Eight = new MyItem("Eight"); c.put(5, new MyItem("Five")); c.put(1, new MyItem("One")); c.put(8, エイト); c.put(3, new MyItem("Three")); c.put(4, new MyItem("Four")); c.put(1, new MyItem("1")); c.put(8, エイト); c.put(9, new MyItem("Nine")); c.remove(3); c.put(7, new MyItem("セブン")); System.out.println(c);//出力? } } クラス MyHashtable{ public static void main(String[] args){ Hashtable c = new Hashtable(); MyItem Eight = new MyItem("Eight"); c.put(5, new MyItem("Five")); c.put(1, new MyItem("One")); c.put(8, エイト); c.put(3, new MyItem("Three")); c.put(4, new MyItem("Four")); c.put(1, new MyItem("1")); c.put(8, エイト); c.put(9, new MyItem("Nine")); c.remove(3); c.put(7, new MyItem("セブン")); System.out.println(c);//出力? } } クラスマイツリーマップ{ public static void main(String[] args){ TreeMap c = new TreeMap(); MyItem Eight = new MyItem("Eight"); c.put(5, new MyItem("Five")); c.put(1, new MyItem("One")); c.put(8, エイト); c.put(3, new MyItem("Three")); c.put(4, new MyItem("Four")); c.put(1, new MyItem("1")); c.put(8, エイト); c.put(9, new MyItem("Nine")); c.remove(3); c.put(7, new MyItem("セブン")); System.out.println(c);//出力? } } クラス MyLinkedHashMap{ public static void main(String[] args){ LinkedHashMap c = 新しい LinkedHashMap(); MyItem Eight = new MyItem("Eight"); c.put(5, new MyItem("Five")); c.put(1, new MyItem("One")); c.put(8, エイト); c.put(3, new MyItem("Three")); c.put(4, new MyItem("Four")); c.put(1, new MyItem("1")); c.put(8, エイト); c.put(9, new MyItem("Nine")); c.remove(3); c.put(7, new MyItem("セブン")); System.out.println(c);//出力? } } クラス MyHashSet{ public static void main(String[] args){ HashSet c = new HashSet(); MyItem Eight = new MyItem("Eight"); c.add(new MyItem("Five")); c.add(new MyItem("One")); c.add(エイト); c.add(new MyItem("Three")); c.add(new MyItem("Four")); c.add(new MyItem("One")); c.add(エイト); c.add(new MyItem("Nine")); c.remove(3); c.add(new MyItem("セブン")); System.out.println(c);//出力? } } クラスMyTreeSet{ public static void main(String[] args){ TreeSet c = 新しい TreeSet(); MyItem Eight = new MyItem("Eight"); c.add(new MyItem("Five")); c.add(new MyItem("One")); c.add(エイト); c.add(new MyItem("Three")); c.add(new MyItem("Four")); c.add(new MyItem("One")); c.add(エイト); c.add(new MyItem("Nine")); c.remove(エイト); c.add(new MyItem("セブン")); System.out.println(c);//出力? } } クラス MyLinkedHashSet{ public static void main(String[] args){ LinkedHashSet c = 新しい LinkedHashSet(); MyItem Eight = new MyItem("Eight"); c.add(new MyItem("Five")); c.add(new MyItem("One")); c.add(エイト); c.add(new MyItem("Three")); c.add(new MyItem("Four")); c.add(new MyItem("One")); c.add(エイト); c.add(new MyItem("Nine")); c.remove(3); c.add(new MyItem("セブン")); System.out.println(c);//出力? } } クラス MyArrayList{ public static void main(String[] args){ ArrayList c = new ArrayList(); MyItem Eight = new MyItem("Eight"); c.add(new MyItem("Five")); c.add(new MyItem("One")); c.add(エイト); c.add(new MyItem("Three")); c.add(new MyItem("Four")); c.add(new MyItem("One")); c.add(エイト); c.add(new MyItem("Nine")); c.remove(3); c.add(new MyItem("セブン")); System.out.println(c);//出力? } } クラスMyVector{ public static void main(String[] args){ ベクトル c = new Vector(); MyItem Eight = new MyItem("Eight"); c.add(new MyItem("Five")); c.add(new MyItem("One")); c.add(エイト); c.add(new MyItem("Three")); c.add(new MyItem("Four")); c.add(new MyItem("One")); c.add(エイト); c.add(new MyItem("Nine")); c.remove(3); c.add(new MyItem("セブン")); System.out.println(c);//出力? } } クラス MyLinkedList{ public static void main(String[] args){ LinkedList c = 新しい LinkedList(); MyItem Eight = new MyItem("Eight"); c.add(new MyItem("Five")); c.add(new MyItem("One")); c.add(エイト); c.add(new MyItem("Three")); c.add(new MyItem("Four")); c.add(new MyItem("One")); c.add(エイト); c.add(new MyItem("Nine")); c.remove(3); c.add(new MyItem("セブン")); System.out.println(c);//出力? } } クラス MyPriorityQueue{ public static void main(String[] args){ PriorityQueue c = 新しい PriorityQueue(); MyItem Eight = new MyItem("Eight"); c.offer(new MyItem("Five")); c.offer(new MyItem("One")); c.offer(エイト); c.offer(new MyItem("Three")); c.offer(new MyItem("Four")); c.offer(new MyItem("One")); c.offer(エイト); c.offer(new MyItem("Nine")); System.out.println(c.peek()); System.out.println(c.poll()); c.offer(new MyItem("セブン")); System.out.println(c);//出力? } }