Map集合遍历键找值方法和Entry键值对儿对象

Map集合遍历键找值方法

键找值方式:即通过元素中的键,获取键所对应的值
分析步骤∶
  1.获取Map中所有的键,由于键是唯一的,所以返回一个Set集合存储所有的键。方法提示: keyset()

  2.遍历键的Set集合,得到每一个键。

  3.根据键,获取键所对应的值。方法提示: get(K key)

 

 

Map集合的第一种遍历方式:

通过键找值的方式Map集合中的方法:

  set<K> keyset()返回此映射中包含的键的Set视图。

实现步骤:
  1.使用Map集合中的方法keySet(),把Map集合所有的key取出来,存储到一个set集合中

  2.遍历set集合,获取Map集合中的每一个key

  3.通过Map集合中的方法get(key),通过key找到value

 public static void main(String[] args) {
        //创建Map集合对象
        Map<String, Integer> map = new HashMap<>();
        map.put("赵丽颖", 168);
        map.put("杨颖", 165);
        map.put("林志玲", 178);
        //1.使用Map集合中的方法keySet( ),把Map集合所有的key取出来,存储到一个set集合中
        Set<String> set = map.keySet();
        //遍历set集合,获取Mqp集合中的每一个key
        // /使用迭代器遍历set集合
        Iterator<String> it = set.iterator();
        while (it.hasNext()) {
            String key = it.next();
            //3.通过Map集合中的方法get( key),通过key找到vaLue
            Integer value = map.get(key ) ;
            System.out.println(key + "=" + value);
        }
        //使用增强for遍历set集合
        for(String key : set){
            //通过Mop集合中的方法get ( key),通过key找到value
            Integer value = map.get(key);
        System.out.println(key+"="+value);
    }
        System.out.println(" ------------");
        //使用增强for遍历set集合
        for(String key : map. keySet()){
        //通过Map集合中的方法get(key),通过key找到vaLue
            Integer value = map.get(key);
        System.out.println(key+"="+value) ;
         }
    }

 

public class Demo02_Iterator {

    /**
     * 通过查看Map集合的api发现没有iterator方法,那么双列集合如何迭代呢?
     * 根据键获取值
     */
    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>();
        map.put("张三", 23);
        map.put("李四", 24);
        map.put("王五", 25);
        map.put("赵六", 26);

//        Integer i = map.get("张三");                    //根据键获取值
//        System.out.println(i);

        //获取所有的键
        /*Set<String> keySet = map.keySet();        //获取所有键的集合
        Iterator<String> it = keySet.iterator();    //获取迭代器
        while(it.hasNext()) {                        //判断集合中是否有元素
            String key = it.next();                    //获取每一个键
            Integer value = map.get(key);            //根据键获取值
            System.out.println(key + "=" + value);
        }*/

        //使用增强for循环遍历
        for(String key : map.keySet()) {            //map.keySet()是所有键的集合
            System.out.println(key + "=" + map.get(key));
        }
    }
}

 

Entry键值对儿对象

我们已经知道,Map 中存放的是两种对象,一种称为key(键),一种称为value(值),它们在在Map中是对应关系,这一对对象又称做Map.中的一个Entry(项)。

Entry|将键值对的对应关系封装成了对象。

即键值对对象,这样我们在遍历Map集合时,就可以从每一个键值对(Entry])对象中获取对应的键与对应的值。

既然Entry表示了一对键和值,那么也同样提供了获取对应键和对应值得方法︰

  public K getKey():获取Entry对象中的键。

  public v getvalue()∶获取Entry对象中的值。在Map集合中也提供了获取所有Entry对象的方法∶

  public Set<Map.Entry<K ,v>> entrySet():获取到Map集合中所有的键值对对象的集合(Set集合)。

 

 

package com.Jinone.www;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;

import javax.swing.text.html.parser.Entity;

public class TextHashMap {
    public static void main(String[] args) {
        HashMap<String,Integer>hashmap=new HashMap<String, Integer>();
        hashmap.put("liu",18);
        hashmap.put("liu1",18);
        hashmap.put("liu2",18);
        hashmap.put("liu",18);
        Set<Entry<String,Integer>>entries=hashmap.entrySet();
        for(Entry<String,Integer>entry1:entries) {
            System.out.println(entry1.getKey()+entry1.getValue());
        }
}
}

 

 

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/*
     Map集合遍历的第二种方式:使用Entry对象遍历

    Map集合中的方法:
        Set<Map.Entry<K,V>> entrySet() 返回此映射中包含的映射关系的 Set视图
    实现步骤:
        1.使用Map集合中的方法entrySet(),把Map集合中多个Entry对象取出来,存储到一个Set集合中
        2.遍历Set集合,获取每一个Entry对象
        3.使用Entry对象中的方法getKey()和getValue()获取键与值
 */
public class Demo03EntrySet {
    public static void main(String[] args) {
        //创建Map集合对象
        Map<String, Integer> map = new HashMap<>();
        map.put("赵丽颖", 168);
        map.put("杨幂", 165);
        map.put("林志玲", 178);

        //1.使用Map集合中的方法entrySet(),把Map集合中多个Entry对象取出来,存储到一个Set集合中
        Set<Map.Entry<String, Integer>> set = map.entrySet();

        //2.遍历Set集合,获取每一个Entry对象
        //使用迭代器遍历Set
        Iterator<Map.Entry<String, Integer>> it = set.iterator();
        while (it.hasNext()) {
            Map.Entry<String, Integer> entry = it.next();
            //3.使用Entry对象中的方法getKey()和getValue()获取键与值
            String key = entry.getKey();
            Integer value = entry.getValue();
            System.out.println(key + "=" + value);
        }
        System.out.println("---------------------------");
        //使用增强for循环遍历
        for (Map.Entry<String,Integer> entry:set) {
            //3.使用Entry对象中的方法getKey()和getValue()获取键与值
            String key = entry.getKey();
            Integer value = entry.getValue();
            System.out.println(key + "=" + value);
        }
    }
}

 

posted @ 2022-07-06 16:58  漁夫  阅读(156)  评论(0)    收藏  举报