Map 集合框架
Map 集合框架Map 集合框架介绍方法归类增加&删除判断&获取比较 Hashtable、HashMap、TreeMap的区别
Map 集合框架应用应用一应用二
Map 集合框架
Map 集合框架介绍
方法归类
map 集合中存放的都是一组组映射关系,key(键)=value(值)
补充:
Map集合映射关系May.Entry描述的伪代码
增加&删除
增加:
put(K key, V value)
putAll(Map<? extends K,? extends V> m)
删除:
clear()
remove(Object key)
判断&获取
判断:
containsKey(Object key)
containsValue(Object value)
isEmpty()
获取:
get(Object key)
size()
values()
entrySet()
keySet()
比较 Hashtable、HashMap、TreeMap的区别
注意:
添加元素时,如果键已经在集合中存在,那么后添加的值会覆盖原来的值,并且put方法会将原有的值返回
Map数据结构特点
Hashtable底层是哈希表数据结构不可以存入null键和null值,线程同步,jdk1.0,效率低HashMap底层是哈希表数据结构允许使用null值和null键,线程不同步。将Hashtable替代;jdk1.2,效率高TreeMap底层是二叉树数据结构线程不同步,可以用于给Map集合中的键进行排序
Map 集合框架应用
应用一
package com
.Map
;
import java
.util
.Comparator
;
import java
.util
.HashMap
;
import java
.util
.Map
;
import java
.util
.Map
.Entry
;
import java
.util
.Set
;
import java
.util
.TreeMap
;
public class TreeMapDemo {
public static void main(String
[] args
) {
Map
<Student, String> map
= new TreeMap<>(new StuComp());
map
.put(new Student("aa", 25), "韩国");
map
.put(new Student("ee", 21), "澳门");
map
.put(new Student("bb", 23), "北京");
map
.put(new Student("dd", 26), "韩国");
map
.put(new Student("cc", 21), "泰国");
map
.put(new Student("dd", 25), "韩国");
System
.out
.println(map
.size());
Set
<Entry
<Student, String>> entrySet
= map
.entrySet();
for (Entry
<Student, String> entry
: entrySet
) {
System
.out
.println(entry
.getKey()+"=="+entry
.getValue());
}
}
}
class Student implements Comparable<Student>{
private String name
;
private int age
;
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
;
}
public Student(String name
, int age
) {
super();
this.name
= name
;
this.age
= age
;
}
public Student() {
super();
}
@Override
public String
toString() {
return "Student [name=" + name
+ ", age=" + age
+ "]";
}
@Override
public int hashCode() {
return this.getName().hashCode()+this.getAge()*39;
}
@Override
public boolean equals(Object obj
) {
Student stu
= (Student
)obj
;
return this.getAge() ==stu
.getAge() && this.getName().equals(stu
.getName());
}
@Override
public int compareTo(Student o
) {
int num
= this.getAge() - o
.getAge();
if(num
== 0) {
return this.getName().compareTo(o
.getName());
}
return num
;
}
}
class StuComp implements Comparator<Student>{
@Override
public int compare(Student o1
, Student o2
) {
int num
= o1
.getName().compareTo(o2
.getName());
if(num
== 0) {
return o1
.getAge()-o2
.getAge();
}
return num
;
}
}
应用二
package com
.Map
;
import java
.util
.HashMap
;
import java
.util
.Map
;
public class HashMapDemo {
public static void main(String
[] args
) {
String str
= "basdfsfasaasadsfadsfa";
str
= count(str
);
System
.out
.println(str
);
}
private static String
count(String str
) {
char[] charArray
= str
.toCharArray();
Map
<Character, Integer> map
= new HashMap<>();
for (char c
: charArray
) {
Integer value
= map
.get(c
);
if(value
== null
) {
map
.put(c
, 1);
}else {
map
.put(c
, value
+1);
}
}
StringBuffer sb
= new StringBuffer();
for(Map
.Entry
<Character, Integer> entry
:map
.entrySet()) {
sb
.append(entry
.getKey()+"("+entry
.getValue()+")");
}
return sb
.toString();
}
}