——- android培训、java培训、期待与您交流! ———-
Map集合,与Collection集合的不同?
Map集合主要用来存放键-值对的,把键-值这一对映射存放进去。
(一)Map集合的常用方法:
1)put(key,alue)
putAll()添加
添加元素,当添加两个相同的键,后来的将覆盖原有值,并put(key,value)返回被覆盖的值。
2)clear清空集合
remove(key)删除集合中某一个元素
3)判断:
containsKey()是否存在某个键
containsValue()是否存在某个值
isEmpty()集合是否为空
4)获取
get(key)通过某个键获取对应的值
size()获取集合的大小
alues()获取集合中所有的值
5)集合中输出元素的两种方法。Map集合中取出原理:将集合转为Set集合,再利用迭代器一一取出。
keySet():返回Map集合中所有的键,返回到set集合中,而Set中有迭代器,可以一一拿到各个键,再根据get(key)可以拿到键所对应的值。
entrySet():将map中的映射关系取出,即Map.Entry(key,value),返回到Set
示例:学生(姓名和年龄相同视为同一个人)作为键,学生地址(String)作为值存入Map集合,并取出。
class Student implements Comparable
{
private String name;
private int age;
public Student(String name,int age)
{
this.name = name;
this.age = age;
}
public int hashCode()
{
return name.hashCode()+age*;
}
public boolean equals(Object obj)
{
if(Object instanceof Student)
throw new ClassCastException("类型不匹配")
Student s = (Student)obj;
return this.name.equals(s.name)&&this.age==s.age
}
public int compareTo(Student s)
{
num = this.name.compareTo(s.name);
if(num==)
return new Integer(this.age).compareTo(new Integer(s.age));
return num;
}
public int getAge()
{
return this.age;
}
public int getName()
{
return this.name;
}
public String toString()
{
return this.name+":::"+this.age;
}
}
public class MapTest
{
public static void main(String args[])
{
HashMap hm = new HashMap();
hm.add(new Student("lisi01",),"北京");
hm.add(new Student("lisi02",),"上海");
hm.add(new Student("lisi03",),"广州");
hm.add(new Student("lisi04",),"南京");
hm.add(new Student("lisi02",),"上海");
hm.add(new Student("lisi01",),"杭州");
Set set = hm.keySet();
Iterator it = set.iterator();
while(it.hasNext())
{
Person key = it.next();
String value = hm.get(key);
System.out.println(key.toString+"....."+value);
}
Set set2 = hm.entrySet();
Iterator it = set.iterator();
while(it.hasNext())
{
Map.Entry entry = it.next();
Person key = entry.getKey();
String value = entry.getValue();
System.out.println(key.toString+"....."+value);
}
}
}
(二)Map的常用子类有三个:
1) HashTable:底层用的数据结构为哈希表,是线程同步,不能存入空的键和值,jdk1.0,效率不高。
2) HashMap:底层用的数据结构为哈希表,是线程不同步,能存入null的键和值,jdk1.2,可以替代HashTable,效率较高。
3)TreeMap:底层用的二叉树数据结构,是线程不同步,能够对键进行排序。
其实Map与Set很像,因为Set底层实际上用的是Map。
示例2:利用TreeMap来实现学生按照年龄升序打印(指定比较器)。
首先定义比较器。
class MyComparator implements Comparator
{
public int compare(Student s1,Student s2)
{
int num = new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
if(num==)
return s1.getName().compareTo(s2.getName());
return num;
}
}
public class MapTest
{
public static void main(String args[])
{
HashMap hm = new HashMap(new MyComparator());
hm.add(new Student("lisi01",),"北京");
hm.add(new Student("lisi02",),"上海");
hm.add(new Student("lisi03",),"广州");
hm.add(new Student("lisi04",),"南京");
hm.add(new Student("lisi02",),"上海");
hm.add(new Student("lisi01",),"杭州");
Set set = hm.keySet();
Iterator it = set.iterator();
while(it.hasNext())
{
Person key = it.next();
String value = hm.get(key);
System.out.println(key.toString+"....."+value);
}
Set set2 = hm.entrySet();
Iterator it = set.iterator();
while(it.hasNext())
{
Map.Entry entry = it.next();
Person key = entry.getKey();
String value = entry.getValue();
System.out.println(key.toString+"....."+value);
}
}
}
示例3:“shfshfdlfhdoshfljeionfn”字符串中,每个字母出现的次数?打印格式为a(1)b(4)…..
class StringTimeDemo
{
public static void main(String args[])
{
String str = "shfshfdlfhdoshfljeionfn";
TreeMap hm = new TreeMap();
int num = ;
for(int i=;i<str.length();i++)
{
Char ch = str.charAt(i);
if(!(ch>'a'&&ch<'z'||ch>'A'&&ch<'Z'))
continue;
int value = hm.get(ch);
if(hm!=null)
num = value;
num++;
hm.put(ch,num);
num = ;
}
Set entry = hm.entrySet();
Iterator it = entry.iterator();
while(it.hasNext())
{
Map.Entry me = it.next();
Char ch = me.getKey();
int num = me.getValue();
System.out.println(ch+"("+num+")");
}
}
}
此篇文章为Map集合的简单介绍!