Collection层次结构
Collection
[plain]
view plain
copy
子接口 Set,List 集合中只能放置对象的引用,不能放置原生数据类型, 我们需要使用原生数据类型的封装类才能加入到集合中
Ordered与Sorted接口
[plain]
view plain
copy
Ordered排序,按照某种由具体情况决定的顺序排序,是后天指定的 Sorted排序,按照天然顺序进行排序,是先天指定的
List
[plain]
view plain
copy
实现类包括 LinkedList,Vector,ArrayList 列表接口,继承与Collection,可以按索引的顺序访问,有索引的Collection 具有列表的功能,元素顺序均是按添加的先后进行排列的, 允许重复的元素,允许多个null元素
List常用方法
[java]
view plain
copy
package com.itlwc;
import java.util.ArrayList;
import java.util.List;
public class Test {
public static void main(String[] args) { List list =
new ArrayList(); list.add(
"lwc"); list.add(
1,
"nxj"); list.addAll(
new ArrayList()); list.clear(); list.contains(
"nxj"); list.containsAll(
new ArrayList()); list.equals(
new ArrayList()); list.get(
0); list.hashCode(); list.indexOf(
"lwc"); list.lastIndexOf(
"lwc"); list.isEmpty(); list.remove(
0); list.remove(
"lwc"); list.removeAll(
new ArrayList()); list.set(
0,
"lp"); list.size(); list.subList(
1,
2); list.toArray(); list.toArray(
new String[] {
"a",
"b" }); } }
ArrayList
[plain]
view plain
copy
构造方法 public ArrayList() public ArrayList(int initialCapacity) public ArrayList(Collection c) ArrayList依赖于数组实现的,初始长度为10的Object[],并且可随需要而增加的动态数组 当元素超过10,那么ArrayList底层会新生成一个数组,长度为原来的1.5倍+1, 然后将原数组内容复制到新数组中,并且后续增加的内容会放到新数组中, 当新数组无法容纳增加的元素,重复该过程 ArrayList对随机访问性能很好,但进行大量插入,删除操作,性能很差, 因为操作之后后续元素需要移动
遍历ArrayList
[java]
view plain
copy
package com.itlwc;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Test {
public static void main(String[] args) { List<String> list =
new ArrayList<String>(); list.add(
"lwc"); list.add(
"nxj"); Iterator<String> ite1 = list.iterator();
while (ite1.hasNext()) { String str = ite1.next(); System.out.println(str); } System.out.println(
"---------------------");
for (Iterator<String> ite2 = list.iterator(); ite2.hasNext();) { String str = ite2.next(); System.out.println(str); } System.out.println(
"---------------------");
for(String s : list){ System.out.println(s); } } }
Vector
[plain]
view plain
copy
向量,历史比较悠久,Java诞生就有了,特点与ArrayList相同, 不同的是Vector操作元素的方法是同步的,同一时刻只能有一个线程访问,没有特殊需求都使用ArrayList 构造方法 public Vector() public Vector(int initialCapacity) public Vector(int initialCapacity,int capacityIncrement) 第一个参数是初始容量,第二个参数是当Vector满时的增量 public Vector(Collection c) Vector也是依赖数组实现的
案例
[java]
view plain
copy
package com.itlwc;
import java.util.Enumeration;
import java.util.Vector;
public class Test {
public static void main(String[] args) { Vector v =
new Vector(); v.add(
"123"); v.add(
"lwc"); v.add(
"你好"); Enumeration e = v.elements();
while (e.hasMoreElements()) { System.out.println(e.nextElement()); } } }
Stack
[java]
view plain
copy
Vector的子类
案例
[java]
view plain
copy
package com.itlwc;
import java.util.Enumeration;
import java.util.Stack;
public class Test {
public static void main(String[] args) { Stack stack =
new Stack(); stack.push(
new Integer(
123)); stack.push(
"lwc"); stack.push(
new Double(
88.88)); Enumeration items = stack.elements();
while (items.hasMoreElements()) { System.out.print(items.nextElement() +
" "); } System.out.println();
while (stack.size() !=
0) { System.out.print(stack.pop() +
" "); } } }
LinkedList
[plain]
view plain
copy
LinkedList功能与ArrayList,Vector相同,内部是依赖双链表实现的, 因此有很好的插入和删除性能,但随机访问元素的性能很差 构造方法 public LinkedList() public LinkedList(Collection c) LinkedList类中有一个Entry内部类,Entry内部类包含3个部分向前的引用,向后的引用,数据 header.next = header.previous = header;
遍历LinkedList
[java]
view plain
copy
package com.itlwc;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
public class Test {
public static void main(String[] args) { List link =
new LinkedList(); link.add(
123); link.add(
"lwc"); link.add(
8.8); link.add(
"nxj"); link.add(
520); printList(link); printReversedList(link); }
private static void printList(List link) { System.out.println(
"正序链表中的元素"); ListIterator li = link.listIterator();
while (li.hasNext()) { System.out.print(li.next() +
" "); } System.out.println(); }
private static void printReversedList(List link) { System.out.println(
"逆向链表中的元素"); ListIterator li = link.listIterator(link.size());
while (li.hasPrevious()) { System.out.print(li.previous() +
" "); } System.out.println(); } }
自定义LinkedList结构
[java]
view plain
copy
package com.itlwc;
class Node { Node previous; String data; Node next;
public Node(String data) {
this.data = data; } }
public class Test {
public static void main(String[] args) { Node node1 =
new Node(
"node1"); Node node2 =
new Node(
"node2"); Node node3 =
new Node(
"node3"); node1.next = node2; node2.previous = node1; node2.next = node3; node3.previous = node2; node3.next = node1; node1.previous = node3; Node node4 =
new Node(
"node4"); node1.next = node4; node4.previous = node1; node4.next = node2; node2.previous = node4; node1.next = node2; node2.previous = node1; node4.previous =
null; node4.next =
null; } }
依赖倒置原理
[plain]
view plain
copy
依赖应该尽量在抽象层进行,避免在具体层进行, 在实际开发中尽量使用接口类型的引用,避免采用具体类型的引用
案例
[java]
view plain
copy
package com.itlwc;
import java.util.LinkedList;
import java.util.List;
public class Test {
public void printLinkedList(LinkedList ll){ System.out.println(ll); }
public void printList(List l){ System.out.println(l); } }
将数组转换为列表
[java]
view plain
copy
package com.itlwc;
import java.util.Arrays;
import java.util.List;
public class Test {
public static void main(String[] args) { String[] str = {
"l",
"w",
"c" }; List l = Arrays.asList(str); System.out.println(str); } }
ArrayList VS LinkedList
[plain]
view plain
copy
ArrayList底层采用数组实现,LinkedList底层采用双链表实现 如果为列表增加对象 ArrayList是ArrayList底层数组维护的,LinkedList是LinkedList底层Entry对象维护的 LinkedList底层Entry结构 Entry{ Entry previous; Object element; Entry next; } 其中element就是我们添加的元素,最后将生成的Entry对象加入到链表中 插入和删除操作时,采用LinkedList好,搜索时,采用ArrayList好
List<Map>遍历
[java]
view plain
copy
package com.itlwc;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class Test {
public static void main(String[] args) { Map<Integer, String> map1 =
new HashMap<Integer, String>(); map1.put(
new Integer(
1),
"lwc"); map1.put(
new Integer(
2),
"nxj"); Map<Integer, String> map2 =
new HashMap<Integer, String>(); map2.put(
new Integer(
3),
"tom"); map2.put(
new Integer(
4),
"cat"); List<Map<Integer, String>> list =
new ArrayList<Map<Integer, String>>(); list.add(map1); list.add(map2); Iterator<Map<Integer, String>> ite1 = list.iterator();
while (ite1.hasNext()) { Map<Integer, String> m = ite1.next(); System.out.println(m); } System.out.println(
"-----------------------------");
for (Iterator<Map<Integer, String>> ite2 = list.iterator(); ite2.hasNext();) { Map<Integer, String> m = ite2.next(); System.out.println(m); } System.out.println(
"-----------------------------");
for (Map<Integer, String> m : list) { System.out.println(m); } } }
Set
[plain]
view plain
copy
实现类 HashSet,LinkedHashSet 子接口 SortSet 实现类 TreeSet 不包含重复元素,最多包含一个null,元素没有顺序
HashSet
[plain]
view plain
copy
HashSet不是Ordered也不是Sorted,存储对象引用时是按照哈希策略来实现的, HashSet中是否存在一个对象是通过equals()和hashCode()协同判断 不保证顺序 构造方法 public HashSet() public HashSet(int initialCapacity) public HashSet(Collection c) HashSet底层是使用HashMap实现的 HashSet的add()方法详解: 判断已经存储在集合中的对象hashCode值是否与增加对象的hashCode值一致 如果不一致,直接加进去 如果一致,再进行equals()比较 如果equals()返回true,对象已经存在不增加进去 如果equals()返回false,把对象增加进去
LinkedHashSet
[plain]
view plain
copy
LinkedHashSet是Ordered,采用双链表实现的 有固定顺序,也就是插入顺序 LinkedHashSet底层是使用LinkedHashMap实现的 构造方法 public LinkedHashSet() public LinkedHashSet(int initialCapacity) public LinkedHashSet(Collection c)
SortedSet接口
[plain]
view plain
copy
保证迭代器按照元素递增顺序遍历的集合,可以按照元素的自然顺序进行排序 常用方法 Object first() 返回此有序集合中当前第一个(最小的)元素 Object last() 返回此有序集合中最后一个(最大的)元素 SortedSet headSet(Object toElement) 返回此有序集合的部分视图,其元素严格小于toElement SortedSet tailSet(Object fromElement) 返回此有序集合的部分视图,其元素大于或等于fromElement SortedSet subSet(Object fromElement,Object toElement) 返回此有序集合的部分视图,元素范围从fromElement(包括)到toElement(不包括) Comparator comparator() 返回与此有序集合关联的比较器,如果使用元素的自然顺序,则返回 null
TreeSet
[java]
view plain
copy
TreeSet是SortedSet接口的实现,元素不论以什么元素插入,在遍历的时候,都会以天然顺序遍历 TreeSet底层是使用TreeMap实现的 构造方法
public TreeSet()
public TreeSet(SortedSet s)
public TreeSet(
int initialCapacity)
public TreeSet(Comparator<?
super E>)
public TreeSet(Collection c) 因为TreeSet是带排序的,所以想要为TreeSet增加自定义类型,必须指定排序规则
TreeSet排序规则Comparator案例
[java]
view plain
copy
package com.itlwc;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
public class Test {
public static void main(String[] args) { TreeSet set =
new TreeSet(
new PersonComparator()); set.add(
new Person(
"lwc",
80)); set.add(
new Person(
"nxj",
70)); set.add(
new Person(
"lp",
60)); set.add(
new Person(
"fy",
75)); Iterator ite = set.iterator();
while (ite.hasNext()) { Person p = (Person)ite.next(); System.out.println(p.name); } } }
class Person { String name;
int score;
public Person(String name,
int score) {
this.name = name;
this.score = score; } }
class PersonComparator
implements Comparator {
public int compare(Object o1, Object o2) { Person p1 = (Person) o1; Person p2 = (Person) o2;
return p1.score - p2.score; } }
Collections
[plain]
view plain
copy
操作Collection类的工具类,类中方法都是静态的
Collections常用方法
[java]
view plain
copy
package com.itlwc;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Test {
public static void main(String[] args) { Collections.copy(
new ArrayList(),
new ArrayList()); Collections.disjoint(
new ArrayList(),
new ArrayList()); Collections.fill(
new ArrayList(),
new Object()); Collections.frequency(
new ArrayList(),
new Object()); Collections.indexOfSubList(
new ArrayList(),
new ArrayList()); Collections.max(
new ArrayList()); Collections.min(
new ArrayList()); Collections.replaceAll(
new ArrayList(),
"oldVal",
"newVal"); Collections.reverse(
new ArrayList()); Collections.reverseOrder(); Collections.reverseOrder(
new Comparator() {
@Override public int compare(Object o1, Object o2) {
return 0; } }); Collections.shuffle(
new ArrayList()); Collections.sort(
new ArrayList()); Collections.sort(
new ArrayList(), Collections.reverseOrder()); Collections.swap(
new ArrayList(),
1,
2); } }