Map的基本用法
关于map的一些基本用法在这里有说明
集合的简单使用
在这里主要是记录一些容易忘的内容,算是积少成多吧
可以通过Map获取到3个视图
map的初始值
Map<String, String> hashMap = new HashMap<String, String>();
hashMap.put("001", "xiaoba");
hashMap.put("002", "xiaojiu");
hashMap.put("003", "xiaoshi");
键视图
//keySet():获取Map的键的集,返回Set集
Set<String> keys = hashMap.keySet();
System.out.println("键集:" + keys);
执行结果:
键集:[001, 002, 003]
值视图
// values():获取Map的值,返回Collection
Collection<String> values = hashMap.values();
System.out.println("值集合:" + values);
执行结果:
值集合:[xiaoba, xiaojiu, xiaoshi]
键值对视图
//entrySet():获取键值视图,返回Set集
Set<Map.Entry<String, String>> entrys = hashMap.entrySet();
System.out.println("键值视图:" + entrys);
执行结果:
键值视图:[001=xiaoba, 002=xiaojiu, 003=xiaoshi]
遍历,通过键值对视图
// 遍历,通过键值视图
System.out.println("遍历哈希表,通过键值视图");
for(Map.Entry<String, String> entry : hashMap.entrySet()){
System.out.println(entry.getKey() + " = " + entry.getValue());
}
注意:这里的值返回的是值对象的toString()的内容,即getValue()其实是返回值对象中toString方法的内容,如果想定义打印内容,可以重写值所属类的toString()方法
遍历,通过键key
// 遍历,通过key
System.out.println("遍历哈希表,通过key");
for(String key : hashMap.keySet()){
System.out.println(key + " = " + hashMap.get(key));
}
说明:如果要遍历Map的内容,建议是用键值对视图的方法来进行遍历,因为通过键key的方法遍历时,会有2次循环,get(Object key)中已经套用了一个循环了
修改toString方法,定义自定义类的值的打印
Student.class
class Student{
String name;
Integer age;
public Student(String name, Integer age){
this.name = name;
this.age = age;
}
}
未重写toString方法
public static void main(String[] args){
Map<String, Student> hashMap = new HashMap<String, Student>();
hashMap.put("001", new Student("xiaoba", 14));
hashMap.put("003", new Student("xiaosan", 16));
hashMap.put("002", new Student("xiaoer", 11));
System.out.println("遍历哈希表,通过键值视图");
for(Map.Entry<String, Student> entry : hashMap.entrySet()) {
System.out.println(entry.getKey() + " = " + entry.getValue());
}
System.out.println("遍历哈希表,通过key");
for(String key : hashMap.keySet()){
System.out.println(key + " = " + hashMap.get(key));
}
}
执行结果
遍历哈希表,通过键值视图
001 = com.zeroten.javales.collections.MapTest$Student@cc34f4d
002 = com.zeroten.javales.collections.MapTest$Student@17a7cec2
003 = com.zeroten.javales.collections.MapTest$Student@65b3120a
遍历哈希表,通过key
001 = com.zeroten.javales.collections.MapTest$Student@cc34f4d
002 = com.zeroten.javales.collections.MapTest$Student@17a7cec2
003 = com.zeroten.javales.collections.MapTest$Student@65b3120a
重写Student.class的toString()
class Student{
String name;
Integer age;
public Student(String name, Integer age){
this.name = name;
this.age = age;
}
@Override
public String toString(){
StringBuilder data = new StringBuilder();
data.append("Student(");
data.append(name);
data.append(", ");
data.append(age);
data.append(")");
return data.toString();
}
}
执行main函数结果:
遍历哈希表,通过键值视图
001 = Student(xiaoba, 14)
002 = Student(xiaoer, 11)
003 = Student(xiaosan, 16)
遍历哈希表,通过key
001 = Student(xiaoba, 14)
002 = Student(xiaoer, 11)
003 = Student(xiaosan, 16)
HashMap的另外2中初始化方式
- 通过赋值另一个Map的方式
HashMap<Object, Object> map2 = new HashMap<>(map1)
- 指定初始容量大小
HashMap<Object, Object> map2 = new HashMap<>(int initialCapacity)
判断键key或值value是否存在
hashMap.containsKey(Object key) // 判断key是否存在
hashMap.containsValue(Object key) // 判断value是否存在
Map<String, Student> hashMap = new HashMap<String, Student>();
hashMap.put("001", new Student("xiaoba", 14));
hashMap.put("003", new Student("xiaosan", 16));
hashMap.put("002", new Student("xiaoer", 11));
// 判断key是否存在
System.out.println("003键存在?" + hashMap.containsKey("003"));
System.out.println("004键存在?" + hashMap.containsKey("004"));
// 判断value是否存在
System.out.println(hashMap.containsValue(new Student("xiaosan", 16)));
003键存在?true
004键存在?false
false
关于containsValue方法比较的时候返回false的问题,是因为containsValue比较的是引用,并没有比较到值,如果要比较值的话,需要重写值所属的类的equals()方法,所以如果是自定义的类要比较值的话,就重写自定义类的equals()方法
TreeMap的使用
TreeMap是有序的(跟List的有序不一样),它是键key根据一定的规则进行排序的,即key必须要有排序器,排序器可以有2种定义方式
- 在创建TreeMap时指定相关排序器,即实现 Comparator 接口
public class MapTest {
static class XuHao1{
private Integer index;
public XuHao1(Integer index){
this.index = index;
}
@Override
public String toString(){
return "学号-" + index;
}
}
static class Sort implements Comparator{
private boolean isAsc = true;
public Sort(boolean isAsc){
this.isAsc = isAsc;
}
@Override
public int compare(Object o, Object t1) {
if(o == null || t1 == null)
return -1;
if(o instanceof XuHao1 && t1 instanceof XuHao1){
XuHao1 d1 = (XuHao1) o;
XuHao1 d2 = (XuHao1) t1;
if(d1.index > d2.index){
return isAsc? 1: -1;
}else if(d1.index > d2.index)
return 0;
else
return isAsc? -1: 1;
}
return -1;
}
}
public static Comparator getComparator(boolean isAsc){
return new Sort(isAsc);
}
public static void TreeMapTest1(){
// 可以通过 getComparator(boolean isAsc) 方法中的isAsc参数确定treeMap1是按照升序还是降序排序
TreeMap<XuHao1, String> treeMap1 = new TreeMap<XuHao1, String>(getComparator(false));
treeMap1.put(new XuHao1(3), "小三, 15");
treeMap1.put(new XuHao1(5), "小五, 17");
treeMap1.put(new XuHao1(1), "小一, 13");
treeMap1.put(new XuHao1(4), "小四, 12");
System.out.println(treeMap1);
}
public static void main(String[] args){
TreeMapTest1();
// HashMapTest();
}
}
执行结果如下
{学号-5=小五, 17, 学号-4=小四, 12, 学号-3=小三, 15, 学号-1=小一, 13}
- 键key对象所属的类实现了 Comparable 接口
public class TreeMapTest{
static class XuHao implements Comparable{
private Integer index;
public XuHao(Integer index){
this.index = index;
}
@Override
public String toString(){
return "学号-" + index;
}
@Override
public int compareTo(Object o) {
if(o != null && o instanceof XuHao){
XuHao data = (XuHao) o;
if(this.index > data.index)
return 1;
else if(this.index == data.index)
return 0;
else
return -1;
}
return -1;
}
}
public static void TreeMapTest(){
TreeMap<XuHao, String> treeMap = new TreeMap<XuHao, String>();
treeMap.put(new XuHao(3), "小三, 15");
treeMap.put(new XuHao(5), "小五, 17");
treeMap.put(new XuHao(1), "小一, 13");
treeMap.put(new XuHao(4), "小四, 12");
// 因为在键所属类XuHao中,compareTo比较了5,发现5已经存在于TreeMap中了,
// 即XuHao(5).compareTo(XuHao(5)) 是返回了0,所以以下语句是更新对应的值
// 如果将compareTo返回-1或者1,则语句是添加键值对
treeMap.put(new XuHao(5), "小wu, 19");
System.out.println(treeMap);
// 同更新一样的道理,remove(Object key)时,是没有调用equals方法的,直接是通过排序器compareTo返回为0的进行删除
treeMap.remove(new XuHao(3));
System.out.println(treeMap);
}
public static void main(String[] args){
TreeMapTest();
}
}
执行结果如下:
{学号-1=小一, 13, 学号-3=小三, 15, 学号-4=小四, 12, 学号-5=小wu, 19}
{学号-1=小一, 13, 学号-4=小四, 12, 学号-5=小wu, 19}