黑马程序员__集合

来源:互联网 发布:做网络销售的聊天技巧 编辑:程序博客网 时间:2024/06/05 06:53

黑马程序员——集合

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

1.集合类

   集合类主要负责保存、盛装其他数据,因此集合类也被称为容器类。

  Java的集合类主要由两个接口派生而出:Collection和Map.这两个接口是Java集合框架的根接口。

  Java集合框架:

 

  注意:Collection接口是在整个Java类集中保存单列值的最大的操作父接口,此集合里每次操作的时候都只能保存一个对象的数据;

             Map是真个Java类中保存双列值(具有映射关系的数据)的最大操作父接口,此集合里每次操作的都是一对对象并且都是以键值对的形式存在的。

             Iterator接口也是Java集合框架的成员,但它与Collection和Map系列的集合不一样:Collection和Map系列的集合用于盛装其他对象;而Iterator 则主要用于迭代Collection集合中的元素,被称为迭代器。

2.Collection:

  

   Collection接口是List和Set接口的父接口,该接口里定义的方法既可以用于操作List集合,也可以操作Set集合。

   代码示例:

  

<span style="font-family:FangSong_GB2312;font-size:14px;">import java.util.*;//集合中的共性方法class CollectionMethodDemo {public static void main(String[] args) {//创建集合对象ArrayList al1=new ArrayList();ArrayList al2=new ArrayList();ArrayList al3=new ArrayList();//向集合中添加元素al1.add("A");al1.add("B");al1.add("C");al1.add("D");al1.add("E");//将集合中的元素添加到指定的集合中al2.addAll(al1);al3.addAll(al1);//打印集合pri("集合1中的元素为:"+al1);pri("集合2中的元素为:"+al2);pri("集合3中的元素为:"+al3);//清空集合里的所有元素al2.clear();pri("集合2中的元素为:"+al2);//判断集合中是否包含指定元素pri("集合1中是否包含C:"+al1.contains("C"));//判断集合中是否包含指定集合中的所有元素pri("集合1中是否包含集合3中的所有元素:"+al1.containsAll(al3));//判断集合是否为空pri("集合2是否为空:"+al2.isEmpty());//删除集合中的指定元素al3.remove("A");pri("删除集合3中的A:"+al3);//从集合中删除指定集合中的所有元素al1.removeAll(al3);pri("从集合1中删除集合3中的所有元素:"+al1);//获取两个集合的交集al1.retainAll(al3);pri("集合1与集合3的交集:"+al1);//获取集合中的元素个数pri("集合3中的元素个数为:"+al3.size());//将集合转换为数组Object[] obj=al3.toArray();pri("转换之后的数组的长度为:"+obj.length);//用迭代器迭代集合Iterator iterator=al3.iterator();while(iterator.hasNext()){pri(iterator.next());}}private static void pri(Object obj){System.out.println(obj);}}</span>


  2.1 List :

      List集合代表一个元素有序并且可重复的集合,集合中的每个元素都有其对应的顺序索引。List集合允许使用重复元素,可以通过索引来访问指定位置的集合元素。List集合默认按照元素的添加顺序设置元素的索引。

      List的相关操作代码示例:

      

