- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在准备 SCJP,现在是 Oracle 认证专业 Java SE 程序员考试。
我很难理解所有不同的集合以及何时使用它们。我也喜欢闪存卡。因此,我尝试创建一组本质上相同的类,除了它们使用的集合之外。我必须确定输出的结果以及每个集合的主要“特征”是什么。
不幸的是我不相信自己。我希望有人确认所有信息是否准确或是否有遗漏。经过一些反馈/更正后,我认为这对于其他试图理解 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? }}
最佳答案
对于初学者,您应该 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);
}
}
并对您拥有的所有集合执行此操作。
接下来,对您的 map 执行类似的操作:
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?
}
您的代码将会缩小、变得可读,甚至可能有一天变得可用。
关于java - 我可以请人验证我的 SCJP 考试 Collection 吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6907249/
我之前遇到的关于 SO 的问题已经将近 2 年了。 我刚刚为 SCJP 6 购买了 Kathy Sierra 和 Bert Bates 的书。但它发生了,所以现在在我阅读博客时,我听说 SCJP“PL
嗨,我无法理解为什么输出是 'ex [6, 7, b, d] [6, 7, b]' 这段代码。请有人建议子集如何与 数字和字母?谢谢 import java.util.*; public class
在调试java多线程程序时,我设置了断点。在启动方法调用后,控件将不会运行menthod,您可以让我知道调试过程吗? 示例代码 class Test extends Thread { publ
public class Hotel { private int roomNr; public Hotel(int roomNr) { this.roomNr = ro
下面将因“标签 z 丢失”而编译失败,但如果我只是将 z: 移动到 o = o + 2 之后的下面一步,那么这会起作用吗?这背后的逻辑是什么? public class Breaker { stati
考虑: class Building { Building() { System.out.print("b "); } Building(String name
public class Starter extends Thread{ private int x = 2; public static void main(String[]
给出下面的代码。该代码由“java Test 1 2 3 4”运行。结果是什么? public class Test { public static void main(String args[])
另一个让我困惑的考试问题: public String makinStrings() { String s = “Fred”; s = s + “47”; s = s.substring(2,
我在理解 K&B 的 SCJP 书中《面向对象》一章中的问题 9 时遇到问题。 问题: public class Redwood extends Tree { public static void m
即使得到正确答案,我也无法弄清楚 SCJP 问题: 从以下代码(来源:http://scjptest.com)中,我们需要确定何时引用为 myInt 的对象符合垃圾回收条件: 01.public vo
给定: public class LineUp { public static void main(String[] args) { double d = 12.345;
只是想知道 SUN 是否提供任何可用于 SCJP 的好东西(免费或付费)??我听说微软正在提供这样的好东西。有人对此有任何想法吗? 干杯,对战 最佳答案 你可以把它写在你的简历上。大部分就是这样。 肯
这个问题来自 SCJP 转储。可能看起来很愚蠢,但我对选项有点困惑。请帮帮我 public class Donkey2 { public static void main(String[] ar
public class Person { private String name; public Person(String name) { this.name =
实际上来自SCJP 6 StudyGuide Exam310-065(MGH,2008) alt text http://www.freeimagehosting.net/uploads/1f3746
我无法理解 SCJP 书籍 K&B 第 9 章(线程)中的以下程序 问题: class Dudes{ static long flag = 0; // insert code her
Kathey Sierra 的 SCJP 书中摘录如下: If a method is overridden but you use a polymorphic (supertype) referen
public class Threads2 implements Runnable { public void run() { System.out.println("run.
这个问题是由考试实验室操纵的 public class B{ public static void main(String args[]){ byte g =10;
我是一名优秀的程序员,十分优秀!