Map 接口是实现了类的键值对形式存储数据(key-value)
Map接口有HashMap和TreeMap子类
HashMap 是数据结构的哈希表方式的结构。
TreeMap 是数据结构的二叉树结构.什么是二叉树呢?主要就是一个元素下面只有两个元素。
Map类中存储的是键值对的标识,所以键不能重复。
Map中键值对的比较是通过equals和hashCode方法,重写equals方法必须要重写hashCode方法。
package com.study;
import java.util.*;
public class MapDemo01 {
public static void main(String []args) {
Map hash = new HashMap();
Map tree = new TreeMap();
/**
hash.put("zds1", new Integer(100));
hash.put("zds2", new Double(100.00));
hash.put("zds3", new Integer(200));
hash.put("zds4", 300);
tree.put("1", 100);
tree.put("2", new Boolean(true));
System.out.println(hash.get("zds4"));
System.out.println(hash.size());
System.out.println(hash.containsKey("zds1"));
System.out.println(hash.containsKey("zds11"));
System.out.println(hash.containsValue(new Integer(200)));
System.out.println(hash.containsValue(new Integer(2002)));
System.out.println(hash.put("zds3", new Integer(222)));//把新数据放入value中,在提取老数据的值返回
Map test = new HashMap(tree);
test.putAll(hash);
System.out.println(test);
*/
hash.put("map1", new MapDemo("zhudanshneg","100"));
hash.put("map1", new MapDemo("zhudanshneg","300")); //重复则以前的键的数据将替换掉
hash.put("map3", new MapDemo("zhudanshneg","400"));
System.out.println(hash);
System.out.println(tree.isEmpty());
}
}
class MapDemo {
private String name;
private String width;
public MapDemo(String name ,String width) {
this.name=name;
this.width=width;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWidth() {
return width;
}
public void setWidth(String width) {
this.width = width;
}
public String toString() {
return this.name +" "+this.width;
}
}
分享到:
相关推荐
集合框架包含collection和map的子类。其中collection包含list、set和queue。map包括hashmap、hashtable和treemap
Collection、Map、List、Set、Iterator
这是对集合的总结,可以帮助我们对集合更深入更清晰的认识。
java Collection&Map
Java Map 集合类简介,Collection,Map,ArrayList
Collection,Map,HashMap,hashTable,TreeMap,List,Vector,ArrayList的区别2008-03-30 13:27 这几天这个论坛还处在各个方法的设计阶段,也写了一部分,现在又遇到了这样的问题,怎么保存大量对象。。。的,查了...
Java容器有两种基本类型Collection 和 Map
Collection,List,Set和_Map用法和区别
Multi-map Generic Collection Class in C# - A Dictionary Collection Class that can Store Duplicate Key-value Pairs
下面小编就为大家带来一篇浅谈Java中常用数据结构的实现类 Collection和Map。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
关于Map,List,collection集合遍历,泛型等
Java集合Collection、List、Set、Map使用详解
NULL 博文链接:https://javazeke.iteye.com/blog/487275
Java集合排序及java集合类详解(Collection、List、Map、Set).doc
Java集合Collection、List、Set、Map使用详解
1. Map里存放的是bean时 <br><logic:iterate id="destMap" name="srcMap"><br> <bean:define id="bean" name="destMap" property="value" /><br> <bean:write name="bean" property="name" /><br> </...
Java集合排序及java集合类详解 (Collection, List, Set, Map)
Java集合排序及java集合类详解(Collection、List、Map、Set)
java集合的详解,collection,list,set,map疑难解答,对于对容器似懂非懂的新手来说,这无疑是给你指点迷津的一个非常不错的选择,讲解内容丰富。