Map概述
该集合存储键值对。一对一对往里存。而且要保证键的唯一性。
添加元素:
put(Object key,Object value)添加元素,如果出现添加时,相同的键。那
么后添加的值会覆盖原有的键值对应值。put方法会返回被覆盖的值。
pubAll(Map<? extends K,? extends V>m)
删除元素:
clear();
remove(Object key);返回remove掉的value
判断元素:
containsKey(Object key)此映射包含指定键的映射关系;
containsValue(Object value);
isEmpty();
获取元素:
get(Object key)返回value,可以通过get方法的返回值来判断一个键是
否存在。通过返回null来判断。
size();获取长度;
values();所有值拿出来;
entrySet();将map集合中的映射关系存入到了Set集合中,而这个关系的 数据类型就是Map.Entry。返回的是Set<Map.Entry<k,v>>,
keySet();将map中所有的键存入Set集合。因为Set集合具备迭代器,所以可以迭代方式取出所有的键,再根据get方法。获取每一个键对应的值。
Map子类对象特点
和Set很像。Set底层就是使用了Map集合。
Hashtable:底层是哈希表数据结构,不可以存入null键null值。该线程是同步的。效率低JDK1.0,用作键的对象必须实现hashCode方法和equals方法
HashMap:底层是哈希表数据结构,允许使用null值和null键,该集合是不同步的。效率高JDK1.2
TreeMap:底层是二叉树结构。线程不同步。可以用于给map集合中的键进行排序。
Map-keySet
Map<String,String> map = new HashMap<String,String>();
map.put("02", "张三2");
map.put("03", "张三3");
map.put("01", "张三1");
map.put("04", "张三4");
//先获取map集合中的所有键的Set集合,keySet();
Set<String> keySet = map.keySet();
//有了Set集合。就可以获取其迭代器。
Iterator<String> it = keySet.iterator();
while(it.hasNext()){
String key = it.next();
String value = map.get(key);
System.out.println("key:" + key +", value:"+value);
}
Map-entrySet
//将Map集合中的映射关系取出。存入到Set集合中
Set<Map.Entry<String, String>> entrySet = map.entrySet();
Iterator<Map.Entry<String, String>> it =entrySet.iterator();
while(it.hasNext()){
Map.Entry<String, String> me = it.next();
String key = me.getKey();
String value = me.getValue();
System.out.println(key + ":" + value);
}
Map练习
/*
学生属性:姓名、年龄
注意:姓名和年龄相同的视为同一个学生。
保证学生的唯一性。
1,描述学生。
2,定义map容器。将学生作为键,地址作为值。存入。
3,获取map集合中的元素
*/
class Student implements Comparable<Student> {
private String name;
private int age;
Student(String name,int age){
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String toString(){
return name + ":" + age;
}
public int hashCode(){
return name.hashCode()+age*34;
}
public boolean equals(Object obj){
if(!(obj instanceof Student))
throw new ClassCastException("类型不匹配!");
Student s = (Student)obj;
return this.name.equals(s.name) && this.age==s.age;
}
public int compareTo(Student s){
int num = new Integer(this.age).compareTo(new Integer(s.age));
if(num == 0)
return this.name.compareTo(s.name);
return num;
}
}
public class MapTest {
public static void main(String[] args) {
HashMap<Student,String> hm = new HashMap<Student,String>();
hm.put(new Student("lisi1",21), "北京");
hm.put(new Student("lisi2",22), "上海");
hm.put(new Student("lisi3",23), "南京");
hm.put(new Student("lisi4",24), "武汉");
//第一种取出方式 keySet
Set<Student> keySet = hm.keySet();
Iterator<Student> it = keySet.iterator();
while(it.hasNext()){
Student stu = it.next();
String addr = hm.get(stu);
System.out.println(stu+"..."+addr);
}
//第二种取出方式 entrySet
Set<Map.Entry<Student, String>> entrySet = hm.entrySet();
Iterator<Map.Entry<Student, String>> iter = entrySet.iterator();
while(iter.hasNext()){
Map.Entry<Student, String> me = iter.next();
Student stu = me.getKey();
String addr = me.getValue();
System.out.println(stu + "......."+addr);
}
}
}
TreeMap练习-字母出现的次数
/*
练习:
"gergjhioawehgihgrewapnvkd"获取该字符串中的字母出现的次数。
希望打印结果:a(1)c(2)....
通过结果发现,每一个字母都有对应的次数。
说明字母和次数之间都有次数关系。
注意:当发现有映射关系时,可以选择Map集合。
因为Map集合中存放的就是映射关系。
什么时候使用Map集合呢?
当数据之间存在着映射关系时,就要先想到Map集合。
思路:
1,将字符串转换成字符数组。因为要对每一个字母进行操作。
2,定义一个map集合,因为打印结果的字母有顺序,所以使用TreeMap集合。
3,遍历字符数组。
将每一个字母作为键去查map集合。
如果返回null,就将该字母和1存入map集合中。
如果返回不是null,说明该字母在map集合内已经存在,并有对应的次数,
那么就获取该次数并进行自增。然后将该字母和自增后的次数存入map集合中覆盖掉原来键所对应的值。
4,将map集合中的数据变成指定的字符串形式返回。
*/
public class MapTest {
public static void main(String[] args) {
System.out.println(charCount("afegre,a1df+-efefasaaa"));
}
public static String charCount(String str){
//字符串编程字符数组
char[] chs = str.toCharArray();
TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>();
int count = 0;
for(int x=0;x<chs.length;x++){
if(!(chs[x]>='a' && chs[x]<='z' || chs[x]>='A' && chs[x]<='Z'))
continue;
Integer value = tm.get(chs[x]);
if(value!=null)
count = value;
count++;
tm.put(chs[x], count);
count = 0;
/*if(value == null){
tm.put(chs[x], 1);
}else{
value = value+1;
tm.put(chs[x], value);
}*/
}
//System.out.println(tm);
StringBuilder sb = new StringBuilder();
Set<Map.Entry<Character,Integer>> entrySet = tm.entrySet();
Iterator<Map.Entry<Character,Integer>> it = entrySet.iterator();
while(it.hasNext()){
Map.Entry<Character, Integer> me = it.next();
Character ch = me.getKey();
Integer value = me.getValue();
sb.append(ch+"("+value+")");
}
return sb.toString();
}
}
------- android培训、java培训、期待与您交流! ----------