栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java Map(HashMap)遍历方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java Map(HashMap)遍历方法

问题:对一个商品进行封装测试,添加商品编号和商品信息,商品信息包括商品编号  商品名称  商品类型  商品价格,然后对商品遍历

利用entrySet()和keySet()方法

Information类

public class Information {
    private String sno;
    private String type1;
    private String type2;
    private String type3;
    public Information(String sno, String type1, String type2, String type3) {
        super();
        this.sno = sno;
        this.type1 = type1;
        this.type2 = type2;
        this.type3 = type3;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((sno == null) ? 0 : sno.hashCode());
        result = prime * result + ((type1 == null) ? 0 : type1.hashCode());
        result = prime * result + ((type2 == null) ? 0 : type2.hashCode());
        result = prime * result + ((type3 == null) ? 0 : type3.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Information other = (Information) obj;
        if (sno == null) {
            if (other.sno != null)
                return false;
        } else if (!sno.equals(other.sno))
            return false;
        if (type1 == null) {
            if (other.type1 != null)
                return false;
        } else if (!type1.equals(other.type1))
            return false;
        if (type2 == null) {
            if (other.type2 != null)
                return false;
        } else if (!type2.equals(other.type2))
            return false;
        if (type3 == null) {
            if (other.type3 != null)
                return false;
        } else if (!type3.equals(other.type3))
            return false;
        return true;
    }
    public String getSno() {
        return sno;
    }
    public void setSno(String sno) {
        this.sno = sno;
    }
    public String getType1() {
        return type1;
    }
    public void setType1(String type1) {
        this.type1 = type1;
    }
    public String getType2() {
        return type2;
    }
    public void setType2(String type2) {
        this.type2 = type2;
    }
    public String getType3() {
        return type3;
    }
    public void setType3(String type3) {
        this.type3 = type3;
    }
    public Information() {
        super();
        // TODO Auto-generated constructor stub
    }
    @Override
    public String toString() {
        return "Information [sno=" + sno + ", type1=" + type1 + ", type2=" + type2 + ", type3=" + type3 + "]";
    }
    
}
测试类

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

public class MapTest2 {
    public static void main(String[] args) {
        // 利用Information类来实现可以方便查到该编号的物品名称,类型,价格
        //利用类实现想要输出信息即只要在普通类型调用方法的基础上加上toString()或者自定义的get()set()方法
        HashMap map = new HashMap<>();

        map.put("15404001", new Information("15404001","牙膏 ", "日用品 ", " 8.5"));
        map.put("15404002", new Information("15404002"," 牙刷 ", "日用品 ", " 4.5"));
        map.put("15404003", new Information("15404003"," 毛巾 ", "日用品 ", " 18.5"));
        map.put("15404004", new Information("15404004"," 鱼 ", "食品 ", " 12.5"));
        System.err.println(map.get("15404003").toString());
        //查找编号为15404003的价格
        // entrySet()
        System.out.println("利用entrySet()遍历");
        System.out.println("商品编号  商品名称  商品类型  商品价格");
        Set> eSet = map.entrySet();    
        for (Entry entry : eSet) {
            System.err.print(entry.getValue().getSno()+" ");
            System.err.print(entry.getValue().getType1()+" ");
            System.err.print(entry.getValue().getType2()+"");
            System.err.print(entry.getValue().getType3()+" ");
            System.err.println();
        }

        // keySet()返回的是key放到set集合里面
           System.out.println("利用KeySet()遍历");
        System.out.println("商品编号  商品名称  商品类型  商品价格");
        Set numSet = map.keySet();
        for (String string : numSet) {
            System.err.print(map.get(string).getSno()+" ");
            System.err.print(map.get(string).getType1()+" ");
            System.err.print(map.get(string).getType2()+"");
            System.err.print(map.get(string).getType3()+" ");
            System.err.println();
        }
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/425652.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号