<span style="font-family:FangSong_GB2312;font-size:14px;">import java.util.*;class ListMethodDemo {public static void main(String[] args) {//创建List集合对象List list=new ArrayList();//向集合中添加元素list.add("A1");list.add("B2");list.add("C3");list.add("D4");list.add("E5");list.add("F6");//method(list);//用for循环来访问集合中的元素//formethod(list);//用foreach来访问集合中的元素//foreach(list);//用迭代器来迭代集合中的元素iterator(list);}//此方法中是List集合的特有操作方法private static void method(List list){//原集合pri("原集合中的元素为:"+list);//将元素插入到集合中的指定位置处list.add(3,"G7");pri("插入元素之后的结果为:"+list);//将集合插入到指定集合的指定位置处ArrayList lt=new ArrayList();lt.add("a1");lt.add("b2");lt.add("c3");list.addAll(5,lt);pri("插入集合之后的结果为:"+list);//获取指定索引处的集合元素pri("集合中1处的元素是:"+list.get(1));//获取指定元素在集合中第一次出现的位置pri("G7的位置是:"+list.indexOf("G7"));//获取指定元素在集合中最后一次出现的位置pri("a1在集合中最后一次出现的位置:"+list.lastIndexOf("a1"));//删除并返回指定位置处的元素pri("删除5处的元素:"+list.remove(5));pri("删除之后的集合是:"+list);//将指定位置处的元素替换list.set(6,"H8");pri("替换之后的集合是:"+list);//获取子集合List L=list.subList(3,7);pri("获取的子集合是:"+L);}private static void formethod(List list){for(int i=0;i<list.size();i++){pri("list<"+i+">="+list.get(i));}}private static void foreach(List list){for(Object obj:list){pri(obj);}}private static void iterator(List list){//获取迭代器对象ListIterator it=list.listIterator();//从前迭代while(it.hasNext()){pri(it.next());}pri("--------------------------");//从后迭代while(it.hasPrevious()){pri(it.previous());}}private static void pri(Object obj){System.out.println(obj);}}</span>

 2.1.1 ArrayList

   (1)底层数据结构使用的数组数据结构;

   (2)默认构造方法初始容量为10;

   (3)扩充算法:原来长度+原来长度>>1;

   (4)优点:元素的有序存储,利于遍历;

            缺点:删除和插入速度慢。

   (5)ArrayList是线程不安全的,性能较高。

   实例代码:

   

<span style="font-family:FangSong_GB2312;font-size:14px;">/*将自定义对象作为元素存入到ArrayList集合中,并去除重复元素  比如:存入人对象,同姓名同年龄视为同一个人,为重复元素。  思路:  (1)定义人类,将信息封装到对象中。  (2)创建集合对象,将数据存储到集合中。  (3)从集合中获取元素。*/import java.util.*;class ArrayListDemo2 {public static void main(String[] args) {//创建集合对象ArrayList list=new ArrayList();list.add(new Person("Apple",18));list.add(new Person("Lily",20));list.add(new Person("Joe",19));list.add(new Person("Apple",18));list.add(new Person("Eama",18));System.out.println("原集合为:");iterator_method(list);list=cancleSame(list);System.out.println("去除重复值为:");iterator_method(list);}private static void iterator_method(List list){//迭代集合Iterator it=list.iterator();while(it.hasNext()){System.out.println(it.next());}}private static ArrayList cancleSame(ArrayList list){//创建一个新的集合ArrayList al=new ArrayList();//迭代指定集合Iterator it=list.iterator();while(it.hasNext()){Object obj=it.next();if(!al.contains(obj))al.add(obj);}return al;}}//人类class Person{private String name;private int age;public Person(String name,int age){this.name=name;this.age=age;}public void setName(String name){this.name=name;}public String getName(){return name;}public void setAge(int age){this.age=age;}public int getAge(){return age;}//覆写equals方法public boolean equals(Object obj){if(obj instanceof Person){//类型转换Person p=(Person)obj;return this.getName().equals(p.getName()) && this.getAge()==p.getAge();}elsereturn false;}//覆写toString 方法public String toString(){return "name"+name+",age"+age;}}</span>


 

2.2.2 Vector

   (1)与ArrayList实现基本一致,初始容量为10;

   (2)扩充算法:原来长度+原来长度(或者指定长度);

   (3)Vector是线程安全的,性能较低。

   (4)除了支持Iterator .ListIterator 迭代输出之外,还支持Enumeration输出。

2.2.3 LinkedList:

   (1)底层数据结构使用的是链表数据结构;

  (2)优点:适合插入和删除操作;

          缺点:查询的速度慢;

  (3)LinkedList可以作为链表、队列和堆栈来使用。

  (4)LinkedList是线程不安全的。

   代码示例:

  

<span style="font-family:FangSong_GB2312;font-size:14px;">//用LinkedList的来模拟队列(先进先出)和堆栈(先进后出)import java.util.*;class LinkedListDemo{public static void main(String[] args) {//创建链表对象LinkedList link=new LinkedList();//队列Quenue q=new Quenue(link);q.Add("A01");q.Add("B02");q.Add("C03");q.Add("D04");q.Add("E05");//遍历while(!q.isNull()){//q.Get();System.out.println(q.Get());}//堆栈DuiZhan d=new DuiZhan(link);d.Add("A01");d.Add("B02");d.Add("C03");d.Add("D04");d.Add("E05");//遍历while(!d.isNull()){//q.Get();System.out.println(d.Get());}}}//队列class Quenue{//内部封装了一个链表的数据结构private LinkedList link;public Quenue(LinkedList link){this.link=link;}//添加元素的方法public void Add(Object obj){//内部调用的是链表的添加方法link.addFirst(obj);}//获取元素的方法public Object Get(){//内部调用的是链表的获取方法return link.removeLast();}//判断队列是否为空public boolean isNull(){return link.isEmpty();}}//堆栈class DuiZhan{//内部封装了一个链表的数据结构private LinkedList link;public DuiZhan(LinkedList link){this.link=link;}//添加元素的方法public void Add(Object obj){//内部调用的是链表的添加方法link.addFirst(obj);}//获取元素的方法public Object Get(){//内部调用的是链表的获取方法return link.removeFirst();}//判断队列是否为空public boolean isNull(){return link.isEmpty();}}</span>


 2.2 Set

   Set接口也是Collection的子接口,与List接口最大的不同在于:Set不允许有重复元素,允许存在Null值。Set接口并没有对Collection接口进行扩充,基本上还是与Collection接口保持一致。

  2.2.1 HashSet:

   HashSet是Set的实现子类,属于散列的存放类集,里面的内容是无序存放的。HashSet 按哈希算法来存储集合中的元素,因此具体很好的存取和查找性能。

   (1)HashSet底层使用的哈希表数据结构;

   (2)默认初始容量为16,默认的加载因子为0.75(表示容量存满75%,就要重新散列);

   (3)在哈希表中存储对象时,判断两个对象是同一个对象的方法:先比较两个对象的hashCode值是否相同,如果不同,就认为不是同一个对象;如果相同,在调用equals()方法判断。

   (4)当向HashSet集合中存入一个元素时,HashSet 会调用该对象的hashCode()来得到该对象的hashCode 值,然后根据该hashCode值决定该对象在HashSet中的存储位置。

   特点:

    a. 不能保证元素的排列顺序;

    b. HashSet 是线程不同步的;

    c. 集合元素可以是null。

  实例代码:

  

<span style="font-family:FangSong_GB2312;font-size:14px;">/*将自定义对象作为元素存入到HashSet集合中  比如:存入人对象,同姓名同年龄视为同一个人,为重复元素。  思路:  (1)定义人类,将信息封装到对象中。  (2)创建集合对象,将数据存储到集合中。  (3)从集合中获取元素。*/import java.util.*;class HashSetDemo{public static void main(String[] args) {HashSet set=new HashSet();set.add(new Person("Apple",19));set.add(new Person("Eama",10));set.add(new Person("Joe",19));set.add(new Person("Nuna",22));set.add(new Person("Eama",10));set.add(new Person("Apple",19));iterator_method(set);}private static void iterator_method(Set set){//迭代集合Iterator it=set.iterator();while(it.hasNext()){System.out.println(it.next());}}}//人类class Person{private String name;private int age;public Person(String name,int age){this.name=name;this.age=age;}public void setName(String name){this.name=name;}public String getName(){return name;}public void setAge(int age){this.age=age;}public int getAge(){return age;}//覆写hashCode()方法public int hashCode(){return name.hashCode()+age*39;}//覆写equals方法public boolean equals(Object obj){if(obj instanceof Person){//类型转换Person p=(Person)obj;//返回比较结果return this.getName().equals(p.getName()) && this.getAge()==p.getAge();}elsereturn false;}//覆写toString 方法public String toString(){return name+":"+age;}}</span>


 2.2.2 TreeSet

    TreeSet可以确保集合元素处于排序状态,其本身属于排序的子类。

   (1)TreeSet底层数据结构是二叉树;

   (2)如果想TreeSet集合中添加自定义对象时,则该对象所属的自定义类必须实现Comparable接口,覆写compareTo方法。如果两个对象通过compareTo方法比较相等时,新对象将无法添加到TreeSet集合中。

  注意:对于TreeSet集合而言,它判断两个对象是否相等的唯一标准就是:两个对象通过compareTo方法比较是否返回0.如果返回0,TreeSet集合会认为它们相等;否则就认为它们不相等。

  实例代码:

  

<span style="font-family:FangSong_GB2312;font-size:14px;">//用TreeSet存储学生对象,并按照年龄进行排序。import java.util.*;class TreeSetDemo{public static void main(String[] args) {//创建集合对象TreeSet<Student> set=new TreeSet<Student>();set.add(new Student("Apple",12));set.add(new Student("Joe",24));set.add(new Student("Apple",24));set.add(new Student("Apple",12));iterator_method(set);}private static void iterator_method(Set set){//迭代集合Iterator<Student> it=set.iterator();while(it.hasNext()){System.out.println(it.next());}}}//学生类:要进行自然排序,必须实现Comparable接口class Student implements Comparable<Student>{private String name;private int age;public Student(String name,int age){this.name=name;this.age=age;}public void setName(String name){this.name=name;}public String getName(){return name;}public void setAge(int age){this.age=age;}public int getAge(){return age;}//覆写compareTo方法,让元素能按照自然顺序输出public int compareTo(Student s ){//比较年龄,返回比较结果if(this.getAge()>s.getAge())return 1;if(this.getAge()==s.getAge()){return this.getName().compareTo(s.getName());}return -1;}public String toString(){return name+":"+age;}}</span>


  比较器

  

<span style="font-family:FangSong_GB2312;font-size:14px;">//用比较器实现TreeSet存储学生对象,并按照姓名进行排序。import java.util.*;class TreeSetDemo2{public static void main(String[] args) {//将自定义的比较器对象作为构造函数的参数TreeSet set=new TreeSet(new MyComparator());set.add(new Student("Apple",24));set.add(new Student("Joe",20));set.add(new Student("Lily",24));set.add(new Student("Eama",30));set.add(new Student("Apple",12));iterator_method(set);}private static void iterator_method(Set set){//迭代集合Iterator it=set.iterator();while(it.hasNext()){System.out.println(it.next());}}}//学生类class Student {private String name;private int age;public Student(String name,int age){this.name=name;this.age=age;}public void setName(String name){this.name=name;}public String getName(){return name;}public void setAge(int age){this.age=age;}public int getAge(){return age;}public String toString(){return name+":"+age;}}//定义比较器class MyComparator implements Comparator{ //覆写compare方法public int compare(Object obj1,Object obj2){if(!((obj1 instanceof Student) && (obj2 instanceof Student)))throw new RuntimeException("参数类型错误!");//类型转换Student s1=(Student)obj1;Student s2=(Student)obj2;//定义变量用来记录姓名的比较结果int result=s1.getName().compareTo(s2.getName());//姓名相同时比较年龄if(result==0)return s1.getAge()-s2.getAge();return result;}}</span>


3.  Iterator接口

   Iterator 接口也是Java集合框架中的成员,主要用于迭代Collection集合中的元素。Iterator的对象也被称为迭代器。

   Iterator接口里定义了如下三个方法:

         boolean HasNext():是否有下一个元素;

        Object next():返回集合里的下一个元素;

        void remove():删除当前元素。

  Iterator仅用于遍历集合,其本身并不提供盛装对象的能力。如果要创建Iterator对象,则必然有一个与之关联的Collection对象。

  注意:在使用Iterator迭代集合的时候,如果在进行迭代输出的时候要想删除当前元素,则只能使用Iterator接口中的remove()方法,而不能使用集合中的remove()方法,否则将出现ConcurrentModificationException。

  3.1 ListIterator接口

     ListIterator表示列表迭代器,只有List接口有。ListIterator提供了专门操作List集合的方法。

     此接口中定义了以下操作方法:

     void add(E e):增加元素;

     boolean hasPrevious():判断是否有前一个元素;

     E previous():取出前一个元素;

     void set(E e):修改元素的内容;

     int previousIndex():返回前一个索引;

     int nextIndex();返回下一个索引。

    注意:如果要想使用ListIterator接口,则必须依靠List接口进行实例化,通过List接口中定义的listIterator()方法。

   代码示例:

   

//获取迭代器对象ListIterator it=list.listIterator();//从前迭代while(it.hasNext()){pri(it.next());}pri("--------------------------");//从后迭代while(it.hasPrevious()){pri(it.previous());}

  3.2 Enumeration接口

    Enumeration是古老的输出接口,专门为Vector进行迭代输出。如果要想使用Enumeration输出的话,则必须使用Vector类,通过其elements()方法来完成。

      代码示例:

   

<span style="font-family:FangSong_GB2312;font-size:18px;">Vector v=new Vector();Enumeration e=v.elements();while(e.hasMoreElements()){Object obj=e.nextElement();}</span>

  4.  Map接口

  

   Map用于保存具有映射关系的数据,因此,Map集合里保存着两组值,一组值用于保存Map里的key,另外一组值用于保存Map里的value,key和value都可以是任何引用类型的数据。

   Map的key不允许重复,key和value之间存在单向一对一关系,即通过指定的key,总能找到唯一的、确定的value。

   Map的相关操作实例代码:

   

<span style="font-family:FangSong_GB2312;font-size:14px;">import java.util.*;class MapDemo {public static void main(String[] args) {//创建Map集合对象,并指定泛型Map<Integer,String> map=new HashMap<Integer,String>();//向集合中添加元素map.put(1,"Apple");map.put(2,"Joe");map.put(3,"Nuna");map.put(4,"Eama");//判断集合中是否包含指定的键print(map.containsKey(2));//判断集合中是否包含一个或者多个值print(map.containsValue("Joe"));//判断集合是否为空print(map.isEmpty());//删除指定键所对应的键值对print(map.remove(3));print(map);//获取集合中的键值对的个数print(map.size());//获取集合中所有值组成的集合Collection<String> col=map.values();print(col);//通过keySet()方法来获取集合中的每一个元素//获取所有键组成的set集合Set<Integer> set=map.keySet();//获取迭代器Iterator<Integer> it1=set.iterator();//获取元素while(it1.hasNext()){Integer key=it1.next();print("key="+key+",vaule="+map.get(key));}print("-------------------------");//通过entrySet()方法来获取集合中的每一个元素//获取映射关系Set<Map.Entry<Integer,String>> entry=map.entrySet();//获取迭代器Iterator<Map.Entry<Integer,String>> it2=entry.iterator();//获取元素while(it2.hasNext()){Map.Entry me=it2.next();print("key="+me.getKey()+",vaule="+me.getValue());}}private static <T> void print(T t){System.out.println(t);}}</span>

  4.1  HashMap

  HashMap是Map的子类,本身是属于无序存放的。

  (1)HashMap底层的数据结构是哈希表;

  (2)默认初始容量为16,默认加载因子为0.75;

   (3)不同步,线程不安全;

    (4)性能较高。

 4.2  Hashtable

  Hashtable是一个古老的Map实现子类,与HashMap是类似的。与HashMap的不同:

   (1)默认初始容量为11,默认加载因子为0.75;

   (2)从JDK1.0开始;

   (3)同步,线程安全的;

   (4)性能较低;

    (5)Hashtable中是不能插入null值的。

  注意:

      A:HashMap和Hashtable都是无序的;

      B:HashMap和Hashtable判断两个key相对的标准是:两个key通过equals()方法比较返回true,两个key的hashCode值也相等;

      C:HashMap和Hashtable判断两个value相对的标准是:只有两个对象通过equals()方法比较返回true即可;

      D:要在HashMap和Hashtable中存储对象,用作key的对象所属类必须实现hashCode()和equals()。

  4.3  TreeMap

   (1)TreeMap的底层数据结构是红黑树;

  (2)TreeMap子类其本身在操作的时候将按照key进行排序,key中的内容可以为任意的对象,但是要求对象所在的类必须实现Comparable接口,覆写compare方法;

  (3)TreeMap中判断两个key相等的标准是:两个key通过compareTo()方法返回0,TreeMap即认为这两个key是相等的。

  实例代码:

     

 <span style="font-family:FangSong_GB2312;font-size:14px;"> /*每一个学生都有一个对应的归属地。学生Student,地址String学生属性:姓名,年龄。注意:姓名和年龄相同的视为同一个学生。保证学生的唯一性。*/import java.util.*;class MapDemo2 { public static void main(String[] args)  {  Map<Student,String> map=new TreeMap<Student,String>(   new Comparator<Student>()   {    //实现compare()    public int compare(Student s1,Student s2)    {     int num=s1.getName().compareTo(s2.getName());     if(num==0)      return s1.getAge()-s2.getAge();     return num;    }   });  //添加元素  map.put(new Student("Apple",18),"NewYork");  map.put(new Student("Joe",20),"BeiJing");  map.put(new Student("Eama",19),"LongDou");  map.put(new Student("Paul",24),"Pairs");  map.put(new Student("Nuna",18),"Shouer");  //第一种方法获取元素  Iterator<Student> it1=map.keySet().iterator();  while(it1.hasNext())  {   Student s=it1.next();   System.out.println("name:"+s.getName()+",age:"+s.getAge()+",contry:"+map.get(s));  }  //第二种方法获取元素  Iterator<Map.Entry<Student,String>> it2=map.entrySet().iterator();;  while(it2.hasNext())  {   Map.Entry<Student,String> me=it2.next();   System.out.println("name---"+me.getKey().getName()+",age---"+me.getKey().getAge()+",contry---"+me.getValue());  } }}//学生类class Student implements Comparable<Student>{ private String name; private int age; public Student(String name,int age) {  this.name=name;  this.age=age; } public void setName(String name) {  this.name=name; } public String getName() {  return name; } public void setAge(int age) {  this.age=age; } public int getAge() {  return age; } //覆写hashCode()和equals(),compareTo() public int hashCode() {  return name.hashCode()+age; } public boolean equals(Object obj) {  if(!(obj instanceof Student))   throw new ClassCastException("类型不匹配!");  //类型转换  Student s=(Student)obj;  return this.getName().equals(s.getName())&&this.getAge()==s.getAge(); } public int compareTo(Student s) {  int num=this.getAge()-s.getAge();  if(num==0)   return this.getName().compareTo(s.getName());  return num; } }</span>

        5. Collections

            Collections是操作集合的工具类,其所有方法都是静态方法。该工具类里提供了大量方法对集合元素进行排序、查询和修改等操作,还提供了将集合对象设置为不可变、对集合对象实现同步控制等方法。

 以下是一些常用方法:            
 addAll(Collection c, T... elements):将所有指定元素添加到指定 collection 中。 
 binarySearch(Listlist, T key) :使用二分搜索法搜索指定列表,以获得指定对象。
 binarySearch(List list, T key, Comparatorc) :使用二分搜索法搜索指定列表,以获得指定对象。
 copy(List dest, List src) :将所有元素从一个列表复制到另一个列表。
 fill(Listlist, T obj) :使用指定元素替换指定列表中的所有元素。 
 ArrayListlist(Enumeration e) :返回一个数组列表,它按返回顺序包含指定枚举返回的元素。
 max(Collection coll) :根据元素的自然顺序,返回给定 collection 的最大元素。
 max(Collection coll, Comparatorcomp) : 根据指定比较器产生的顺序,返回给定 collection 的最大元素。
 min(Collection coll) :根据元素的自然顺序 返回给定 collection 的最小元素。
 min(Collectioncoll, Comparator comp) :根据指定比较器产生的顺序,返回给定 collection 的最小元素。 
 replaceAll(Listlist, T oldVal, T newVal):使用另一个值替换列表中出现的所有某一指定值。
 reverse(List list) :反转指定列表中元素的顺序。
 reverseOrder() :返回一个比较器,它强行逆转实现了 Comparable 接口的对象 collection 的自然顺序。
 reverseOrder(Comparator cmp) :返回一个比较器,它强行逆转指定比较器的顺序。 
 shuffle(List list) :使用默认随机源对指定列表进行置换。 
 sort(List list) :根据元素的自然顺序 对指定列表按升序进行排序。
 sort(List list, Comparator c) :根据指定比较器产生的顺序对指定列表进行排序。
 swap(List list, int i, int j) :在指定列表的指定位置处交换元素。
 synchronizedCollection(Collection c) : 返回指定 collection 支持的同步(线程安全的)collection。

 

总结:

<1> List的遍历方法

<span style="font-family:FangSong_GB2312;font-size:14px;">//第一种:for(int i=0;i<list.size();i++){System.out.println(list.get(i));}//第二种:for(Object obj:list){System.out.println(obj);}//第三种Iterator iter=list.iterator();while(iter.hasNext()){System.out.println(iter.next());}//第四种ListIterator lister=list.listIterator();//从前迭代while(lister.hasNext()){System.out.println(lister.next());}pri("--------------------------");//从后迭代while(lister.hasPrevious()){System.out.println(lister.previous());}//第五种Vector v=(Vector)list;Enumeration e=v.elements();while(e.hasMoreElements()){Object obj=e.nextElements();System.out.println(obj);}</span>


 

<2>Set集合的遍历方法

<span style="font-family:FangSong_GB2312;font-size:14px;">//第一种for(Object obj:set){System.out.println(obj);}//第二种Iterator iter=set.iterator();while(iter.hasNext()){System.out.println(iter.next());}</span>


 

<3>Map集合的遍历方法

<span style="font-family:FangSong_GB2312;font-size:14px;">//第一种Set<Map.Entry<K,V>> set=map.entrySet();Iterator<Map.Entry<K,V>> iter=set.iterator();while(iter.hasNext()){Map.Entry<K,V> entry=iter.next();System.out.println("key:"+entry.getKey()+",value:"+entry.getValue());}//第二种Set<K> keys=map.keySet();Iterator<K> iter=keys.iterator();while(iter.hasNext()){K key=iter.next();System.out.println("key:"+key+",vaule:"+map.get(key));}</span>


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


0 0
原创粉丝点击