温习java collection(集合对象)和泛型

来源:互联网 发布:淘宝炒作多久会消失 编辑:程序博客网 时间:2024/06/06 06:51

collection是收集,聚集的意思,顾名思义就是将数据集中在一起,以某种方式来访问数据。大家可能会想到一种数据结构-----数组,严格来说数组不是一种集合(动态数组除外),因为集合里面的元素数量不是固定的,可以是0个,也可以是10个,也可以是无数个(当然这是在内存允许的情况下),数组只能说的与集合类似。


在JDK1.2版本之前,JDK对collection的实现并不完善,只有Vector,Hashtable,Arrays三种类。



在JDK1.2和1.2之后,java完善和提供了全新的处理与保存集合对象的统一框架,称为 Java Collections Framework,包括三个部分:接口(interface),算法(algorithms),

实现(implementation)。其中在最顶层的集合接口为Collection<E>,有一个AbstractCollection<E>类实现了该接口。而为了方便取出(遍历)集合中的数据,最顶层的集合接口继承了一个Iterable<T>接口(又名迭代器接口)。


上述出现的E,T这两个符号,其实他们是java JDK 1.5引入的泛型(Generic)(JDK开发内部版本号为1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,JDK发行版办号为1.1,1.2,1.3,1.4,5.0,6.0,7.0,8.0),泛型的出现背景是在编译时期检查集合对象的类型,因此可以减少程序在读取元素时所必须的元素类型转换。泛型类型 :  <大写字母符号>  ,例如<T>  <E> <A>  <B>


接下来我们介绍下实现集合接口的类:

实现Set接口----------HashSet接口(无序不重复)

import java.util.HashSet;import java.util.Iterator;public class MyCollection {public static void main(String[] args) {//HashSet<E>HashSet<String> hashSet = new HashSet<String>();hashSet.add("D");hashSet.add("A");hashSet.add("B");hashSet.add("C");hashSet.add("A");//打印HashSet集合内数据System.out.println("HashSet's data is "+hashSet.toString());//遍历HashSet集合内数据:一(foreach)for (String string : hashSet) {System.out.println("HashSet's data is "+string);}//遍历HashSet集合内数据:二(iterator)Iterator<String> iterator = hashSet.iterator() ;while (iterator.hasNext()) {System.out.println("HashSet's data is "+iterator.next());}}}
程序运行截图:


实现SortedSet接口------------TreeSet类(从小到大排列,不重复)

import java.util.Iterator;import java.util.TreeSet;public class MyCollection {public static void main(String[] args) {//TreeSet<E>TreeSet<Integer> treeSet = new TreeSet<Integer>() ;for(int i=5;i >=0;i--)treeSet.add(i); treeSet.add(5); treeSet.add(100); //打印TreeSet集合内数据System.out.println("TreeSet's data is "+treeSet.toString());//遍历TreeSet集合内数据:一(foreach)for (Integer integer : treeSet) {System.out.println("TreeSet's data is "+integer);}//遍历TreeSet集合内数据:二(iterator)Iterator<Integer> iterator = treeSet.iterator() ;while (iterator.hasNext()) {System.out.println("TreeSet's data is "+iterator.next());}}}
程序运行截图


实现List接口一:------------LinkedList   ( List:链接   LinkedList:链表),可用Iterator和ListInterator

import java.util.Iterator;import java.util.LinkedList;public class MyCollection {public static void main(String[] args) {//LinkedList<E>LinkedList<Integer> linkedList = new LinkedList<Integer>() ;for(int i=3;i >=0;i--)linkedList.add(i);linkedList.addFirst(10);linkedList.addLast(4);linkedList.addFirst(12); //打印LinkedList集合内数据System.out.println("LinkedList's data is "+linkedList.toString());//遍历LinkedList集合内数据:一(foreach)for (Integer integer : linkedList) {System.out.println("LinkedList's data is "+integer);}//遍历LinkedList集合内数据:二(iterator)Iterator<Integer> iterator = linkedList.iterator() ;while (iterator.hasNext()) {System.out.println("LinkedList's data is "+iterator.next());}}}
程序运行截图


实现List接口------------ArrayList类(类似与数组)

import java.util.ArrayList;import java.util.Iterator;public class MyCollection {public static void main(String[] args) {//LinkedList<E>ArrayList<Integer> arrayList = new ArrayList<Integer>() ;for(int i=3;i >=0;i--)arrayList.add(i);arrayList.add(1,100);arrayList.add(3,200);arrayList.add(0,300); //打印ArrayList集合内数据System.out.println("ArrayList's data is "+arrayList.toString());//遍历ArrayList集合内数据:一(foreach)for (Integer integer : arrayList) {System.out.println("ArrayList's data is "+integer);}//遍历ArrayList集合内数据:二(iterator)Iterator<Integer> iterator = arrayList.iterator() ;while (iterator.hasNext()) {System.out.println("ArrayList's data is "+iterator.next());}}}
程序运行截图


现Map接口----------------HashMap类 (键值对) (无重复)

import java.util.HashMap;import java.util.Iterator;import java.util.Set;public class MyCollection {public static void main(String[] args) {//key value//HashMap<K, V>  K:关键值(关键字)   V:对应值(值)HashMap<Integer, String> hashMap = new HashMap<Integer,String>() ;hashMap.put(1, "wang");hashMap.put(2, "li");hashMap.put(3, "zhao");hashMap.put(1, "sun"); //打印HashMap集合内数据System.out.println("HashMap's data is "+hashMap.toString());//遍历HashMap集合内数据:Set<Integer> key = hashMap.keySet();Iterator<Integer> iterator = key.iterator() ;while (iterator.hasNext()) {int index = iterator.next();System.out.println("[key:"+index+",value:"+hashMap.get(index)+"]");}}}

程序运行结果

HashMap's data is {1=sun, 2=li, 3=zhao}
[key:1,value:sun]
[key:2,value:li]
[key:3,value:zhao]


实现SortedMap接口------------TreeMap类(元素按关键之从小到大排序,无重复)

import java.util.Iterator;import java.util.Set;import java.util.TreeMap;public class MyCollection {public static void main(String[] args) {//key value//TreeMap<K, V>  K:关键值(关键字)   V:对应值(值)TreeMap<Integer, String> treeMap = new TreeMap<Integer,String>() ;treeMap.put(3, "zhao");treeMap.put(1, "wang");treeMap.put(2, "li");treeMap.put(1, "sun"); //打印TreeMap集合内数据System.out.println("TreeMap's data is "+treeMap.toString());//遍历TreeMap集合内数据:Set<Integer> key = treeMap.keySet();Iterator<Integer> iterator = key.iterator() ;while (iterator.hasNext()) {int index = iterator.next();System.out.println("[key:"+index+",value:"+treeMap.get(index)+"]");}}}
程序运行结果

TreeMap's data is {1=sun, 2=li, 3=zhao}
[key:1,value:sun]
[key:2,value:li]
[key:3,value:zhao]

0 0
原创粉丝点击