-------
android培训、
java培训、期待与您交流! ----------
2.删除
clear();从此映射中移除所有映射关系
V remove(Object key) 如果存在一个键的映射关系,则将其从此映射中移除
Map
|---Hashtable: 底层是哈希表数据结构,不可以存入null值, 该集合是线程同步,jdk1.0 效率低
|---HashMap:底层是哈希表数据结构, 允许使用null值和null键,该集合是线程不同步的,jdk1.2 效率高
|---TreeMap:底层是二叉树数据结构, 线程不同步, 可以i用于给map集合中的键进行排序
Map集合:该集合存储键值对,一对的一对往里存,二期要保证键的唯一性
1.添加
V put(K key, V value)
putAll(Map<? extends K,? extends V> m)
V put(K key, V value)
putAll(Map<? extends K,? extends V> m)
2.删除
clear();从此映射中移除所有映射关系
V remove(Object key) 如果存在一个键的映射关系,则将其从此映射中移除
3.判断
containsKey(Object key)
containsValue(Object value) 如果此映射将一个或多个键映射到指定值
isEmpty()
containsKey(Object key)
containsValue(Object value) 如果此映射将一个或多个键映射到指定值
isEmpty()
Set<Map.Entry<K,V>> entrySet() 返回此映射中包含的映射关系的 Set 视图
Set<K> keySet() 返回此映射中包含的键的 Set 视图
Set<K> keySet() 返回此映射中包含的键的 Set 视图
4.获取
V get(Object key)
size()
Collection<V> values() 返回此映射中包含的值的 Collection 视图
V get(Object key)
size()
Collection<V> values() 返回此映射中包含的值的 Collection 视图
Map
|---Hashtable: 底层是哈希表数据结构,不可以存入null值, 该集合是线程同步,jdk1.0 效率低
|---HashMap:底层是哈希表数据结构, 允许使用null值和null键,该集合是线程不同步的,jdk1.2 效率高
|---TreeMap:底层是二叉树数据结构, 线程不同步, 可以i用于给map集合中的键进行排序
和Set很像。
其实Set底层是使用了Map集合
例子1:
其实Set底层是使用了Map集合
例子1:
public class MapDemo {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
// 添加元素,如果出现添加相同的键,那么后添加的值会覆盖原有键对应值
// 并put方法会返回被覆盖的值
map.put("01", "kiss__01");
System.out.println(map.put("01", "kiss__05"));
map.put("02", "kiss__02");
map.put("03", "kiss__03");
map.put("04", "kiss__04");
System.out.println("01 iscontains Map:" + map.containsKey("01"));
// System.out.println("remove 02:"+ map.remove("01"));
System.out.println("get 01 Value:" + map.get("01"));
//可以通过get方法的返回值来判断一个键是否存在
//健和值都可以为null 但不建议这样使用
map.put(null, "kiss__09");
map.put("kiss__09", null);
//获取map集合中所有的值
Collection<String> col = map.values();
System.out.println("col:" + col);
System.out.println("map:" + map);
}
}
map集合的两种取出方式:
1,Set<K> keySet: 将map中所有的键存入到Set集合中, 因为set具备迭代器。
所有可以迭代方式取出所有的键,在根据get方法,获取每一个键对应的值。
Map集合的取出原理:将map集合转成set集合,再通过迭代器取出。
所有可以迭代方式取出所有的键,在根据get方法,获取每一个键对应的值。
Map集合的取出原理:将map集合转成set集合,再通过迭代器取出。
2.Set<Map.Entry<K,V>> entrySet:将map集合中的映射广西存入到了set集合中,而这个关系的数据类型就是:Map.Entry
Map.Entry 其实Entry也是一个接口,它是Map接口中的一个内部接口
常用方法:
getKey() 返回与此项对应的键
getValue() 返回与此项对应的值。
interface Map{
public static interface Entry{
public abstract Object getKey();
public abstract Object getValue();
}
}
取出Map遍历:
Map.Entry 其实Entry也是一个接口,它是Map接口中的一个内部接口
常用方法:
getKey() 返回与此项对应的键
getValue() 返回与此项对应的值。
interface Map{
public static interface Entry{
public abstract Object getKey();
public abstract Object getValue();
}
}
取出Map遍历:
public class MapDemo02 {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("1", "lisi01");
map.put("4", "lisi04");
map.put("2", "lisi02");
map.put("3", "lisi03");
//第二种取出方式:entrySet方式
//Map.Entry是Map接口中的一个内部类接口
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);
}
// 第一种取出方式:keySet方式
/*
Set<String> keySet = map.keySet();
Iterator<String> it = keySet.iterator();
while (it.hasNext()) {
String key = it.next();
String value = map.get(key);
System.out.println("key:" + key + ",value:" + value);
}
*/
}
}
需求:每一个学生都有对应的归属地
学生Student,地址String
学生属性:姓名,年龄
注意:姓名和年龄相同的视为同一个学生
保证学生的唯一性
学生Student,地址String
学生属性:姓名,年龄
注意:姓名和年龄相同的视为同一个学生
保证学生的唯一性
1.描述学生、
2.定义map容器,将学生作为键,地址作为值,存入
3.获取map集合中的元素
实现:
需求:对学生对象的年龄进行升序排序
因为数据是以键值对形式存在的
所以要使用可以排序的Map集合。TreeMap。
例子:
“geaajiosaglkmepbogea”获取该字符串中的字幕出现的次数
实现:
class Student implements Comparable<Student>{
private String name;
private int age;
Student(String name,int age){
this.name = name;
this.age = age;
}
//使用到哈希表有关的集合,我们一点要重写hashCode和equals方法
@Override
public int hashCode() {
return name.hashCode()+age*37;
}
@Override
public boolean equals(Object obj) {
if(!(obj instanceof Student))
throw new ClassCastException();
Student stu = (Student) obj;
return this.name.equals(stu.name) && this.age == stu.age;
}
public int compareTo(Student stu) {
int num = new Integer(age).compareTo(stu.age);
if(num == 0){
return name.compareTo(stu.name);
}
return num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return name+":"+age;
}
}
public class MapTest {
public static void main(String[] args) {
Map<Student,String> map = new HashMap<Student,String>();
map.put(new Student("zhangsan01",21), "guangxi");
map.put(new Student("zhangsan02",22), "beijing");
map.put(new Student("zhangsan03",23), "shenzhun");
map.put(new Student("zhangsan04",24), "shanghai");
//第一种获取方式:keySet
Set<Student> keySet = map.keySet();
Iterator<Student> it = keySet.iterator();
while(it.hasNext()){
Student stu = it.next();
String addr = map.get(stu);
System.out.println(stu+"----"+addr);
}
//第二种获取方式:entrySet
Set<Map.Entry<Student, String>> me = map.entrySet();
Iterator<Map.Entry<Student, String>> ite = me.iterator();
while(ite.hasNext()){
Map.Entry<Student, String> entry = ite.next();
Student stu = entry.getKey();
String address = entry.getValue();
System.out.println(stu+"______"+address);
}
}
}
需求:对学生对象的年龄进行升序排序
因为数据是以键值对形式存在的
所以要使用可以排序的Map集合。TreeMap。
例子:
public class MapTest02 {
public static void main(String[] args) {
TreeMap<Student,String> tm = new TreeMap<Student,String>(new StuNameComparator());
tm.put(new Student("Tom06",22), "beijing");
tm.put(new Student("Tom07",21), "America");
tm.put(new Student("Tom09",24), "guangxi");
tm.put(new Student("Tom02",23), "shanghai");
//遍历
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);
}
}
}
//以名字排序的比较器(Comparator) 当我们不能改动源代码时,我们可以使用这种实现Comparator的方法
class StuNameComparator implements Comparator<Student>{
@Override
public int compare(Student o1, Student o2) {
int num = o1.getName().compareTo(o2.getName());
if(num == 0){
return new Integer(o1.getAge()).compareTo(new Integer(o2.getAge()));
}
return num;
}
}
练习:“geaajiosaglkmepbogea”获取该字符串中的字幕出现的次数
希望打印结果:a(4) b(1)
通过结果发现,每一个字母都有对应的次数
说明字母和次数之间都有映射关系
当发现有映射关系时,可以选择map集合
因为map集合中存放就是映射关系
什么时候使用map集合呢?
当数据之间存在映射关系时,就要先想map集合
思路:
1.将字符串转换成字符数组,因为要对每一个字母进行操作
2.定义一个map集合,因为打印结果的字母有顺序,所以使用treemap集合
3.遍历字符数组。
将每一个字母作为键去查map集合
如果返回null,将该字母和1存入到map集合中
如果返回不是null,说明该字母在map集合中存在并有对应次数
那么, 就获取该次数并进行自增,然后该字母和自增后的次数存入到Map集合中,覆盖调用原来的键所对应的值
4.将map集合中的数组变成指定的字符串形式返回;
例子:
通过结果发现,每一个字母都有对应的次数
说明字母和次数之间都有映射关系
当发现有映射关系时,可以选择map集合
因为map集合中存放就是映射关系
什么时候使用map集合呢?
当数据之间存在映射关系时,就要先想map集合
思路:
1.将字符串转换成字符数组,因为要对每一个字母进行操作
2.定义一个map集合,因为打印结果的字母有顺序,所以使用treemap集合
3.遍历字符数组。
将每一个字母作为键去查map集合
如果返回null,将该字母和1存入到map集合中
如果返回不是null,说明该字母在map集合中存在并有对应次数
那么, 就获取该次数并进行自增,然后该字母和自增后的次数存入到Map集合中,覆盖调用原来的键所对应的值
4.将map集合中的数组变成指定的字符串形式返回;
例子:
public class MapTest03 {
public static void main(String[] args) {
String str = "geaajiosaglkmepboge@#a";
String response = charCount(str);
System.out.println(response);
}
public static String charCount(String str) {
char[] arr = str.toCharArray();
Map<Character, Integer> map = new TreeMap<Character, Integer>();
for (int i = 0; i < arr.length; i++) {
Integer value = map.get(arr[i]);
//当输入不是字母是过滤掉
if(!(arr[i]>='a' && arr[i]<='z' || arr[i]>='A' && arr[i]<='Z'))
continue;
if (value == null) {
map.put(arr[i], 1);
} else {
value = value + 1;
map.put(arr[i], value);
}
}
//用来存储字符串
StringBuilder sb = new StringBuilder();
Set<Map.Entry<Character,Integer>> set = map.entrySet();
Iterator<Map.Entry<Character,Integer>> it = set.iterator();
while(it.hasNext()){
Entry<Character, Integer> me = it.next();
Character cha = me.getKey();
Integer count = me.getValue();
sb.append(cha+"("+count+") ");
}
return sb.toString();
}
}
Map集合扩展知识:
需求:学校中包含教室,教室中包含学生
例子:
例子:
class
Stu{
private
String
no ;
private
String
name;
Stu(
String
no,
String
name){
this.
no
=
no;
this.
name
=
name;
}
public
String
getName() {
return
name;
}
public
void
setName(
String
name) {
this.
name
=
name;
}
public
String
getNo() {
return
no;
}
public
void
setNo(
String
no) {
this.
no
=
no;
}
}
public
class
MapDemo03 {
public
static
void
demo(){
HashMap
<
String,
List
<
Stu
>>
school
=
new
HashMap
<
String,
List
<
Stu
>>();
List
<
Stu
>
cla01
=
new
ArrayList
<
Stu
>();
List
<
Stu
>
cla02
=
new
ArrayList
<
Stu
>();
school.
put(
"class01",
cla01);
school.
put(
"class02",
cla02);
cla01.
add(
new
Stu(
"01",
"zhangsan"));
cla01.
add(
new
Stu(
"02",
"lisi"));
cla02.
add(
new
Stu(
"01",
"wangwu"));
cla02.
add(
new
Stu(
"02",
"zhaoliu"));
Iterator
<
String
>
it
=
school.
keySet().
iterator();
while(
it.
hasNext()){
String
className
=
it.
next();
List
<
Stu
>
list
=
school.
get(
className);
System.
out.
println(
"className:"
+
className);
get(
list);
}
}
//遍历Stu
public
static
void
get(
List
<
Stu
>
cla){
Iterator
<
Stu
>
it
=
cla.
iterator();
while(
it.
hasNext()){
Stu
stu
=
it.
next();
System.
out.
println(
stu.
getNo()
+
":"
+
stu.
getName());
}
}
public
static
void
main(
String[]
args) {
demo();
/* HashMap<String, HashMap<String, String>> school = new HashMap<String, HashMap<String, String>>();
HashMap<String, String> cla01 = new HashMap<String, String>();
HashMap<String, String> cla02 = new HashMap<String, String>();
// 向cla01班级里添加学生
cla01.put("01", "zhangshan");
cla01.put("02", "lisi");
// 向cla02班级里添加学生
cla02.put("01", "wangwu");
cla02.put("02", "zhaoliu");
// 将班级添加到学校中
school.put("class01", cla01);
school.put("class02", cla02);
IteAllStu(school);
*/
}
// 遍历班级所有学生
public
static
void
IteStuName(
HashMap
<
String,
String
>
cla) {
Iterator
<
String
>
it
=
cla.
keySet().
iterator();
while (
it.
hasNext()) {
String
no
=
it.
next();
String
name
=
cla.
get(
no);
System.
out.
println(
no
+
":"
+
name);
}
}
// 遍历学校所有学生
public
static
void
IteAllStu(
HashMap
<
String,
HashMap
<
String,
String
>>
school) {
Iterator
<
String
>
it
=
school.
keySet().
iterator();
while (
it.
hasNext()) {
String
className
=
it.
next();
HashMap
<
String,
String
>
cla
=
school.
get(
className);
System.
out.
println(
"room:"
+
className);
IteStuName(
cla);
}
}
}