浅谈集合框架三、Map常用方法及常用工具类

最近刚学完集合框架,想把自己的一些学习笔记与想法整理一下,所以本篇博客或许会有一些内容写的不严谨或者不正确,还请大神指出。初学者对于本篇博客只建议作为参考,欢迎留言共同学习。

  之前有介绍集合框架的体系概念(http://www.cnblogs.com/yjboke/p/8761195.html),本篇介绍一下Map的常用方法及常用工具类。


map基本查询方法:

  增加:put、putAll;

  删除:remove(输如键进行删除)、clear(删除集合所有元素)

  查询:get(输入键查询值)、size(查询集合长度)、values(查询所有值)

  判断:containsKey(判断键是否存在)、containsValue(判断值是否存在)、isEmpty(判断集合是否为null)

  具体操作方式可参考下方代码:

HashMap<Integer, String> map = new HashMap<Integer, String>();

        map.put(1001, "zhangsan");
map.put(1003, "lisi");
map.put(1002, "wangwu");
map.put(1004, "maliu");
map.put(1004, "houqi");
map.put(null, "ainiyou"); //添加元素,可添加重复的键,但值会覆盖原所对应的值,可添加null,但只能有1个null。 System.out.println(map); //输出:{null=ainiyou, 1001=zhangsan, 1002=wangwu, 1003=lisi, 1004=houqi}
System.out.println(map.remove(1002)); //删除键为1002的键值对,并输出值“wangwu”,如没有该键,则输出null
System.out.println(map);
System.out.println(map.get(1002)); //查询键所对应的值,如没有该键,则输出null。
System.out.println(map.get(null)); //可直接查询null,输出对应的值:“ainiyou”
System.out.println(map.size()); //输出集合长度
System.out.println(map.values()); //输出所有的值:[ainiyou, zhangsan, lisi, houqi]
System.out.println(map.containsKey(1002)); //判断键是否存在,返回boolean
System.out.println(map.containsValue("lisi")); //判断值是否存在,返回boolean
map.clear(); //删除所有元素
System.out.println(map.isEmpty()); //判断是否为null,现已被清空,返回true

  基本操作方式如上,但是map集合不可直接使用迭代器输出元素,如果有需求的同学可以参考以下两种方法:

    keySet:

     Set<Integer> si = map.keySet();                                    //使用keySet方法获取所有的key,并存到Set集合中
for(Iterator<Integer> it = si.iterator(); it.hasNext();){ //将Set集合迭代输出
Integer key = it.next(); //获取key
String value = map.get(key); //获取key所对应的value
System.out.println("key:"+key + "; value :"+ value); //输出key和value
}

    entry:

  Set<Map.Entry<Integer, String>> entrySet = map.entrySet();                                //定义entrySet方法的set集合,去除key和value值
for(Iterator<Map.Entry<Integer, String>> it = entrySet.iterator(); it.hasNext(); ){ //使用迭代器取出key和value值
Map.Entry<Integer, String> me = it.next(); //将值存入me中
Integer key = me.getKey(); //分别取出
String value = me.getValue();
System.out.println("key:"+key + "; value :"+ value); //输出key和value
}

    以上两种方式的输出结果都为:

key:null; value :ainiyou
key:1001; value :zhangsan
key:1003; value :lisi
key:1004; value :houqi

  创建一个自定义类Book,使用上述方法进行输出,请见下方代码:

import java.util.*;
public class MapBookTest { public static void main(String[] args) {
Map<Book, String> bookMap = new HashMap<Book, String>();
bookMap.put(new Book(50, "1明朝那些事"), "京东");
bookMap.put(new Book(39, "2解忧杂货店"), "天猫");
bookMap.put(new Book(48, "3白夜行"), "当当网");
bookMap.put(new Book(59, "4官场笔记"), "亚马逊");
bookMap.put(new Book(48, "5白夜行"), "当当网");
//entrySet方法
Set<Map.Entry<Book, String>> entrySet = bookMap.entrySet();
for(Iterator<Map.Entry<Book, String>> it = entrySet.iterator(); it.hasNext();){
Map.Entry<Book, String> entry = it.next();
Book key = entry.getKey();
String value = entry.getValue();
System.out.println(key + ",购买自"+value);
}
//keySet方法
Set<Book> sb = bookMap.keySet();
for(Iterator<Book> it = sb.iterator(); it.hasNext();){
Book key = it.next();
String value = bookMap.get(key);
System.out.println(key + ",购买自"+value);
}
}
}
//实现接口让其具备比较性
class Book implements Comparable<Book>{ private String name;
private Integer price; public Book(Integer price,String name) {
super();
this.name = name;
this.price = price;
}
public void setPrice(Integer price){
this.price = price;
}
public int getPrice(){
return price;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public String toString(){
return name + "的价格为:"+price;
} //复写hashCode
public int hashCode(){
final int prime = 31;
int result = 1;
result = prime * result + price;
result = prime * result +name.hashCode()*result;
return result;
} //复写equals
public boolean equals(Object obj){
if(!(obj instanceof Book)){
throw new ClassCastException("请确认格式");
}
Book book = (Book)obj;
return book.name.equals(this.name) && book.price == this.price;
}
//复写比较器
public int compareTo(Book b){
int num = new Integer(this.price).compareTo(new Integer(b.price));
if(num == 0){
return this.name.compareTo(b.name);
}
return num;
}
}

因为HashMap是无序的,想要排序可将HashMap改为TreeMap,即可实现排序功能。如下方代码,以价格进行排序:

Map<Book, String> bookMap = new TreeMap<Book, String>();
bookMap.put(new Book(50, "明朝那些事"), "京东");
bookMap.put(new Book(39, "解忧杂货店"), "天猫");
bookMap.put(new Book(48, "白夜行"), "当当网");
bookMap.put(new Book(59, "官场笔记"), "亚马逊");
bookMap.put(new Book(48, "白夜行"), "当当网");
//entrySet方法
Set<Map.Entry<Book, String>> entrySet = bookMap.entrySet();
for(Iterator<Map.Entry<Book, String>> it = entrySet.iterator(); it.hasNext();){
Map.Entry<Book, String> entry = it.next();
Book key = entry.getKey();
String value = entry.getValue();
System.out.println(key + ",购买自"+value);
}

输出结果为:

解忧杂货店的价格为:39,购买自天猫
白夜行的价格为:48,购买自当当网
明朝那些事的价格为:50,购买自京东
官场笔记的价格为:59,购买自亚马逊
上一篇:SQL Server 2008 (R2) 单机版安装的先决条件


下一篇:[POI2006]ORK-Ploughing