MAP集合的遍历方法

好记性不如赖笔头……


*******************************实体类*******************************

package com.***.equalsDemo;

import java.util.Date;

public class Entity implements Comparable<Entity>{

    public Integer id;

    public String tableName;

    public Date createTime;

    public Integer getId() {
        return id;
    }

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

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    @Override
    public String toString() {
        return "entity [id=" + id + ", tableName=" + tableName + ", createTime=" + createTime + "]";
    }

    public Entity(Integer id, String tableName, Date createTime) {
        super();
        this.id = id;
        this.tableName = tableName;
        this.createTime = createTime;
    }

    public Entity() {
        super();
    }

    /**
     * 如果想在set集合中,对比存放对象的具体内容,须要重写equals
     */
    public boolean equals(Object obj) {
        if(obj instanceof Entity){
            Entity entity = (Entity)obj;
            if(entity.id == this.id && this.tableName != null && this.tableName.equals(entity.tableName) && (this.createTime.compareTo(entity.getCreateTime())) == 0){
                return true;
            }
        }
        return false;
    }

    /**
     * 还须要重写hashCode,因为equals在判断是否相等时,还会判断hashCode
     */
    public int hashCode() {
        return 100000;
    }

    /**
     * 如果想使用Arrays中的sort排序,必须重写compareTo方法,否则会报错
     */
    @Override
    public int compareTo(Entity o) {
        Integer val = this.id - o.id;
        if(val == 0 && this.tableName != null){
            val = this.tableName.compareTo(o.tableName);
            if(val == 0){
                val = this.createTime.compareTo(o.createTime);
            }
        }
        return val;
    }



}


*******************************测试类*******************************

package com.***.CollectionDemo;

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

import com.**.equalsDemo.Entity;

/**
 * @author Ckinghan
 *  @描述:Map集合的遍历方法
 */
public class MapDemo {

    public static void main(String[] args) {
        /**
         * 创建一个MAP集合,并填充数据
         * Map是无序的
         * map的key值必须唯一,
         * map的key可以为null,但只能一个
         * map的值可以为null,且可以有多个
         */
        Map<String, Entity> map = new HashMap<String,Entity>();
        map.put("A", new Entity(1, "AA", new Date()));
        map.put("B", new Entity(2, "AB", new Date()));
        map.put("C", new Entity(3, "AC", new Date()));
        map.put("D", new Entity(4, "AD", new Date()));
        map.put(null, null);

        /**
         * 将map集合的所有key值获取
         * 通过key值可以获取map的value
         */
        Set<String> set = map.keySet();
        for(String string : set){
            Entity entity = map.get(string);
            System.out.println("使用keySet获取所有的Key值,并遍历key值获取map的value值为:"+entity);
        }


        /**
         * 通过entrySet()方法获取map集合中的key与value
         */
        Set<Entry<String, Entity>> entries = map.entrySet();
        for(Entry<String, Entity> entry : entries){
            System.out.println("通过entrySet方法获取的key值为:"+entry.getKey()+";获取到的value值为:"+entry.getValue());
        }

    }

}

*******************************执行结果*******************************

使用keySet获取所有的Key值,并遍历key值获取map的value值为:null
使用keySet获取所有的Key值,并遍历key值获取map的value值为:entity [id=4, tableName=AD, createTime=Wed May 10 16:07:15 CST 2017]
使用keySet获取所有的Key值,并遍历key值获取map的value值为:entity [id=1, tableName=AA, createTime=Wed May 10 16:07:15 CST 2017]
使用keySet获取所有的Key值,并遍历key值获取map的value值为:entity [id=2, tableName=AB, createTime=Wed May 10 16:07:15 CST 2017]
使用keySet获取所有的Key值,并遍历key值获取map的value值为:entity [id=3, tableName=AC, createTime=Wed May 10 16:07:15 CST 2017]
通过entrySet方法获取的key值为:null;获取到的value值为:null
通过entrySet方法获取的key值为:D;获取到的value值为:entity [id=4, tableName=AD, createTime=Wed May 10 16:07:15 CST 2017]
通过entrySet方法获取的key值为:A;获取到的value值为:entity [id=1, tableName=AA, createTime=Wed May 10 16:07:15 CST 2017]
通过entrySet方法获取的key值为:B;获取到的value值为:entity [id=2, tableName=AB, createTime=Wed May 10 16:07:15 CST 2017]
通过entrySet方法获取的key值为:C;获取到的value值为:entity [id=3, tableName=AC, createTime=Wed May 10 16:07:15 CST 2017]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值