000 list and using foreach map

A: list of use

1. Programs

package com.jun.it.java8;

import java.util.ArrayList;
import java.util.List;

public class Foreach8 {
    public static void main(String[] args) {
        foreachList();
    }

    //对list做foreach
    public static void foreachList(){
        List<User> list = new ArrayList<>();
        list.add(new User(1,"aaa"));
        list.add(new User(5,"ttt"));
        list.add(new User(3,"ccc"));
        list.forEach(user -> {
            user.setId(user.getId()+10);
        });
        System.out.println(list);
    }

    private static class User{
        Integer id;
        String name;
        public User(){}
        public User(Integer id, String name){
            this.id=id;
            this.name=name;
        }

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return "User{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    '}';
        }
    }



}

  effect:

  

 

Two: Use the map

1. Programs

package com.jun.it.java8;

import java.util.HashMap;
import java.util.Map;

public class ForeachMap {
    public static void main(String[] args) {
        foreachMap();
    }

    //对map做foreach
    public static void foreachMap(){
        Map<Integer,User> userMap = new HashMap<>();
        userMap.put(1,new User(1,"aaa"));
        userMap.put(5,new User(5,"ddd"));
        userMap.put(3,new User(3,"ccc"));
        userMap.forEach((k, v)->{
            System.out.println("k="+k+",v="+v);
        });
    }




    private static class User{
        Integer id;
        String name;
        public User(){}
        public User(Integer id, String name){
            this.id=id;
            this.name=name;
        }

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return "User{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    '}';
        }
    }
}

  effect:

  

 

Three: Review

1. Description

  Here is a description of the main previous traversal

 

2.entrySet method

package com.jun.it.java8;

import java.util.HashMap;
import java.util.Map;

public class ForDemo {
    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>();
        map.put("a",1);
        map.put("c",3);
        map.put("b",2);
        test1(map);
    }
    

    //entrySet
    public static void test1(Map<String,Integer> map){
        for (Map.Entry<String,Integer> entry : map.entrySet()){
            String key = entry.getKey();
            Integer value = entry.getValue();
            System.out.println("key="+key+",value="+value);
        }
    }
}

  

3.Iterator way

  I found the back of the same.

package com.jun.it.java8;

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

public class ForDemo {
    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>();
        map.put("a",1);
        map.put("c",3);
        map.put("b",2);
        test1(map);
    }


    //entrySet
    public static void test1(Map<String,Integer> map){
        for (Map.Entry<String,Integer> entry : map.entrySet()){
            String key = entry.getKey();
            Integer value = entry.getValue();
            System.out.println("key="+key+",value="+value);
        }
    }

    //Iterator接口
    public static void test2(Map<String,Integer> map){
        Iterator<Map.Entry<String, Integer>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<String, Integer> next = iterator.next();
        }
    }
}

  

4.keySet way

package com.jun.it.java8;

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

public class ForDemo {
    public static void main(String[] args) {
        Map<String, Integer> map = new HashMap<>();
        map.put("a",1);
        map.put("c",3);
        map.put("b",2);
        test1(map);
    }


    //entrySet
    public static void test1(Map<String,Integer> map){
        for (Map.Entry<String,Integer> entry : map.entrySet()){
            String key = entry.getKey();
            Integer value = entry.getValue();
            System.out.println("key="+key+",value="+value);
        }
    }

    //Iterator接口
    public static void test2(Map<String,Integer> map){
        Iterator<Map.Entry<String, Integer>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<String, Integer> next = iterator.next();
        }
    }

    //使用keySet
    public static void test3(Map<String,Integer> map){
        for (String key : map.keySet()){
            Integer value=map.get(key);
        }
    }
}

  

 

Guess you like

Origin www.cnblogs.com/juncaoit/p/11431423.html