黑马程序员_集合框架

来源:互联网 发布:人工智能的利弊英文ppt 编辑:程序博客网 时间:2024/05/01 10:59

---------------------- ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ----------------------

      集合用于存储对象的容器,长度可变,且不能存储基本数据类型数据。集合框架的顶层有Collection、Map,所有集合类都要实现Collection、Map接口。

      一、Collection接口主要方法:1,添加。 boolean add(Object obj):  boolean addAll(Collection coll):

2,删除。boolean remove(object obj):  boolean removeAll(Collection coll);  void clear();

3,判断: boolean contains(object obj):   boolean containsAll(Colllection coll);   boolean isEmpty():判断集合中是否有元素。

4,获取:int size():获取集合长度。 Iterator iterator():取出元素的方式:迭代器。该对象必须依赖于具体容器(集合),因为每一个容器的数据结构都不同。 获取实例:

package itcast;import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;public class IteratorDemo {public static void main(String[] args) {Collection coll = new ArrayList();coll.add("abc1");coll.add("abc2");coll.add("abc3");coll.add("abc4");//用迭代器依次取出coll中元素for(Iterator it = coll.iterator(); it.hasNext(); ){System.out.println(it.next());}}}

      迭代器实现原理:所以该迭代器对象是在容器中进行内部实现的。 对于使用容器者而言,具体的实现不重要,只要通过容器获取到该实现的迭代器的对象即可, 也就是iterator方法。Iterator接口就是对所有的Collection容器进行元素取出的公共接口。

      二、List和Set:他们都是Collection的子接口

1、List与Set的不同点:List:有序(存入和取出的顺序一致),元素都有索引(角标),元素可以重复。Set:元素不能重复,无序。

2、List主要方法:List:特有的常见方法:有一个共性特点就是都可以操作角标。
 1)添加 void add(index,element); void add(index,collection);

 2)删除;Object remove(index): 

 3)修改: Object set(index,element);

 4)获取:Object get(index); int indexOf(object);  int lastIndexOf(object); List subList(from,to);

 5)迭代:ListIterator listIterator() ;为了避免迭代器和集合本身并发操作集合元素发生ConcurrentModificationException异常,可以使用Iterator的子接口ListIterator接口,但是只有List及其子类才能用ListIterator接口,就是说只有List才能使用这种迭代器,才能在迭代过程中对集合元素进行操作。ListIterator接口可以完成双向迭代,从某个位置迭代的功能。

package itcast.list;import java.util.ArrayList;import java.util.List;import java.util.ListIterator;public class ListDemo2 {public static void main(String[] args) {List list = new ArrayList();list.add("abc1");list.add("abc2");list.add("abc3");System.out.println("list:"+list);ListIterator it = list.listIterator();//获取列表迭代器对象//它可以实现在迭代过程中完成对元素的增删改查。//注意:只有list集合具备该迭代功能.while(it.hasNext()){Object obj = it.next();if(obj.equals("abc2")){it.set("abc9");}}//ListIterator的反向迭代功能while(it.hasPrevious()){System.out.println("previous:"+it.previous());}System.out.println("list:"+list);}}

3、List主要子类:Vector,ArrayList,LinkedList

1)Vector:内部是数组数据结构,是同步的。增删,查询都很慢!

2)ArrayList:内部是数组数据结构,是不同步的。替代了Vector。查询的速度快。

3)LinkedList:内部是链表数据结构,是不同步的。增删元素的速度很快。主要添加了与头和尾有关的方法

添加:addFirst();addLast();    offerFirst();offerLast();——JDK1.6新特性,链表为空时返回null。

获取不删除:getFirst();.getLast();  ——获取但不移除,如果链表为空,抛出NoSuchElementException.
                      peekFirst();peekLast();——获取但不移除,如果链表为空,返回null,JDK1.6新特性
获取并删除:removeFirst();removeLast();——获取并移除,如果链表为空,抛出NoSuchElementException.

                     pollFirst();pollLast();——获取并移除,如果链表为空,返回null,JDK1.6新特性

4、Set, Set的方法与Collection方法一致,主要子类有HashSet、TreeSet

1),HashSet:内部数据结构是哈希表 ,是不同步的。

哈希表确定元素相同的方法:先判断两个元素的哈希值(hashCode方法)是否相同;如果相同,判断两个对象内容(equals方法)是否相同。如果哈希值不同不用判断判断equals。只有两个方法都相同才视为相同元素,反之视为不同元素。

注:如果自定义类的元素要存储到HashSet集合中,必须覆盖hashCode方法和equals方法。一般情况下,如果定义的类会产生很多对象,比如人,学生,书,通常都需要覆盖equals,hashCode方法。建立对象判断是否相同的依据。

