RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
JAVA8独有的map遍历方式(非常好用)

使用JAV8 带来的map遍历方式使遍历非常简单

创新互联是一家集网站建设,荆门企业网站建设,荆门品牌网站建设,网站定制,荆门网站建设报价,网络营销,网络优化,荆门网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

public class LambdaMap {

  private Map map = new HashMap<>();

  @Before
  public void initData() {
    map.put("key1", "value1");
    map.put("key2", "value2");
    map.put("key3", "value3");
    map.put("key4", 4);
    map.put("key5", 5);
    map.put("key5", 'h');
  }


  /**
   * 遍历Map的方式一
   * 通过Map.keySet遍历key和value
   */
  @Test
  public void testErgodicWayOne() {
    System.out.println("---------------------Before JAVA8 ------------------------------");
    for (String key : map.keySet()) {
      System.out.println("map.get(" + key + ") = " + map.get(key));
    }
    System.out.println("---------------------JAVA8 ------------------------------");
    map.keySet().forEach(key -> System.out.println("map.get(" + key + ") = " + map.get(key)));
  }

  /**
   * 遍历Map第二种
   * 通过Map.entrySet使用Iterator遍历key和value
   */
  @Test
  public void testErgodicWayTwo() {
    System.out.println("---------------------Before JAVA8 ------------------------------");
    Iterator> iterator = map.entrySet().iterator();
    while (iterator.hasNext()) {
      Map.Entry entry = iterator.next();
      System.out.println("key:value = " + entry.getKey() + ":" + entry.getValue());
    }
    System.out.println("---------------------JAVA8 ------------------------------");
    map.entrySet().iterator().forEachRemaining(item -> System.out.println("key:value=" + item.getKey() + ":" + item.getValue()));
  }

  /**
   * 遍历Map第三种
   * 通过Map.entrySet遍历key和value,在大容量时推荐使用
   */
  @Test
  public void testErgodicWayThree() {
    System.out.println("---------------------Before JAVA8 ------------------------------");
    for (Map.Entry entry : map.entrySet()) {
      System.out.println("key:value = " + entry.getKey() + ":" + entry.getValue());
    }
    System.out.println("---------------------JAVA8 ------------------------------");
    map.entrySet().forEach(entry -> System.out.println("key:value = " + entry.getKey() + ":" + entry.getValue()));
  }

  /**
   * 遍历Map第四种
   * 通过Map.values()遍历所有的value,但不能遍历key
   */
  @Test
  public void testErgodicWayFour() {
    System.out.println("---------------------Before JAVA8 ------------------------------");
    for (Object value : map.values()) {
      System.out.println("map.value = " + value);
    }
    System.out.println("---------------------JAVA8 ------------------------------");
    map.values().forEach(System.out::println); // 等价于map.values().forEach(value -> System.out.println(value));
  }

  /**
   * 遍历Map第五种
   * 通过k,v遍历,Java8独有的
   */
  @Test
  public void testErgodicWayFive() {
    System.out.println("---------------------Only JAVA8 ------------------------------");
    map.forEach((k, v) -> System.out.println("key:value = " + k + ":" + v));
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


分享题目:JAVA8独有的map遍历方式(非常好用)
网站路径:http://sczitong.cn/article/gephpe.html