以前的基础篇中咱们知道了一种数据结构:数组,能够存放不少数据。可是数据有很大的局限性:java
那么是否有其余的数据结构或者数据类型用于存储数据以解决数组的局限性呢,集合框架就是如此,也称为容器。算法
集合类型可分为Collection和Map。数组
Collection的结构图以下:复杂继承和接口实现
安全
接口名 | 描述与做用 | ||
---|---|---|---|
Iterator | 迭代器,以前在说加强for循环中有提到过迭代器,是Collection的父接口 | ||
Collection | 是List、Set和Queue的父接口,存储一组不惟1、无序的对象,通常使用其子接口实现类进行操做数据 | ||
List | 可经过索引获取对象,存储一组不惟1、有序的对象 | ||
Set | 存储一组惟1、无序的对象 | ||
Queue | 队列接口 |
Map地结构图以下:
数据结构
接口名 | 描述与做用 | ||
---|---|---|---|
Map | 存储key-value的一组键值对象 |
接下我会选出经常使用的实现类进行解析。框架
实现了List接口和Queue接口,即存储一组不惟1、容许null,有序的对象,而且也可做为队列使用。采用链表结构进行实现,便于集合的插入和删除元素,访问元素相对较慢。因为其实现方法没有synchronized关键字修饰,因此是线程不安全的。
例1(正常):ide
public class TestLinkedList1 { public static void main(String[] args) { List<String> linkedList = new LinkedList<>(); //添加元素 linkedList.add("zhangsan"); linkedList.add("lisi"); linkedList.add(null); linkedList.add(null); //经过索引获取对象 System.out.println(linkedList.get(0)); System.out.println("------------------"); //使用加强for循环遍历迭代器 for (String name : linkedList) { System.out.println(name); } System.out.println("------------------"); //删除元素 linkedList.remove(null); for (String name : linkedList) { System.out.println(name); } } }
执行结果:
this
例2(看成队列):线程
public class TestLinkedList2 { public static void main(String[] args) { Queue<String> queue = new LinkedList<>(); //添加元素 queue.add("zhangsan"); queue.add("lisi"); System.out.println(queue.peek()); //删除元素 String name = queue.poll(); System.out.println(name); } }
执行结果:
code
遵循队列的先进先出原则。
实现了List接口,存储一组不惟1、容许null,有序的对象。采用大小可变的数组实现,可进行快速的随机访问,即索引访问,可是插入和删除元素较为费时。初始大小为10,也可以使用构造器指定大小建立。和LinkedList同样是线程不安全的。
例子:
public class TestArrayList { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("zhangsan"); list.add("lisi"); list.add("lisi"); System.out.println(list.get(0)); System.out.println("--------------"); for (String name : list) { System.out.println(name); } System.out.println("--------------"); list.remove("lisi"); for (String name : list) { System.out.println(name); } } }
执行结果:
实现了Set接口,即存储一组惟一的、无序的、能够为null的对象。因为使用hash算法存储集合元素,所以具备很好的存取和查找的新娘功能。是线程不安全的。
public class TestHashSet { public static void main(String[] args) { Set<String> hashSet = new HashSet<>(); hashSet.add("zhangsan"); hashSet.add("zhangsan"); hashSet.add("lisi"); System.out.println(hashSet.size()); System.out.println(hashSet.contains("lisi")); System.out.println("-------------------"); for (String name : hashSet) { System.out.println(name); } System.out.println("-------------------"); hashSet.remove("zhangsan"); for (String name : hashSet) { System.out.println(name); } } }
执行结果:
实现了SortedSet接口(该接口继承Set接口),即存储一组惟一的、有序的对象,这里的有序是有条件的,对象须要实现Comparable接口。是线程不安全的。
咱们发现Comparable接口中只有一个方法public int compareTo(T o);
,则返回对象实现该方法便可。若是当前对象小于、等于和大于方法中的对象,返回负整数、零和正整数。
例子:
public class Person implements Comparable { private String name; private int age; public Person(){} public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } @Override public int compareTo(Person o) { if (this.age > o.getAge()) { return 1; } else if (this.age == o.getAge()) { return 0; } else { return -1; } } }
public class TestTreeSet { public static void main(String[] args) { TreeSet<Person> treeSet = new TreeSet<>(); treeSet.add(new Person("zhangsan", 20)); treeSet.add(new Person("lisi", 33)); treeSet.add(new Person("zhangsan2", 20)); treeSet.add(new Person("wanger", 15)); for (Person p : treeSet) { System.out.println(p); } System.out.println("----------------"); treeSet.remove(new Person("lisi2", 33)); for (Person p : treeSet) { System.out.println(p); } } }
执行结果:
咱们使用Person类的age属性做为比较的依据,相同age的即相同对象。当咱们插入数据时,会按照age进行升序排列;当删除元素时,按照age相等的进行删除。
实现了Map接口,即键值对存储对象,key不可重复,无序,使用hash算法进行存储元素,相同key进行插入时会覆盖原有的值。是线程不安全的。
例子:
public class TestHashMap { public static void main(String[] args) { Map<String, Integer> hashMap = new HashMap<>(); hashMap.put("zhangsan", 20); hashMap.put("lisi", 33); hashMap.put("zhangsan", 22); hashMap.put("wanger", 15); hashMap.put("wanger2", null); for (Map.Entry<String, Integer> entry : hashMap.entrySet()) { System.out.println("key=" + entry.getKey() + ", value=" + entry.getValue()); } System.out.println("----------------"); hashMap.remove("zhangsan"); for (Map.Entry<String, Integer> entry : hashMap.entrySet()) { System.out.println("key=" + entry.getKey() + ", value=" + entry.getValue()); } } }
执行结果:
实现了SortedMap接口,故名思意是有序的,key有序、不可为null、不可重复。是线程不安全的。
例子:
public class TestTreeMap { public static void main(String[] args) { Map<String, String> map = new TreeMap<>(); map.put("zhangsan", "aaaa"); map.put("lisi", "bbbb"); map.put("zhangsan", "cccc"); for (String name : map.keySet()) { System.out.println(map.get(name)); } System.out.println("----------------"); map.remove("lisi"); for (String name : map.keySet()) { System.out.println(map.get(name)); } } }
执行结果:
集合框架的优点在于元素通用性。在以前的集合中咱们已经遇到了泛型,在Map<String, String> map = new TreeMap<>();
中map的key指定为String类,value也指定String类,在TreeMap类定义中public class TreeMap<K,V>
这里的K和V就是泛型,泛型提供编译时类型安全检测机制。
例子:
public class TestHashMap { public static void main(String[] args) { Integer[] arr1 = {1,2,3,4,5}; Double[] arr2 = {1.1,2.2,3.3,4.4,5.5}; String[] arr3 = {"zhangsan", "lisi", "wanger"}; printArray(arr1); printArray(arr2); printArray(arr3); } static <E> void printArray(E[] array) { for (E e : array) { System.out.println(e); } System.out.println("-------------------"); } }
执行结果: