关于java:我可以让某人验证我的SCJP考试的馆藏

Can I have someone verify my collections for the SCJP Exam

我一直在学习SCJP,现在是Oracle认证的专业JavaSE程序员考试。

我很难把自己的脑袋集中在所有不同的收藏品上,以及何时使用它们。我也喜欢闪存卡。所以我尝试创建一组基本上相同的类,除了它们使用的集合。我将必须确定输出将如何出来,以及每个集合的主要"特性"是什么。

不幸的是,我不相信自己。我想请人确认所有的信息都是准确的,如果有丢失的话。然后经过一些反馈/更正,我认为这将是一个伟大的练习,任何其他人试图了解Java集合。

所涵盖的集合包括:hashmap、hashtable、treemap、linkedhashmap、hashset、treeset、linkedhashset、arraylist、vector、linkedlist、priorityqueue。

我也把所有的文件分开,可以在这里下载:http://www.allgo.com/personal/mycollections.zip

提前谢谢

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
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
##
"
);
        MyHashtable.main(args);         System.out.println("Hashtable: Thread Safe. Hash=Unsorted, Unordered. Map=key/value pair
##
"
);
        MyTreeMap.main(args);           System.out.println("TreeMap: Tree=sorted. Map=key/value.
##
"
);
        MyLinkedHashMap.main(args);     System.out.println("LinkedHashMap: Linked=Maintains Insertion Order. Hash=unsorted, unordered. Map=key/value pair.
##
"
);
        MyHashSet.main(args);           System.out.println("HashSet: Hash=Unsorted, Unordered. Set=Unique. Define=equals/hashCode
##
"
);
        MyTreeSet.main(args);           System.out.println("TreeSet: Tree=Sorted. Set=Unique. Define=Comparable/Comparator
##
"
);
        MyLinkedHashSet.main(args);     System.out.println("LinkedHashSet: Liniked=Maintains Insertion Order. Hash=Unsorted. Set=Unique. Define=equals/hashCode
##
"
);
        MyArrayList.main(args);         System.out.println("ArrayList: List=Queue. Maintains insertion order, Allowed duplicates
##
"
);
        MyVector.main(args);            System.out.println("Vector: Thread Safe. ArrayList. Maintains Insertion Order, Allows duplicates
##
"
);
        MyLinkedList.main(args);        System.out.println("LinkedList: Linked=Maintaines Insertion Order. List=Queue. Advanced ArrayList with more methods.
##
"
);
        MyPriorityQueue.main(args);     System.out.println("PriorityQueue: Define=Comparable/comparator
##
"
);
    }
}
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?
    }
}

首先,您应该重构代码。基本上,无论你在哪里使用"复制粘贴",都不要。

创建如下方法:

1
2
3
4
5
6
7
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?
}

然后,不要使用现有的方法,而是执行以下操作:

1
2
3
4
5
6
class MyVector{
    public static void main(String[] args){
        Vector c = new Vector();
        fill(c);
    }
}

对你所有的收藏品都这样做。

接下来,对地图执行类似的操作:

1
2
3
4
5
6
7
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?
}

您的代码将收缩,可读,甚至有一天会变得可用。