map架构:
如上图:
(1)map是映射接口,map中存储的内容是键值对(key-value)
(2)abstractmap是继承于map的抽象类,实现了map中的大部分api。
(3)sortedmap是继承于map的接口,sortedmap中的内容是排序的键值对,排序的方法是通过比较器。
(4)navigablemap继承于sortedmap,其中有一系列的导航方法,如“获取大于或等于某对象的键值对”等等
(5)treemap继承于abstractmap和navigablemap接口,因此treemap中的内容是有序的键值对。
(6)hashmap继承于abstractmap,内容也是键值对,但不保证次序。
(7)weakhashmap继承于abstractmap,它和hashmap的键类型不同,weakhashmap是弱键。
(8)hashtable继承于directionary同时也实现了map,因此是键值对的,但不保证次序,同时是线程安全的。
总结:
hashmap是基于”拉链法“实现的散列表,一般用于单线程,键值都可以为空,支持iterator(迭代器)遍历
hashtable是基于”拉链法“实现的散列表,是线程安全的,可以用于多线程程序中。支持iterator(迭代器)遍历和enumeration(枚举器)两种遍历方式。
weakhashmap也是基于”拉链法“实现的散列表,同时是弱键
treemap 是有序的散列表,通过红黑树来实现的,键值都不能为空。
java8的map接口的源码:
public interface map<k,v> {
int size();//数目
boolean isempty();//判断是否为空
boolean containskey(object key);//判断是否含有某个key
boolean containsvalue(object value);//判断是否含有某个值
v get(object key);//通过key获得value
v put(k key, v value);//插入键值对
v remove(object key);//通过key删除
void putall(map<? extends k, ? extends v> m);//将一个map插入
void clear();//清空
set<k> keyset();//返回key集合
collection<v> values();//返回value
set<map.entry<k, v>> entryset();//实体集合,map的改变会影响到它
interface entry<k,v> {
k getkey();//获得key
v getvalue();//获得value
v setvalue(v value);//设置值
boolean equals(object o);//判断对象是否相等
int hashcode();//返回hashcode
//比较器,比较两个key
public static <k extends comparable<? super k>, v> comparator comparingbykey() {
return (comparator<map.entry<k, v>> & serializable)
(c1, c2) -> c1.getkey().compareto(c2.getkey());
}
//比较两个值
public static <k, v extends comparable<? super v>> comparator comparingbyvalue() {
return (comparator<map.entry<k, v>> & serializable)
(c1, c2) -> c1.getvalue().compareto(c2.getvalue());
}
//比较两个key
public static <k, v> comparator<map.entry<k, v>> comparingbykey(comparator<? super k> cmp) {
objects.requirenonnull(cmp);
return (comparator<map.entry<k, v>> & serializable)
(c1, c2) -> cmp.compare(c1.getkey(), c2.getkey());
}
//比较两个值
public static <k, v> comparator<map.entry<k, v>> comparingbyvalue(comparator<? super v> cmp) {
objects.requirenonnull(cmp);
return (comparator<map.entry<k, v>> & serializable)
(c1, c2) -> cmp.compare(c1.getvalue(), c2.getvalue());
}
}
//比较map是否相等
boolean equals(object o);
int hashcode();//hashcode
default v getordefault(object key, v defaultvalue) {
v v;
return (((v = get(key)) != null) || containskey(key))
? v
: defaultvalue;
}
default void foreach(biconsumer<? super k, ? super v> action) {
objects.requirenonnull(action);
for (map.entry<k, v> entry : entryset()) {
k k;
v v;
try {
k = entry.getkey();
v = entry.getvalue();
} catch(illegalstateexception ise) {
// this usually means the entry is no longer in the map.
throw new concurrentmodificationexception(ise);
}
action.accept(k, v);
}
}
default void replaceall(bifunction<? super k, ? super v, ? extends v> function) {
objects.requirenonnull(function);
for (map.entry<k, v> entry : entryset()) {
k k;
v v;
try {
k = entry.getkey();
v = entry.getvalue();
} catch(illegalstateexception ise) {
// this usually means the entry is no longer in the map.
throw new concurrentmodificationexception(ise);
}
// ise thrown from function is not a cme.
v = function.apply(k, v);
try {
entry.setvalue(v);
} catch(illegalstateexception ise) {
// this usually means the entry is no longer in the map.
throw new concurrentmodificationexception(ise);
}
}
}
default v putifabsent(k key, v value) {
v v = get(key);
if (v == null) {
v = put(key, value);
}
return v;
}
//删除某个key和value对应的对象
default boolean remove(object key, object value) {
object curvalue = get(key);
if (!objects.equals(curvalue, value) ||
(curvalue == null && !containskey(key))) {
return false;
}
remove(key);
return true;
}
//将某个key和oldvalue对应的值替换为newvalue
default boolean replace(k key, v oldvalue, v newvalue) {
object curvalue = get(key);
if (!objects.equals(curvalue, oldvalue) ||
(curvalue == null && !containskey(key))) {
return false;
}
put(key, newvalue);
return true;
}
//替换key的值
default v replace(k key, v value) {
v curvalue;
if (((curvalue = get(key)) != null) || containskey(key)) {
curvalue = put(key, value);
}
return curvalue;
}
default v computeifabsent(k key,
function<? super k, ? extends v> mappingfunction) {
objects.requirenonnull(mappingfunction);
v v;
if ((v = get(key)) == null) {
v newvalue;
if ((newvalue = mappingfunction.apply(key)) != null) {
put(key, newvalue);
return newvalue;
}
}
return v;
}
default v computeifpresent(k key,
bifunction<? super k, ? super v, ? extends v> remappingfunction) {
objects.requirenonnull(remappingfunction);
v oldvalue;
if ((oldvalue = get(key)) != null) {
v newvalue = remappingfunction.apply(key, oldvalue);
if (newvalue != null) {
put(key, newvalue);
return newvalue;
} else {
remove(key);
return null;
}
} else {
return null;
}
}
default v compute(k key,
bifunction<? super k, ? super v, ? extends v> remappingfunction) {
objects.requirenonnull(remappingfunction);
v oldvalue = get(key);
v newvalue = remappingfunction.apply(key, oldvalue);
if (newvalue == null) {
// delete mapping
if (oldvalue != null || containskey(key)) {
// something to remove
remove(key);
return null;
} else {
// nothing to do. leave things as they were.
return null;
}
} else {
// add or replace old mapping
put(key, newvalue);
return newvalue;
}
}
default v merge(k key, v value,
bifunction<? super v, ? super v, ? extends v> remappingfunction) {
objects.requirenonnull(remappingfunction);
objects.requirenonnull(value);
v oldvalue = get(key);
v newvalue = (oldvalue == null) ? value :
remappingfunction.apply(oldvalue, value);
if(newvalue == null) {
remove(key);
} else {
put(key, newvalue);
}
return newvalue;
}
}
以上就是java集合之map的示例代码详解的详细内容。