您的位置:首页 > 编程语言 > Java开发

容器深入研究笔记

2017-11-06 23:51 323 查看

容器深入研究

之前介绍过了Java容器类库的概念和基本功能,这些对于使用容器来说已经足够了。这次的笔记目的在于更加深入的探究这个重要的类库。

完整的容器分类法



上图来自百度百科

Java SE5 添加了:Queue接口、ConcurrentMap接口、CopyOnWriteArrayList、CopyOnWriteArraySet、EnumSet、EnumMap。

填充容器

就如同Arrays.fill()一样,容器也有填充的方法,相应的是Collections.fill()、Collection.nCopies()方法。
注意:Collections.fill()替换已经存在在list中的元素

public class TestClass {
public static void main(String[] args) {
// Collections.nCopies()方法用来向list添加多个某对象的引用
List<TestClass> list = new LinkedList<>(Collections.nCopies(2, new TestClass()));
System.out.println(Arrays.toString(list.toArray()));
// Collections.fill()替换已经存在在list中的元素
Collections.fill(list, new TestClass());
System.out.println(Arrays.toString(list.toArray()));
}
}
----------------------------output----------------------------
[com.stupidzhe.jdklearning.base.collection.TestClass@6f79caec,  com.stupidzhe.jdklearning.base.collection.TestClass@6f79caec]
[com.stupidzhe.jdklearning.base.collection.TestClass@67117f44, com.stupidzhe.jdklearning.base.collection.TestClass@67117f44]
--------------------------------------------------------------

Collection接口

Collection接口下的类有ArrayList、LinkedList、HashSet、TreeSet、Stack、Queue等。

关于Collection接口下的一些常用方法:

// contains()检查是否有某元素在该list中,有则返回true
if (list.contains(testClass)) {
System.out.println("contains: " + true);
}

// toArray()返回一个数组
String[] array = list.toArray();
System.out.println(Arrays.toStirng(array));

// clear()清空list中的元素
list.clear();

// isEmpty检验list是否为空
if (list.isEmpty()) {
System.out.println("isEmpty" + true);
}

<!--## List的功能方法

```{Java}

// 添加
list.add(new Object());

// 通过索引获取元素
list.get(0);

```-->

Set和存储顺序

Set 要求每个元素都必须是唯一的,因为Set不保存重复元素。加入Set的元素必须定义equals()方法以确保对象的唯一性。Set与Collection有完全一样的接口。Set接口不保证维护元素的次序

HashSet快速查找而设计的Set。存入HashSet的元素必须定义hashCode()

TreeSet 保持次序的Set,底层为树结构。使用它可以从Set中提取有序的序列。元素必须实现Comparable接口

LinkedHashSet 具有内部使用链表维护元素的顺序(插入的次序)。于是在使用迭代器遍历Set时,结果会按元素插入的次序显示。元素也必须定义hashCode()方法。

注意:HashSet应该是你默认的选择。

如果查看JDK源码,你会发现一个事实:HashSet内部是由HashMap构成。

SortedSet中的元素可以保证处于排序状态,TreeSet是SortedSet的实现类

// 要扩展Comparable
public class TreeSetExample implements Comparable<TreeSetExample> {

private static int index = 0;
private int c;

public TreeSetExample() {
c = index++;
}

public static void main(String[] args) {
SortedSet<TreeSetExample> sortedSet = new TreeSet<>();
TreeSetExample t1 = new TreeSetExample();
TreeSetExample t2 = new TreeSetExample();
TreeSetExample t3 = new TreeSetExample();
TreeSetExample t4 = new TreeSetExample();
sortedSet.add(t1);
sortedSet.add(t2);
sortedSet.add(t3);
sortedSet.add(t4);
for (TreeSetExample treeSetExample : sortedSet) {
System.out.println(treeSetExample.c);
}
}

@Override
public int compareTo(TreeSetExample o) {
if (o.equals(this)) {
return 0;
}
if (o.c == this.c) {
return 0;
}
// 降序
return o.c < this.c?-1:1;
}
}
-----------------output----------------
3
2
1
0
---------------------------------------

队列

除了并发应用,Queue在Java SE5中仅有的两个实现是LinkedQueue和PriorityQueue,它们的差异在于排序行为而不是性能
以下是通过LinkedList来实现队列:

public class QueueExample {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();
queue.add("2");
queue.add("1");
queue.add("1");
queue.add("3");
System.out.println(queue.peek());
System.out.println(queue.poll());
System.out.println(queue.poll());
System.out.println(queue.poll());
System.out.println(queue.poll());
System.out.println(queue.poll());
}
}
---------------output----------------
2
2
1
1
3
null
-------------------------------------

以下是PriorityQueue:

public class QueueExample {

private static int index = 0;
private int c;

public QueueExample() {
c = index++;
}

public String test(FInterface fInterface, String content) {
return fInterface.print(content);
}

public static void main(String[] args) {

// 这里通过lambda表达式实例化扩展Comparetor接口的匿名类
Queue<QueueExample> queue = new PriorityQueue<>(10, (o1, o2) -> (o1.c >= o2.c)?1:-1);
queue.add(new QueueExample());
queue.add(new QueueExample());
queue.add(new QueueExample());
queue.add(new QueueExample());
// enen这是我测试lambda表达式写的东西
System.out.println(queue.poll().test((o) -> o + "1", "test"));
System.out.println(queue.poll().c);
System.out.println(queue.poll().c);
System.out.println(queue.poll().c);

}
}
interface FInterface {
public String print(String n);
}

-----------output-----------
test1
1
2
3
----------------------

注意:LinkedList准确来说是双向队列(Deque)

public static void main(String[] args) {

LinkedList<String> deque = new LinkedList<>();
deque.addFirst("1");
deque.addFirst("2");
System.out.println(deque.removeFirst());
System.out.println(deque.removeFirst());
deque.addLast("3");
deque.addLast("4");
System.out.println(deque.removeFirst());
System.out.println(deque.removeFirst());

deque.addLast("5");
deque.addLast("6");
System.out.println(deque.removeLast());
System.out.println(deque.removeLast());
}

------------------output--------------------
2
1
3
4
6
5
--------------------------------------------

关于Map,将在下一个学习笔记中记录。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Collection Java