java10-集合框架

来源:互联网 发布:福州网络学校 编辑:程序博客网 时间:2024/06/18 11:45

------- android培训java培训、期待与您交流! ----------


集合框架



出现原因
面向对象语言对事物的体现都是以对象的形式,为了方便对多个对象的操作,就对对象进行存储,集合就是存储对象最常用的一种方式。


数据和集合都是容器。
数据的长度是固定的,可以存储基本数据类型。集合长度是可变的,只能存储对象。可以存储不同类型的对象。


常用集合框架
Collection包含List和Set。List包含ArrayList,LinkedList,Vector。Set包含HashSet,TreeSet.
因为每个容器对数据的存储方式都有不同。这个存储方式称之为:数据结构。


Collection


基本方法
1,add方法的参数类型是Object.以便于接收任意类型对象。
2,集合中存储的都是对象的引用(地址)。


迭代器
就是集合取出元素的方式。
内部类能够直接访问外部类中元素。集合取出方式定义在集合内部,就可以直接访问集合内的元素,所以定义成了内部类。

每个集合的数据结构不同,取出对象都是判断和取出,但是细节不同,取出过程判断和取出相同,就把过程抽象成接口Interator。对外提供方法inerator();来获取取出对象。通过多态父类类型指向子类对象实现。

import java.util.*;class CollectionDemo{public static void main(String[] args){//创建一个集合容器,使用Collection接口的子类ArrayListArrayList al = new ArrayList();//添加元素al.add("java01");al.add("java02");//获取个数,集合长度al.size();//删除元素al.remove("java02");//清空集合al.clear();//判断元素al.contains("java01");ArrayList al2 = new ArrayList();al2.add("java01");//取交集al.retainAll(al2);//al中只有和al2相同的对象//去交集al.removeAll(al2);//al中只有和al2不相同的对象Iterator it = al.iterator();//获取迭代器,用于取出集合中的元素。while(it.hasNext())//hasNext如果仍有元素可以迭代,则返回true。{System.out.println(it.next);//next()返回迭代的下一个元素}for(Iterator it = al.iterator();it.hasNext();)//标准格式,节省空间//迭代循环时,hasNext一次next一次{System.out.println(it.next);}}}

Collection
--List:元素是有序的,元素可以重复。因为该集合体系有索引。(有序无序是对存入取出的顺序)
--ArrayList:底层的数据结构使用的是数组结构。特点是查询修改速度快,但是增加删除慢,线程是不同步的。(初始容量为10,new一个增加50%的,放进新数组)
--LinkedList:底层使用的链表数据结构。特点是是查询速度慢,但是增加删除快
--Vector:底层使用的是数组结构。1.0出现的。线程同步的,在1.2时被ArrayList替代了。(new增加100%)
枚举是Vector特有的取出方式。和迭代一样,因为名称和方法名称过长,被迭代器取代了。
--Set:元素是无序的,不可以重复。


List集合共性方法
特有方法。凡是可以操作角标的方法都是该体系特有的方法。


add(index,element);
addAll(index,Collection);



remove(index);



set(index,element);

get(index);
subList(from,to);子列表
listIterator();列表迭代器


LinkedList特有方法:
addFirst();
addLast();
头添加,尾添加。
getFirst();
getLast();
获取元素,但不删除元素。如集合中没有元素,会出现NoSuchElementException
removeFirst();
removeLast();
获取元素,并删除元素。如集合中没有元素,会出现NoSuchElementException


1.6中出现了替代方法
offerFirst();
offerLast();


peekFirst();
peekLast();
如集合中没有元素,会返回null。
pollFirst();
pollLast();
如集合中没有元素,会返回null。

{ArrayList al = new AllayList();al.add("a1");al.add("a2");al.add("a3");al.add("a4");//在指定位置添加元素al.add(1,"a4");//删除指定位置的元素al.remove(2);//修改元素al.set(1,"a2");//通过角标获取元素al.get(1);//获取所有元素for(int x = 0;x<al.size();x++){System.out.println("al"+x+"="+al.get(x));}//也可以用迭代器for(Iterator it = al.iterator();it.hasNext();){System.out.println(it.next);}//通过indexOf获取对象位置al.indexOf("a1");//获取子列表List sub = al.subList(1,3);}



listIterator
List集合特有的迭代器。ListIteraTor是Iterator的子接口。
迭代时,不可以通过集合对象的方法操作集合中的元素,因为会发生并发修改异常ConcurrentModificationException.
迭代时,只能用迭代器的方法操作元素,但Interator方法是有限的,只能对元素进行判断,取出,删除操作。其他操作就要用子接口ListIterator.该接口只能通过List集合的listIterator方法获取。List集合是有序的,可以进行多种特有操作。

import java.util.*;class Lis{public static void main (String[] args){ArrayList al = new ArrayList();al.add("1");al.add("2");al.add("3");ListIterator li = al.listIterator();while(li.hasNext()){Object obj = li.next();if(obj.equals("2"))li.add("5");//添加//li.set("6");//替换//li.remove();//删除//操作的是集合中的指引,对象还在,System.out.println(obj);//}while(li.hasPrevious())//反向遍历,{System.out.println(li.previous()+"-");}System.out.println(al);}}1233-5-2-1-[1, 2, 5, 3]/*练习使用LinkedList模拟一个堆栈或者队列数据结构堆栈:先进后出,如同杯子。队列:先进先出,如同水管。*/import java.util.*;class DuiLie{private LinkedList link;DuiLie(){link = new LinkedList();}public void myAdd(Object obj){link.addFirst(obj);}public Object myGet(){return link.removeLast();//队列:先进先出//First()堆栈:先进后出}public boolean isNull(){return link.isEmpty();}}class Lin{public static void main(String[] args){DuiLie dl = new DuiLie();dl.myAdd("dl1");dl.myAdd("dl2");dl.myAdd("dl3");dl.myAdd("dl4");while(!dl.isNull()){System.out.println(dl.myGet());}}}/*将自定义对象作为元素存到ArrayList集合中,并去除重复元素。存入人对象,同名同年为同一人List集合中,comtains与remove调用的是对象类中的equals方法。输入对象时,要根据需要对类从新覆写设定equals方法。迭代器中对象Object类指向对象,要调用对象方法,就要向下转型。*/import java.util.*;class Person{private String name;private int age;Person(String name,int age){this.name = name;this.age = age;}public boolean equals(Object obj){if(!(obj instanceof Person))return false;Person p = (Person)obj;//向下强转型return this.name.equals(p.name)&&this.age == p.age;}public String getName(){return name;}public int getAge(){return age;}}class ArrayListd{public static void main(String[] args){ArrayList al = new ArrayList();al.add(new Person("a1",12));al.add(new Person("a2",23));al.add(new Person("a1",12));al.add(new Person("a4",45));al.add(new Person("a2",23));al = singleElement(al);for(Iterator it = al.iterator();it.hasNext();){//迭代器中对象类为Object类要使用对象方法就要向下转型Person p = (Person)it.next();System.out.println(p.getName()+"-"+p.getAge());}}public static ArrayList singleElement(ArrayList al){ArrayList newAl = new ArrayList();//创建一个临时容器for(Iterator it = al.iterator();it.hasNext();){//迭代循环时,hasNext一次next一次Object obj = it.next();if(!newAl.contains(obj))newAl.add(obj);}return newAl;}}

--Set:元素是无序的,不可以重复。
Set集合的功能和Collection的功能是一致的。
--HashSet:底层数据结构是哈希表。
通过元素的两个方法,hashCode和equals判断元素保证元素唯一性。(hashCode||equals)因此,HashSet通常要覆写hashCode和equals方法。hashCode(age*23;*23尽量保证哈希值唯一性。)线程是不同步的
(hashCode||equals)判断元素是否存在,及删除等操作。


--TreeSet:可以对Set集合中的元素进行排序。底层数据结构是二叉树。
保证元素唯一性是compareTo方法return 0.排序是根据compareTo方法返回正负0
。当主要条件相同时,一定要判断下次要条件||。


TreeSet排序1:让元素自身具备比较性。需要实现Comparable接口,覆盖cpmpareTo方法、也称为元素的自然顺序或默认顺序。
TreeSet排序2:不具备比较性,或者比较性不是所需的时,需要让集合自身具备所需比较性,使集合初始化时就具有。
两种方法都存在时,以比较器为主。
定义一个类,实现Comparator接口,覆盖compare方法。


/*练习TreeSet用字符串长度做比较性*/import java.util.*;class Lis{public static void main(String[] args){TreeSet ts = new TreeSet(new MyComparator());ts.add("s1");ts.add("s12");ts.add("s23");ts.add("s145");ts.add("s1");//迭代for(Iterator it = ts.iterator();it.hasNext();){System.out.println(it.next());}}}class MyComparator implements Comparator{public int compare(Object o1,Object o2){//类型向下转换String s1 = (String) o1;String s2 = (String) o2;//比较int num = new Integer(s1.length()).compareTo(new Integer(s2.length()));if(num == 0)return s1.compareTo(s2);/*int num;num = (s1.length()-s2.length());if(num==0)return(s1.compareTo(s2));*/return num;}}

------- android培训java培训、期待与您交流! ----------