JAVA复习5(集合——集合的遍历 Iteratorforeach、Enumeration——HashMap、HashTable、LinkedHashMap——map的遍历)

集合的遍历 Iterator   foreach  掌握   Enumeration

 

List    Set    观察两个接口

 

迭代器输出 Iterator

 

 

Set接口 或者 List 接口 都存在Iterator方法 该方法返回一个Iterator接口,通过该接口可与获得迭代器

 

Iterator 的方法

1 public boolean hasNext() 判断集合中的下一个元素是否存在

 

2 public E next()  取得集合中的下一个元素

 

3 default void remove() 删除迭代器中元素


范例:实现迭代输出

package org.collection;

 

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

 

public class ListDemo01 {

 

     public static void main(String[] args) {

         

           List<String> all=new ArrayList<>();

           

           

           all.add("hello");

           

           all.add("world");

           

           all.add("haha");

           

 

          //1 声明迭代接口

           

           Iterator<String> iter=all.iterator();

           

           while(iter.hasNext()) {

                

                String str=iter.next();//如果存在元素 取出赋值给 str

                

                System.out.println(str);

           }

     }

}

 

 

 

2 Foreach 输出

 //2 foreach输出

           for(String str:all) {

                

                System.out.println(str);

           }  

 

 

3 枚举输出 Enumeration

Enumeration 也是一个古老的接口,和Vector 是一样的, Enumeration 主要就是为Vector服务的

观察Vector 类中的方法

public Enumeration<E> elements() ;返回值正好就是Enumeration Vector独有的方法,如果想使用该方法可以不转型 或者向下转型

 

Enumeration 方法有两个:

 

1 boolean hasMoreElements(); 判断下一个元素是否存在

 

2 E nextElement() 取得下一个元素

 

范例: 使用枚举输出 Enumeration ( 只为Vector服务) (了解)

 //3 枚举输出 Enumeration

           

           Enumeration<String> enm=all.elements(); //获得枚举输出接口

           

           while(enm.hasMoreElements()) {  //判断下一个元素是否存在

                

                String str=enm.nextElement();  //取出下一个元素

                

                System.out.println(str);

           }

           

 

 

Map 接口

 

之前学习的Collection 接口 都是顺序结构,在Colleciont接口中保存的所有对象都是单个对象,而Map是键值对的方式出现的

 

  Key     value

 

1             张三

 

张三        123456

 

Map集合中最大的父接口就是 Map 该接口的定义如下:

public interface Map<K,V>  K   V 使用的时候可以定义两个泛型 一个另一个是V

 

Map接口中的核心方法:

1  public V put(K key, V value)map集合中添加元素,k   v

 

2  public V get(Object key)  给定一个key 取出key对应的V

 

3 Set<Map.Entry<K,V>> entrySet(); 把一个map集合转换为Set集合

 

4 public boolean containsKey(Object key) 查询指定的key是否存在

 

5 public Set<K> keySet(); 其中的key 转换为set

 

6public  V remove(Object key) ; 根据指定的key删除元素

 

 

Map本身是一个接口,接口不能直接使用,实现类

 

1 HashMap

2 HashTable

3 LinkedHashMap

4 TreeMap

范例: 实现Map集合的应用(HashMap)

public static void main(String[] args) {

          // k 只能是String   v 只能是int

          Map<String, Integer> all=new HashMap<>();

         

          //通过put的方法添加元素到Map集合

          all.put("one", 1);

         

          all.put("two", 2);

         

          all.put("one", 3); //重复的key

         

          System.out.println(all);

     }

hashMap Key值是不能重复的, value是可以重复的, 如果出现了相同的key 之前的key 会被覆盖掉

范例:使用get方法取得map集合中的指定内容

System.out.println(all.get("one"));

 

 

使用HashMap put方法 get方法

 

Put存放 键值对  get  通过 key 取得对应的value

 

特点:其实对于put的方法也是存在返回值的 当存储的keymap集合中不存在则返回null  如果存在相同的key 返回上一个相同keyvalue

 

 

相关方法的使用:

 

1 删除

Remove

System.out.println(all.remove("noe"));

 

