2011-08-02 6 views
2

私はSCJP、現在Oracle Certified Professional Java SE Programmer試験のために勉強しています。SCJP試験のコレクションを誰かに確認させてもらうことはできますか

私は、さまざまなコレクションの周りに自分の頭を包んで、いつそれらを使うのが難しい時がありました。私もフラッシュカードが好きです。だから私は、彼らが使っているコレクション以外は本質的に同じクラスのセットを作ろうとしました。出力がどのように出てくるのか、各コレクションの主要な「機能」が何であるかを特定する必要があります。

残念ながら私は自分自身を信用しません。誰かにすべての情報が正確であるかどうか、または不足していることを確認してもらいたい。その後、いくつかのフィードバック/修正の後で、私はそれがJavaコレクションを理解しようとしている誰にとっても偉大な運動をするだろうと思っています。

対象コレクションは、 HashMap、Hashtable、TreeMap、LinkedHashMap、HashSet、TreeSet、LinkedHashSet、ArrayList、Vector、LinkedList、PriorityQueueです。

私もすべてのファイルが分離している、彼らはここからダウンロードすることができます。事前にhttp://www.allgo.com/personal/MyCollections.zip

おかげ手始めに

 
import java.util.*; 
import java.lang.*; 
class MyItem implements Comparable{ 
    private String name; 
    MyItem(String n){ name = n; } 
    public String toString(){return name;} 
    public String getName(){return name;} 

    public boolean equals(Object obj){ 
     if(this==obj) return true; 
     else if(obj==null) return false; 
     else if(getName() != ((MyItem)obj).getName()) return false; 
     else return true; 
    } 
    public int hashCode(){ return 5; } 
    public int compareTo(MyItem b){return b.getName().compareTo(getName());} 

} 
public class MyCollections{ 
    public static void main(String[] args){ 
     MyHashMap.main(args);   System.out.println("HashMap: Hash=Unsorted, Unordered. Map=key/value pair\n##\n"); 
     MyHashtable.main(args);   System.out.println("Hashtable: Thread Safe. Hash=Unsorted, Unordered. Map=key/value pair\n##\n"); 
     MyTreeMap.main(args);   System.out.println("TreeMap: Tree=sorted. Map=key/value.\n##\n"); 
     MyLinkedHashMap.main(args);  System.out.println("LinkedHashMap: Linked=Maintains Insertion Order. Hash=unsorted, unordered. Map=key/value pair.\n##\n"); 
     MyHashSet.main(args);   System.out.println("HashSet: Hash=Unsorted, Unordered. Set=Unique. Define=equals/hashCode\n##\n"); 
     MyTreeSet.main(args);   System.out.println("TreeSet: Tree=Sorted. Set=Unique. Define=Comparable/Comparator\n##\n"); 
     MyLinkedHashSet.main(args);  System.out.println("LinkedHashSet: Liniked=Maintains Insertion Order. Hash=Unsorted. Set=Unique. Define=equals/hashCode\n##\n"); 
     MyArrayList.main(args);   System.out.println("ArrayList: List=Queue. Maintains insertion order, Allowed duplicates\n##\n"); 
     MyVector.main(args);   System.out.println("Vector: Thread Safe. ArrayList. Maintains Insertion Order, Allows duplicates\n##\n"); 
     MyLinkedList.main(args);  System.out.println("LinkedList: Linked=Maintaines Insertion Order. List=Queue. Advanced ArrayList with more methods.\n##\n"); 
     MyPriorityQueue.main(args);  System.out.println("PriorityQueue: Define=Comparable/comparator\n##\n"); 
    } 
} 
class 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, Eight); c.put(3, new MyItem("Three")); 
     c.put(4, new MyItem("Four")); c.put(1, new MyItem("1")); c.put(8, Eight); c.put(9, new MyItem("Nine")); 
     c.remove(3); c.put(7, new MyItem("Seven")); 
     System.out.println(c);//output? 
    } 
} 
class 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, Eight); c.put(3, new MyItem("Three")); 
     c.put(4, new MyItem("Four")); c.put(1, new MyItem("1")); c.put(8, Eight); c.put(9, new MyItem("Nine")); 
     c.remove(3); c.put(7, new MyItem("Seven")); 
     System.out.println(c);//output? 
    } 
} 
class MyTreeMap{ 
    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, Eight); c.put(3, new MyItem("Three")); 
     c.put(4, new MyItem("Four")); c.put(1, new MyItem("1")); c.put(8, Eight); c.put(9, new MyItem("Nine")); 
     c.remove(3); c.put(7, new MyItem("Seven")); 
     System.out.println(c);//output? 
    } 
} 
class MyLinkedHashMap{ 
    public static void main(String[] args){ 
     LinkedHashMap c = new LinkedHashMap(); 
     MyItem Eight = new MyItem("Eight"); 
     c.put(5, new MyItem("Five")); c.put(1, new MyItem("One")); c.put(8, Eight); c.put(3, new MyItem("Three")); 
     c.put(4, new MyItem("Four")); c.put(1, new MyItem("1")); c.put(8, Eight); c.put(9, new MyItem("Nine")); 
     c.remove(3); c.put(7, new MyItem("Seven")); 
     System.out.println(c);//output? 
    } 
} 
class 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(Eight); c.add(new MyItem("Three")); 
     c.add(new MyItem("Four")); c.add(new MyItem("One")); c.add(Eight); c.add(new MyItem("Nine")); 
     c.remove(3); c.add(new MyItem("Seven")); 
     System.out.println(c);//output? 
    } 
} 