LinkedHashSet是HashSet的子类,具有可预知迭代顺序的 Set 接口的哈希表和链接列表实现。

2),TreeSet:可以对Set集合中的元素进行排序。是不同步的。 判断元素唯一性的方式:就是根据比较方法的返回结果是否是0,是0,就是相同元素,不存。

TreeSet对元素进行排序的方式一:让元素自身具备比较功能,元就需要实现Comparable接口。覆盖compareTo方法。

package itcast;import java.util.Iterator;import java.util.TreeSet;public class TreeSetDemo {public static void main(String[] args) {TreeSet<Person> ts = new TreeSet<Person>();ts.add(new Person("zhangsan",15));ts.add(new Person("lisi",21));ts.add(new Person("xiaoqiang",15));ts.add(new Person("xiaowang",23));ts.add(new Person("wangwu",23));Iterator<Person> it = ts.iterator();while(it.hasNext()){Person p = it.next();System.out.println(p.getName()+"::"+p.getAge());}}}class Person implements Comparable<Person>{private String name;private int age;public Person(String name, int age) {super();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;}public int compareTo(Person o) {//按年龄排序if(this.age==o.age)  return this.name.compareTo(o.name);return this.age-o.age;}}


如果不要按照对象中具备的自然顺序进行排序。如果对象中不具备自然顺序。怎么办?
可以使用TreeSet集合第二种排序方式二: 让集合自身具备比较功能,定义一个类实现Comparator接口,覆盖compare方法。将该类对象作为参数传递给TreeSet集合的构造函数。

用构造函数:TreeSet(Comparator<? super E> comparator)     构造一个新的空 TreeSet,它根据指定比较器进行排序。

 

package itcast;import java.util.Comparator;import java.util.Iterator;import java.util.TreeSet;public class TreeSetDemo {public static void main(String[] args) {TreeSet<Person> ts = new TreeSet<Person>(new Comparator<Person>() {public int compare(Person o1, Person o2) {//按年龄排序int temp = o1.getAge()-o2.getAge();return temp==0?o1.getName().compareTo(o2.getName()):temp;}});ts.add(new Person("zhangsan",15));ts.add(new Person("lisi",21));ts.add(new Person("xiaoqiang",15));ts.add(new Person("xiaowang",23));ts.add(new Person("wangwu",23));Iterator<Person> it = ts.iterator();while(it.hasNext()){Person p = it.next();System.out.println(p.getName()+"::"+p.getAge());}}}class Person implements Comparable<Person>{private String name;private int age;public Person(String name, int age) {super();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;}public int compareTo(Person o) {//按年龄排序if(this.age==o.age)  return this.name.compareTo(o.name);return this.age-o.age;}}

      三、Map:一次添加一对元素。Collection 一次添加一个元素。Map也称为双列集合,Collection集合称为单列集合。其实map集合中存储的就是键值对。 map集合中必须保证键的唯一性。
1,主要方法:1)添加。 value put(key,value):返回前一个和key关联的值,如果没有返回null.

2)删除。void  clear():清空map集合。   value remove(key):根据指定的key翻出这个键值对。

3)判断。boolean containsKey(key):   boolean containsValue(value):   boolean isEmpty();

4)获取。 value get(key):通过键获取值,如果没有该键返回null。当然可以通过返回null,来判断是否包含指定键。

int size(): 获取键值对的个数。

Collection values(); 返回此映射中包含的值的 Collection 视图。

Set  keySet();返回此映射中包含的键的 Set 视图。通过keySet方法获取map中所有的键所在的Set集合,在通过Set的迭代器获取到每一个键,在对每一个键通过map集合的get方法获取其对应的值即可完成对Map中所有元素的遍历。

Set entrySet();该方法将键和值的映射关系作为对象存储到了Set集合中,而这个映射关系的类型就是Map.Entry类型。

package itcast;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Set;public class MapDemo {public static void main(String[] args) {Map<Integer,String> map = new HashMap<Integer,String>();Set<Map.Entry<Integer, String>> entrySet = map.entrySet();Iterator<Map.Entry<Integer, String>> it = entrySet.iterator();while(it.hasNext()){Map.Entry<Integer, String> me = it.next();Integer key = me.getKey();String value = me.getValue();System.out.println(key+"::::"+value);}}}

2,常用子类:HashTable、HashMap、TreeMap

HashTable:内部结构是哈希表,是同步的,不支持空作为键或者值

Properties:是Hashtable的子类,用来存储键值对型的配置文件的信息,可以和IO技术相结合。

HashMap:内部结构是哈希表,不是同步的

TreeMap:内部结构是二叉树,可以对键进行排序

---------------------- ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ----------------------

详细请查看:http://edu.csdn.net

原创粉丝点击