hashmap使用

hashmap作为dao对象存储数据库数据

list是把每一个数据库的字段都映射了,而hashmap则是唯一id:数据库字段作为key

hashmap遍历方式

java 复制代码
public class Main {

    //使用迭代器(Iterator)EntrySet
        public static void main(String[] args) {
            // 创建并赋值 HashMap
            Map<Integer, String> map = new HashMap();
            map.put(1, "Java");
            map.put(2, "JDK");
            map.put(3, "Spring Framework");
            map.put(4, "MyBatis framework");
            map.put(5, "Java");
            // 遍历
            Iterator<Map.Entry<Integer, String>> iterator = map.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<Integer, String> entry = iterator.next();
                System.out.println(entry.getKey());
                System.out.println(entry.getValue());
            }
        }

    //ForEach EntrySet
    @Test
    public void test1(){
        // 创建并赋值 HashMap
        Map<Integer, String> map = new HashMap();
        map.put(1, "Java");
        map.put(2, "JDK");
        map.put(3, "Spring Framework");
        map.put(4, "MyBatis framework");
        map.put(5, "Java");
        // 遍历
        for (Map.Entry<Integer, String> entry : map.entrySet()) {
            System.out.print(entry.getKey());
            System.out.print(entry.getValue());
        }
    }

    //ForEach KeySet
    @Test
    public void test2(){
        // 创建并赋值 HashMap
        Map<Integer, String> map = new HashMap();
        map.put(1, "Java");
        map.put(2, "JDK");
        map.put(3, "Spring Framework");
        map.put(4, "MyBatis framework");
        map.put(5, "Java");
        // 遍历
        for (Integer key : map.keySet()) {
            System.out.print(key);
            System.out.print(map.get(key));
        }
    }

    //Lambda
    @Test
    public void test3(){
        // 创建并赋值 HashMap
        Map<Integer, String> map = new HashMap();
        map.put(1, "Java");
        map.put(2, "JDK");
        map.put(3, "Spring Framework");
        map.put(4, "MyBatis framework");
        map.put(5, "Java中文社群");
        // 遍历
        map.forEach((key, value) -> {
            System.out.print(key);
            System.out.print(value);
        });
    }

    //Streams API 单线程
    @Test
    public void test4(){
        // 创建并赋值 HashMap
        Map<Integer, String> map = new HashMap();
        map.put(1, "Java");
        map.put(2, "JDK");
        map.put(3, "Spring Framework");
        map.put(4, "MyBatis framework");
        map.put(5, "Java中文社群");
        // 遍历
        map.entrySet().parallelStream().forEach((entry) -> {
            System.out.print(entry.getKey());
            System.out.print(entry.getValue());
        });
    }
}
相关推荐
a努力。4 分钟前
中国电网Java面试被问:Dubbo的服务目录和路由链实现
java·开发语言·jvm·后端·面试·职场和发展·dubbo
爬山算法4 分钟前
Hibernate(42)在Hibernate中如何实现分页?
java·后端·hibernate
不平衡的叉叉树8 分钟前
我们遇到了正则表达式的灾难性回溯问题
java·正则表达式
wangkay8820 分钟前
【Java 转运营】Day05:抖音新号起号:对标账号运营全指南
java·新媒体运营
大飞哥~BigFei34 分钟前
新版chrome浏览器安全限制及解决办法
java·前端·chrome·安全·跨域
{Hello World}41 分钟前
Java多态:三大条件与实现详解
java·开发语言
老蒋每日coding41 分钟前
Java解析Excel并对特定内容做解析成功与否的颜色标记
java·开发语言·excel
lang2015092842 分钟前
Java反射利器:Apache Commons BeanUtils详解
java·开发语言·apache
m0_7482459242 分钟前
SQLite 数据类型概述
java·数据库·sqlite