Map集合
Map集合:该集合存储键值对。一对一对往里存,而且要保证键的唯一性。
1,添加。
put(K key, V Value)
putAll(Map<? extends K,? extends V> m)
2,删除。
clear()
remove(Object key)
3,判断。
containsValue(Object value)
containsKey(Object key)
isEmpty()
4,获取。
get(Object key)
size()
value()
entrySet()
keySet()
Map
|--Hashtable:底层是哈希表数据结构,不可以存入null键null值。该集合是线程同步的。jdk1.0,效率低
|--HashMap:底层是哈希表数据结构,允许使用null值和null键,该集合是不同步的。jdk1.2,效率高
|--TreeMap:底层是二叉树数据结构,线程不同步。可以用于给map集合中的键进行排序。
和Set很像。
map集合的两种取出方式:
1,Set<k> keySet:将map中所有的键存入到Set集合,因为set具备迭代器。
所以可以迭代方式取出所有的键,在根据get方法,获取每一个键对应的值。
Map集合的取出原理:将map集合转成set集合。在通过迭代器取出。
2,Set<Map.Entry<k,v>> entrySet:将map集合中的映射关系存入到了set集合中,而这个关系的数据类型就是:Map.Entry
EntrySet方法图例
练习2:
练习3:
Map集合:该集合存储键值对。一对一对往里存,而且要保证键的唯一性。
1,添加。
put(K key, V Value)
putAll(Map<? extends K,? extends V> m)
2,删除。
clear()
remove(Object key)
3,判断。
containsValue(Object value)
containsKey(Object key)
isEmpty()
4,获取。
get(Object key)
size()
value()
entrySet()
keySet()
Map
|--Hashtable:底层是哈希表数据结构,不可以存入null键null值。该集合是线程同步的。jdk1.0,效率低
|--HashMap:底层是哈希表数据结构,允许使用null值和null键,该集合是不同步的。jdk1.2,效率高
|--TreeMap:底层是二叉树数据结构,线程不同步。可以用于给map集合中的键进行排序。
和Set很像。
其实Set集合底层就是使用了Map集合。
import java.util.*;
class MapDemo
{
public static void main(String[] args)
{
Map<String,String> map = new HashMap<String,String>();
//添加元素,添加元素,如果出现添加时,相同的键。那么后添加的值回复该原有键对应的值
//并put方法会返回覆盖后的值。
System.out.println("put:"+map.put("01","zhangsan1"));
System.out.println("put:"+map.put("01","wangwu"));
map.put("02","zhangsan2");
map.put("03","zhangsan3");
System.out.println("containsKey:"+map.containsKsy("022"));
//System.out.println("remove:"+map.remove("02"));
System.out.println("get:"+map.get("023"));
map.put("04",null);
System.out.println("get:"+map.get("04"));
//可以通过get方法的返回值来判断一个键是否存在,通过返回null来判断。
//获取map集合中所有的值。
Collection<String> coll = map.value();
System.out.println(coll);
System.out.println(map);
}
}
map集合的两种取出方式:
1,Set<k> keySet:将map中所有的键存入到Set集合,因为set具备迭代器。
所以可以迭代方式取出所有的键,在根据get方法,获取每一个键对应的值。
Map集合的取出原理:将map集合转成set集合。在通过迭代器取出。
2,Set<Map.Entry<k,v>> entrySet:将map集合中的映射关系存入到了set集合中,而这个关系的数据类型就是:Map.Entry
KeySet方法图例
EntrySet方法图例
import java.util.*;
class MapDemo2
{
public static void main(String[] args)
{
Map<String,String> map = new HashMap<String,String>();
map.put("01","zhangsan1");
map.put("02","zhangsan2");
map.put("03","zhangsan3");
map.put("04","zhangsan4");
//将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集合的所有键的Set集合,keySet();
Set<String> keySet = map.keySet();
//有了Set集合,就可以获取其迭代器。
//Iterator<String> it = keySet.iterator();
while(it.hasNext())
{
String key = it.next();
//有了键可以通过map集合的get方法获取其对应的值。
String value = map.get(key);
System.out.println("key:"+key+",value:"+value);
}
*/
}
}
/*
Map.Entry 其实Entry也是一个接口,它是Map接口中的一个内部接口。
*/
练习1:
/*
每一个学生都有对应的归属地。
学生Student,地址String。
学生属性:姓名,年龄。
注意:姓名和年龄相同的视为同一个学生。
保证学生的唯一性。
1,描述学生。
2,定义map容器。将学生作为键,地址作为值。存入。
3,获取map集合中的元素。
实现,覆盖两个方法,养成习惯!
*/
import java.util.*;
class Student implements Comparable<Student>
{
private String name;
private int age;
Student(String name,int age)
{
this.name = name;
this.age = 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 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 String getName();
{
return name;
}
public int getAge();
{
return age;
}
public String toString()
{
return name+":"+age;
}
}
class MapTest
{
public static void main(String[] args)
{
HashMap<Student,String> hm = new HashMap<Student,String>();
hm.put(new Student("lisi1",21),"beijing");
hm.put(new Student("lisi2",22),"shanghai");
hm.put(new Student("lisi3",23),"nanjing");
hm.put(new Student("lisi4",24),"wuhan");
//第一种取出方法 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> iter = entrySet.iterator();
Student stu = me.getKey();
String addr = me.getValue();
System.out.println(stu+"......"+addr);
}
}
}<span style="font-weight: bold; ">
</span>
练习2:
/*
需求:对学生对象的年龄进行升序排序。
因为数据是以键值对形式存在的。、
所以要使用可以排序的Map集合。TreeMap。
*/
import java.util.*;
class StuNameComparator implements Comparator<Student>
{
public int compare(Student s1,Student s2)
{
int num = s1.getName().compareTo(s2.getName());
if(num==0)
return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
return num;
}
}
class MapTest2
{
public static void main(String[] args)
{
TreeMap<Student,String> tm = new TreeMap<Student,String>(new StuNameComparator());
tm.put(new Student("blisi1",21),"beijing");
tm.put(new Student("lisi1",21),"tianjin");
tm.put(new Student("lisi2",22),"shanghai");
tm.put(new Student("alisi3",23),"nanjing");
tm.put(new Student("lisi4",24),"wuhan");
Set<Map.Entry<Student,String>> entrySet = tm.EntrySet();
Iterator<Map.Entry<Student,String>> it = entrySet.iterator();
while(it.hasNext())
{
Map.Entry<Student,String> me = it.next();
Student stu = me.getKey();
String addr = me.getValue();
System.out.println(stu+":::"+addr);
}
}
}
练习3:
/*
练习:
“sdfgzxcvasdfxcvdf”获取该字符串中字母出现的次数。
希望打印结果:a(1)c(2)....
通过结果发现,每一个字母都有对应的次数。
说明该字母和次数之间都有映射关系。
注意了,当发现有映射关系时,可以选择map集合。
因为map集合中存放就是映射关系。
为什么使用map集合呢?
当数据之间存在着映射关系时,就要先想map集合。
思路(见下图例):
1,将字符串转换成字符数组。因为要对每一个字母进行操作。
2,定义一个map集合,因为打印结果的字母有顺序,所以使用treemap集合。
3,遍历字符数组。
将每一个字母作为键去查找map集合。
如果返回null,将该字母和1存入到map集合中.
如果返回不是null,说明该字母在map集合已经存在并有对应次数。
那么就获取该次数并进行自增。然后将该字母和自增后的次数存入到map集合中,覆盖调用原值所对应的值。
4,将map集合中的数据变成指定的字符串形式返回。
*/
import java.util.*;
class MapTest3
{
<span style="white-space:pre"> </span>public static void main(String[] args)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>String s = charCount("sdfgzxcvasdfxcvdf");
<span style="white-space:pre"> </span>System.out.println(s);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public static String charCount(String str)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>char[] chs = str.toCharArray();
<span style="white-space:pre"> </span>TreeMap<Character,Integer> tm = TreeMap<Character,Integer>();
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>int count = 0;
<span style="white-space:pre"> </span>for(int x=0; x<chs.length; x++)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>if(chs[x]>='a' && chs[x]<='z' || chs[x]>='A' && chs[x]<='Z')
<span style="white-space:pre"> </span>continue;
<span style="white-space:pre"> </span>Integer value = tm.get(chs[x]);
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>if(value!=null)
<span style="white-space:pre"> </span>count = value;
<span style="white-space:pre"> </span>count++;
<span style="white-space:pre"> </span>tm.put(chs[x],count);
<span style="white-space:pre"> </span>count = 0;//如果把count定义在循环内,占用资源,开辟空间,不好。
<span style="white-space:pre"> </span>/*
<span style="white-space:pre"> </span>if(value==null)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>tm.put(chs[x].1);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>else
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>value = value +1;
<span style="white-space:pre"> </span>tm.put(chs[x],value);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>*/
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>//System.out.println(tm);
<span style="white-space:pre"> </span>StringBuilder sb = new StringBuilder();
<span style="white-space:pre"> </span>Set<Map.Entry<Character,Integer>>entrySet = tm.entrySet();
<span style="white-space:pre"> </span>Iterator<Map.Entry<Charactor,Integer>> it = entrySet.iterator();
<span style="white-space:pre"> </span>while(it.hasNext())
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>Map.Entry<Character,Integer> me = it.next();
<span style="white-space:pre"> </span>Character ch = ne.getKey();
<span style="white-space:pre"> </span>Integer value = me.getValue();
<span style="white-space:pre"> </span>sb.append(ch+"("+value+")");
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>return sb.toString();
<span style="white-space:pre"> </span>}
}
<img src="https://img-blog.csdn.net/20150109113427684" alt="" />
<strong>map扩展知识。</strong>
/*
map扩展知识。
map集合被使用是因为具备映射关系。
"yureban""01""zhangsan";
"yureban""02""lisi";
"jiuyeban""01""wangwu"
"jiuyeban""02""zhaoliu"
一个学校有多个教室,每一个教室都有名称。
*/
import java.util.*;
class Student
{
<span style="white-space:pre"> </span>private String id;
<span style="white-space:pre"> </span>private String name;
<span style="white-space:pre"> </span>Student(String id,String name)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>this.id = id;
<span style="white-space:pre"> </span>this.name = name;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public String toString()
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>return id+":::"+name;
<span style="white-space:pre"> </span>}
}
class MapDemo3
{
<span style="white-space:pre"> </span>public static void main(String[] args)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>HashMap<String,List<Student>> czbk = new HashMap<String,List<Student>>();
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>List<Student> reyu = new ArrayList<Student>();
<span style="white-space:pre"> </span>List<Student> jiuye = new ArrayList<Student>();
<span style="white-space:pre"> </span>czbk.put("yureban",reyu);
<span style="white-space:pre"> </span>czbk.put("jiuyeban",jiuye);
<span style="white-space:pre"> </span>reyu.add(new Studnet("01","zhangsa"));
<span style="white-space:pre"> </span>reyu.add(new Studnet("04","wangwu"));
<span style="white-space:pre"> </span>jiuye.add(new Studnet("01","zhouqi"));
<span style="white-space:pre"> </span>jiuye.add(new Studnet("02","zhaosi"));
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>Iterator<String> it = czbk.keySet().iterator();
<span style="white-space:pre"> </span>while(it.hasNext())
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>String roomName = it.next();
<span style="white-space:pre"> </span>List<Student> room = czbk.get(roomName);
<span style="white-space:pre"> </span>System.out.println(roomName);
<span style="white-space:pre"> </span>getInfos(room);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
}