2 查找指定的key是否存在

System.out.println(all.containsKey("one"));

3 查找指定的value是否存在

System.out.println(all.containsValue(1));

 

 

2 HashTable

 

其实相对于HashMap也是一个古老的类,功能上和HashMap完全一样,但是区别是:

1 HashTable 线程安全  同步处理   效率低    key     value  都不允许为null

 

2 HashMap 非线程安全, 异步处理  效率高   key    value 都允许为null

 

面试题

 

3 LinkedHashMap

 

HashMap在集合的使用中是最常用的,但是它的特点是存放的数据是无序的,而如果现在想要存放的数据为有序的map集合 则可以使用LinkedHashMap完成 (有序  存放的顺序)

public static void main(String[] args) {

         

          Map<String, Integer> all=new LinkedHashMap<>();

          all.put("three", 3);

         

          all.put("two", 2);

         

          all.put("one", 1);

         

          System.out.println(all);

     }

 

LinkedHashMap 主要的实现形式基于链表实现的,既然是链表的保存,其保存的数据量一般不会太大,如果使用该结构的时候存储了大量的数据,则时间复杂度会大量的攀升

 

 

4 TreeMap  (树结构  二叉树)

 

TreeMap子类的特点:就是可以按照key进行排序,使用这个类的时候一定要配合一个接口:

 

Comparable 接口

TreeMap定义如下:

public class TreeMap<K,V> extends AbstractMap<K,V> implements NavigableMap<K,V>, Cloneable, Serializabl

 

范例: 实现TreeMap排序

public static void main(String[] args) {

         

          Map<String, Integer> all=new TreeMap<>();

          all.put("C", 3);

          all.put("B", 2);

          all.put("A", 1);

          System.out.println(all);

     }

 

练习: 自己定义一个类 作为key存储在TreeMap 实现key的排序

public static void main(String[] args) {

         

         

          Map<Person, Integer> all=new TreeMap<>();

    

          all.put(new Person("张三",20), 3);

         

          all.put(new Person("小强",999), 2);

         

          all.put(new Person("隔壁老王",89), 1);

         

          System.out.println(all);

     }

 

 

主要掌握 HashMap  LinkedHashMap  TreeMap  HashTable 了解其实现类的特点:在开发中根据实际的业务需求选择使用相应的实现类

 

map的遍历:

 

Map.Entry接口:

 static class Node<K,V> implements Map.Entry<K,V>

 

 

 

在Map.Entry接口中提供了如下方法:

 

1  public K getKey() 取得key

 

2   public V getValue() 取得value

 

通过之前提到一个方法 entrySet()

Public Set<Map.Entry<K,V>> entrySet()  返回值包含了 Map.entry

 

思考 Map集合中 key是不可重复的,treeMap 有序的  HashMap 是无序的 Set集合非常的相似 key底层和set实现的原理是一样的 所以实际上mapset是可以相互转换的

 

Map.entry 里面存放的是 key  value  最终遍历取值的也是通过map.entry 

 

范例: 实现Map集合的遍历

package org.map;

 

import java.util.HashMap;

import java.util.Hashtable;

import java.util.Iterator;

import java.util.Map;

import java.util.Set;

 

public class HashMapDemo {

 

     public static void main(String[] args) {

         

          // k 只能是String   v 只能是int

          Map<String, Integer> all=new HashMap<>();

         

          //通过put的方法添加元素到Map集合

          all.put("one", 1);

         

          all.put("two", 2);

         

          all.put("three", 3);

         

          //1 map集合转换为Set集合

         

          Set<Map.Entry<String, Integer>> set=all.entrySet();

         

          //2 遍历set集合

          Iterator<Map.Entry<String, Integer>> iter=set.iterator();

         

          //循环取出迭代器中的内容

          while(iter.hasNext()) {

              

               Map.Entry<String, Integer> me=iter.next();

              

               System.out.println(me.getKey()); //取得key的方法

              

               System.out.println(me.getValue());//取得value的方法

          }

     }

}

 

 

猜你喜欢

转载自blog.csdn.net/weixin_42923199/article/details/86675274
今日推荐