Map集合

Collection是所有集合的父亲,但是Map,和Collection没有一点关联。Collection是单独存储的,而Map是以键值对成对出现存储的,在某些情况下,用Map集合会比Collection有更好的效果。用一个小例子来说明一下Map和Collection的具体区别:

需求:存储学生数据,根据学生ID找到学生。

学生类:

public class Student {
	private Integer id;
	private String name;
	public Student(Integer id, String name) {
		super();
		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 "Student [id=" + id + ", name=" + name + "]";
	}
}

Collection集合实现(ArrayList)

public class ListDemo {
	public static void main(String[] args) {
		List<Student> list = new ArrayList<Student>();
		list.add(new Student(1, "张三"));
		list.add(new Student(2, "李四"));
		list.add(new Student(3, "王五"));
		//需求,获取id为3的学生
		for (Student student : list) {
			if (student.getId()==3) {
				System.out.println(student);
			}
		}
	}
}
Map集合实现(HashMap)

public class MapDemo {
	public static void main(String[] args) {
		Map<Integer, Student> map = new HashMap<Integer, Student>();
		map.put(1, new Student(1, "张三"));
		map.put(2, new Student(2, "李四"));
		map.put(3, new Student(3, "王五"));
		//需求,获取id为3的学生信息
		System.out.println(map.get(3));
	}
}
上面打印出来的结果是:Student [id=3, name=王五]
虽然他们都实现了相同的效果,但是,可以发现,要取到id为3的学生,需要遍历整个List,而Map集合就方便多了,不用遍历,直接就可以取到。方便了很多。

    public V get(Object key) {
        Node<K,V> e;
        return (e = getNode(hash(key), key)) == null ? null : e.value;
    }
    final Node<K,V> getNode(int hash, Object key) {
        Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
        if ((tab = table) != null && (n = tab.length) > 0 &&
            (first = tab[(n - 1) & hash]) != null) {<span style="white-space:pre">			</span>//检验Map是否为空。
            if (first.hash == hash && // always check first node
                ((k = first.key) == key || (key != null && key.equals(k))))<span style="white-space:pre">	</span>//从头结点开始遍历
                return first;
            if ((e = first.next) != null) {
                if (first instanceof TreeNode)
                    return ((TreeNode<K,V>)first).getTreeNode(hash, key);
                do {
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))
                        return e;
                } while ((e = e.next) != null);
            }
        }
        return null;
    }
HashMap结果的遍历:

方式一:得到Key集合,然后根据key集合依次得到value,其中keySet()方法就是将Map中的key转换为集合

public class MapDemo {
	public static void main(String[] args) {
		Map<Integer, Student> map = new HashMap<Integer, Student>();
		map.put(1, new Student(1, "张三"));
		map.put(2, new Student(2, "李四"));
		map.put(3, new Student(3, "王五"));
		Set<Integer> keySet = map.keySet();		//得到key集合
		for (Integer integer : keySet) {
			System.out.println(map.get(integer));
		}
	}
}
方式二:将key和value作为一对,依次取出。

public class MapDemo {
	public static void main(String[] args) {
		Map<Integer, Student> map = new HashMap<Integer, Student>();
		map.put(1, new Student(1, "张三"));
		map.put(2, new Student(2, "李四"));
		map.put(3, new Student(3, "王五"));
		Set<Entry<Integer,Student>> entrySet = map.entrySet();	//得到Set视图
		for (Entry<Integer, Student> entry : entrySet) {
			System.out.println(entry);
		}
	}
}
两者方法差不多都是一样的。

除了HashMap之外,还有TreeMap,两者基本没什么区别,主要的区别在于,TreeMap是同步的,自然排序或者根据条件进行排序,和TreeSet相似。是基于红黑树来实现的。












  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值