class MyTreeSet{ 
    public static void main(String[] args){ 
     TreeSet c = new TreeSet(); 
     MyItem Eight = new MyItem("Eight"); 
     c.add(new MyItem("Five")); c.add(new MyItem("One")); c.add(Eight); c.add(new MyItem("Three")); 
     c.add(new MyItem("Four")); c.add(new MyItem("One")); c.add(Eight); c.add(new MyItem("Nine")); 
     c.remove(Eight); c.add(new MyItem("Seven")); 
     System.out.println(c);//output? 
    } 
} 
class MyLinkedHashSet{ 
    public static void main(String[] args){ 
     LinkedHashSet c = new LinkedHashSet(); 
     MyItem Eight = new MyItem("Eight"); 
     c.add(new MyItem("Five")); c.add(new MyItem("One")); c.add(Eight); c.add(new MyItem("Three")); 
     c.add(new MyItem("Four")); c.add(new MyItem("One")); c.add(Eight); c.add(new MyItem("Nine")); 
     c.remove(3); c.add(new MyItem("Seven")); 
     System.out.println(c);//output? 
    } 
} 
class 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(Eight); c.add(new MyItem("Three")); 
     c.add(new MyItem("Four")); c.add(new MyItem("One")); c.add(Eight); c.add(new MyItem("Nine")); 
     c.remove(3); c.add(new MyItem("Seven")); 
     System.out.println(c);//output? 
    } 
} 
class MyVector{ 
    public static void main(String[] args){ 
     Vector c = new Vector(); 
     MyItem Eight = new MyItem("Eight"); 
     c.add(new MyItem("Five")); c.add(new MyItem("One")); c.add(Eight); c.add(new MyItem("Three")); 
     c.add(new MyItem("Four")); c.add(new MyItem("One")); c.add(Eight); c.add(new MyItem("Nine")); 
     c.remove(3); c.add(new MyItem("Seven")); 
     System.out.println(c);//output? 
    } 
} 
class MyLinkedList{ 
    public static void main(String[] args){ 
     LinkedList c = new LinkedList(); 
     MyItem Eight = new MyItem("Eight"); 
     c.add(new MyItem("Five")); c.add(new MyItem("One")); c.add(Eight); c.add(new MyItem("Three")); 
     c.add(new MyItem("Four")); c.add(new MyItem("One")); c.add(Eight); c.add(new MyItem("Nine")); 
     c.remove(3); c.add(new MyItem("Seven")); 
     System.out.println(c);//output? 
    } 
} 
class MyPriorityQueue{ 
    public static void main(String[] args){ 
     PriorityQueue c = new PriorityQueue(); 
     MyItem Eight = new MyItem("Eight"); 
     c.offer(new MyItem("Five")); c.offer(new MyItem("One")); c.offer(Eight); c.offer(new MyItem("Three")); 
     c.offer(new MyItem("Four")); c.offer(new MyItem("One")); c.offer(Eight); c.offer(new MyItem("Nine")); 
     System.out.println(c.peek()); 
     System.out.println(c.poll()); 
     c.offer(new MyItem("Seven")); 
     System.out.println(c);//output? 
    } 
} 

答えて

3

を、あなたはあなたのコードをrefactor必要があります。基本的に、どこでも "コピー貼り付け"を使用しましたが、そうしないでください。

このような方法で作成します。代わりにあなたが持っている方法を次に

private static void fill(Collection c) { 
    MyItem Eight = new MyItem("Eight"); 
    c.add(new MyItem("Five")); c.add(new MyItem("One")); c.add(Eight); c.add(new MyItem("Three")); 
    c.add(new MyItem("Four")); c.add(new MyItem("One")); c.add(Eight); c.add(new MyItem("Nine")); 
    c.remove(3); c.add(new MyItem("Seven")); 
    System.out.println(c);//output? 
} 

を、この実行します

class MyVector{ 
    public static void main(String[] args){ 
     Vector c = new Vector(); 
     fill(c); 
    } 
} 

をそして、あなたが持っているすべてのコレクションのためにそれを行います。

次に、あなたのマップについても同様のことを実行します。

private static void fill(Map<?,?> map) { 
    MyItem Eight = new MyItem("Eight"); 
    map.put(5, new MyItem("Five")); map.put(1, new MyItem("One")); map.put(8, Eight); map.put(3, new MyItem("Three")); 
    map.put(4, new MyItem("Four")); map.put(1, new MyItem("1")); map.put(8, Eight); map.put(9, new MyItem("Nine")); 
    map.remove(3); map.put(7, new MyItem("Seven")); 
    System.out.println(map);//output? 
} 

をあなたのコードは、縮小読み取り可能と一日でも使用可能になることがあります。

+0

ありがとう、私はリファクタリングが不可欠であることに同意します。しかし、この場合は非常に慎重です。各クラスは、コレクションが何をするかについての手がかりを与えずに、個別にフラッシュカードに表示することを意図しているためです。彼らは本当に非常に異なるので、少しトリッキーです。 –

関連する問題