A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© shw1072047958 中级黑马   /  2016-6-9 22:56  /  667 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

迭代器原理         由于多种集合的数据结构不同,所以存储方式不同,所以,取出方式也不同。
         这个时候,我们就把判断和获取功能定义在了一个接口中,将来,遍历哪种集合的时候,只要该集合内部实现这个接口即可。
迭代器源码
[java] view plain copy
print?

  • public interface Iterator  
  •                       {  
  •                                publicabstract boolean hasNext();  
  •                                publicabstract Object next();  
  •                       }  
  •   
  •                       publicinterface Collection  
  •                       {  
  •                                publicabstract Iterator iterator();  
  •                       }  
  •   
  •                       publicinterface List extends Collection  
  •                       {  
  •                                ...  
  •                       }  
  •                        
  •                       publicclass ArrayList implements List  
  •                       {  
  •                                publicIterator iterator()  
  •                                {  
  •                                          returnnew Itr();  
  •                                }  
  •   
  •                                privateclass Itr implements Iterator  
  •                                {  
  •                                          publicboolean hasNext(){...}  
  •                                          publicObject next(){...}  
  •                                }  
  •                       }  

      public interface Iterator                            {                                     publicabstract boolean hasNext();                                     publicabstract Object next();                            }                             publicinterface Collection                            {                                     publicabstract Iterator iterator();                            }                             publicinterface List extends Collection                            {                                     ...                            }                                                       publicclass ArrayList implements List                            {                                     publicIterator iterator()                                     {                                               returnnew Itr();                                     }                                      privateclass Itr implements Iterator                                     {                                               publicboolean hasNext(){...}                                               publicObject next(){...}                                     }                            }

Collection存储字符串和自定义对象并通过迭代器遍历                   1、存储字符串
[java] view plain copy
print?

  • Collectionc = new ArrayList();  
  • c.add("hello");  
  • c.add("world");  
  • c.add("java");  
  •   
  • Iteratorit = c.iterator();  
  • while(it.hasNext())  
  • {  
  •          Strings = (String)it.next();  
  •          System.out.println(s);  
  • }  

                            Collectionc = new ArrayList();                            c.add("hello");                            c.add("world");                            c.add("java");                             Iteratorit = c.iterator();                            while(it.hasNext())                            {                                     Strings = (String)it.next();                                     System.out.println(s);                            }
                   2、存储自定义对象(Student类的代码省略)
[java] view plain copy
print?

  • Collection c=newArrayList();  
  • Student s1=newStudent("林青霞",26);            
  • c.add("s1");  
  •   
  • Iteratorit=c.iterator();  
  • while(it.hasNext())  
  •    {  
  •      Strings=(String)it.next();  
  •     System.out.println(s);  
  •    }  

                            Collection c=newArrayList();                            Student s1=newStudent("林青霞",26);                                     c.add("s1");                                                 Iteratorit=c.iterator();                           while(it.hasNext())                              {                                Strings=(String)it.next();                               System.out.println(s);                              }
ListIterator迭代器是Iterator的子接口            所以List的遍历方式共有三种
                   1、Iterator迭代器
                   2、ListIterator迭代器
                   3、普通for+get()
五.Map map是一个键值对形式的集合。它的元素都是有键和值组成。Map的键(key)是唯一的,(value)可以重复。


Map的功能:                   A:添加功能
                                     V put(K key ,V value) :当key在集合中不存在是,添加元素;当key存在时替换元素
                   B:判断功能
                                     booleancontainsKey (Object key) :判断指定的键是否在集合中存在
                                     BooleancontainsValue(Object value):判断指定的值是否在集合中存在
                                     BooleanisEmpty() :判断集合是否为空
                   C:删除功能
                                     Voidclear():清除所有键值对数据
                   D:获取功能
                                     Objectget (Object key) :根据键获取值
                                     Set<K> keyset(): 所有键的集合
                                     Collection<V>values() :所有值的集合
                  E:长度功能
                                     Intsize()

Map包括HashMap、HashTable和TreeMap。其中,HashTable已基本被HashMap取代,这里不做讨论。Map遍历的两种方式:(导图上面已有,这里直接上代码了)键找值:
[java] view plain copy
print?

  • public static void main(String[] args) {  
  •          Map<String,Integer>map = new HashMap<String,Integer>();  
  •          map.put("二阳",23);  
  •          map.put("二峥",24);  
  •          map.put("二光",25);  
  •          Set<String> keys=map.keySet();              //把键其中起来,存入到set集合中.  
  •          for(Stringkey:keys){                                     //遍历键集合,获取每一个键。增强for  
  •                    Integervalue=map.get(key);            //让键去找值 get(Object key)  
  •                    System.out.println(key+"***"+value);  
  •          }  
  • }  

public static void main(String[] args) {         Map<String,Integer>map = new HashMap<String,Integer>();         map.put("二阳",23);         map.put("二峥",24);         map.put("二光",25);         Set<String> keys=map.keySet();              //把键其中起来,存入到set集合中.         for(Stringkey:keys){                                     //遍历键集合,获取每一个键。增强for                   Integervalue=map.get(key);            //让键去找值 get(Object key)                   System.out.println(key+"***"+value);         }}

键值对:
[java] view plain copy
print?

  • public static void main(String[] args) {  
  •          Map<String,Integer>map = new HashMap<String,Integer>();  
  •          map.put("二阳",23);  
  •          map.put("二峥",24);  
  •          map.put("二光",25);  
  •          
  •          Set<Map.Entry<String,Integer>> set=map.entrySet(); //获取键值对集合。Set<> entrySet()  
  •          for(Map.Entry<String,Integer> me:set){                                    //遍历键值对集合,获取到每一个键值对。增强for,迭代器  
  •                    Stringkey=me.getKey();                                                          //通过键值对获取键getKey()  
  •                    Integervalue=me.getValue();                                      //通过键值对获取值getValue()  
  •                    System.out.println(key+"***"+value);  
  •          }  
  • }  

public static void main(String[] args) {         Map<String,Integer>map = new HashMap<String,Integer>();         map.put("二阳",23);         map.put("二峥",24);         map.put("二光",25);                 Set<Map.Entry<String,Integer>> set=map.entrySet(); //获取键值对集合。Set<> entrySet()         for(Map.Entry<String,Integer> me:set){                                    //遍历键值对集合,获取到每一个键值对。增强for,迭代器                   Stringkey=me.getKey();                                                          //通过键值对获取键getKey()                   Integervalue=me.getValue();                                      //通过键值对获取值getValue()                   System.out.println(key+"***"+value);         }}


3 个回复

倒序浏览
学习了,,,
回复 使用道具 举报
歇歇楼主分享 学习了
回复 使用道具 举报
看的格式脑袋晕, 虽然感觉大神, 但是格式不太好看啊,
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马