这次我要写的是有关于JAVA的集合框架。首先要阐述一下什么是集合框架,我引用的是百度百科的资料。集合框架是为表示和操作集合而规定的一种统一的标准的体系结构
用我自己的话来说,集合框架就是一类数据的容器,可以存取一类数据。
以下是关于集合框架的简化图
根据这张简化图,我今天分别从Collection实现的接口Set,List和Map中选择继承类,实现代码来展示给大家看。
因为集合框架是一类数据的容器,所以,我首先要来通过编写代码来定义一类数据,以下2个类是实现这一目的代码。
父类(Shape类)
public int x1, y1, x2, y2;
public Color color;
public float stroke;
public Shape(int x1, int y1, int x2, int y2, Color color, float stroke) {
super();
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.color = color;
this.stroke = stroke;
}
@Override
public String toString() {
return "Shape [x1=" + x1 + ", y1=" + y1 + ", x2=" + x2 + ", y2=" + y2
+ ", color=" + color + ", stroke=" + stroke + "]";
}
public abstract void Shape(Graphics2D g);
子类(LineShape)
public LineShape(int x1, int y1, int x2, int y2, Color color, float stroke) {
super(x1, y1, x2, y2, color, stroke);
// TODO Auto-generated constructor stub
}
@Override
public void Shape(Graphics2D g) {
// TODO Auto-generated method stub
g.setColor(color);
g.setStroke(new BasicStroke(stroke));
g.drawLine(x1, y1, x2, y2);
}
第一个部分(List<>接口)
1.1ArrayList
基于数组:优点查询,插入麻烦
ArrayList<Shape> list = null;
public void genData() {
list = new ArrayList<Shape>();
LineShape s1 = new LineShape(1, 0, 0, 0, null, 0);
list.add(s1);
list.add(s1);
list.add(s1);
list.add(new LineShape(2, 0, 0, 0, null, 0));
list.add(new LineShape(3, 0, 0, 0, null, 0));
list.add(new LineShape(4, 0, 0, 0, null, 0));
}
public void printData() {
for (int i = 0; i < 6; i++) {
Shape shape = list.get(i);
System.out.println(shape);
}
}
public TestArrayList() {
genData();
printData();
}
public static void main(String[] args) {
new TestArrayList();
}
运行结果
总结:
从代码和运行结果可以看出,1是有序的 2是可重复的
List接口还有2个实现类LinkList,Vector,但这两个的用法和上面的ArrayList很相似,所以我出于避免使我的博客看起来很重复,就不写代码了。
我归纳了LinkList,Vector和ArrayList的特点,以下是我的总结。
1.java.util.List 接口: 1.有序的(谁先加,谁先遍历)2.可以重复
1.java.util.ArrayList:基于数组:优点查询,插入麻烦
方法:add get size set contains
2.java.util.LinkedList:基于:链表:插入容易,访问麻烦
3.java.util.Vector:基于:基于数组
ArrayList和Vector 都是基于数组,Vector是基于线程安全,付出了一定性能代价
第二部分(Set接口)
2.1 HashSet
HashSet<Shape> hashSet = null;
public void genData() {
hashSet = new HashSet<Shape>();
LineShape s1 = new LineShape(1, 0, 0, 0, null, 0);
hashSet.add(s1);
hashSet.add(s1);
hashSet.add(s1);
hashSet.add(new LineShape(2, 0, 0, 0, null, 0));
hashSet.add(new LineShape(3, 0, 0, 0, null, 0));
hashSet.add(new LineShape(4, 0, 0, 0, null, 0));
}
public void printData() {
Iterator<Shape> iterator = hashSet.iterator();
while (iterator.hasNext()) {
Shape shape = iterator.next();
System.out.println(shape);
}
}
public static void main(String[] args) {
TestHashSet t = new TestHashSet();
t.genData();
t.printData();
}
截图
根据代码,和运行结果,可以分析出HashSet有以下2个特点
1.无序性 2.不可重复性
2.2 LinkedHashSet
代码如下
LinkedHashSet<Shape> linked = null;
public void genData() {
linked = new LinkedHashSet<Shape>();
linked.add(new LineShape(1, 0, 0, 0, null, 0));
linked.add(new LineShape(2, 0, 0, 0, null, 0));
linked.add(new LineShape(3, 0, 0, 0, null, 0));
linked.add(new LineShape(4, 0, 0, 0, null, 0));
}
public void printData() {
Iterator<Shape> iterator = linked.iterator();
while (iterator.hasNext()) {
Shape shape = iterator.next();
System.out.println(shape);
}
}
public static void main(String[] args) {
TestLinkedHashSet t = new TestLinkedHashSet();
t.genData();
t.printData();
}
运行结果
第三部分(Map
3.1 HashMap
代码部分
HashMap<String, Shape> hashMap = null;
public void genData() {
hashMap = new HashMap<String, Shape>();
hashMap.put("shape1", new LineShape(1, 0, 0, 0, null, 0));
hashMap.put("shape2", new LineShape(2, 0, 0, 0, null, 0));
hashMap.put("shape3", new LineShape(3, 0, 0, 0, null, 0));
hashMap.put("shape4", new LineShape(4, 0, 0, 0, null, 0));
}
public void printData() {
Set<String> keySet = hashMap.keySet();
Iterator<String> iterator = keySet.iterator();
while (iterator.hasNext()) {
String key = iterator.next();
Shape shape = hashMap.get(key);
System.out.println(key);
System.out.println(shape);
}
}
public static void main(String[] args) {
TestHashMap t = new TestHashMap();
t.genData();
t.printData();
}
运行结果
分析可知,这里也是乱序的
4395

被折叠的 条评论
为什么被折叠?



