java Map(HashMap)遍历方法

java Map(HashMap)遍历方法,第1张

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();
        }
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5082317.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-16
下一篇 2022-11-16

发表评论

登录后才能评论

评论列表(0条)

保存