1、集合框架Map介绍
遍历方式一
通过键找值:
获取所有键的集合,然后遍历键的集合,获取到每一个键,接着根据键找值
Map<String, String> map=new HashMap<>();
map.put("bb", "13");
map.put("aa", "12");
map.put("cc", "14");
map.put("dd", "15");
Set<String> st = map.keySet();
for (String set : st) {
System.out.println("键"+set+" 值"+map.get(set));
}
遍历方式二
直接获取键值对对象:
获取所有的键值对对象的集合,遍历键值对对象的集合,获取到值。
Map<String, String> map=new HashMap<>();
map.put("bb", "13");
map.put("aa", "12");
map.put("cc", "14");
map.put("dd", "15");
Set<Entry<String, String>> set2 = map.entrySet();
for (Entry<String, String> entry : set2) {
System.out.println("键"+entry.getKey()+" 值"+entry.getValue());
}
删除功能
remove(Object key):根据键删除键值对元素
Map<String, String> map=new HashMap<>();
map.put("bb", "13");
map.put("aa", "12");
map.put("cc", "14");
map.put("dd", "15");
System.out.println(map.toString());
String st=map.remove("bb");
System.out.println(st);
System.out.println(map.toString());
小结:
Map
Hashtable:底层是哈希表数据结构,不可以存入null键null值,该集合石线程同步的,jdk1.0,效率低
HashMap:底层是哈希表数据结构,允许使用null值和null键,该集合是不同步的。将Hashtable替代;jdk1.2,效率高
TreeMap:底层是二叉树数据结构,线程不同步,可以用于给Map集合中的键进行排序
注意:
添加元素时,如果键已经在集合中存在,那么后添加的值会覆盖原来的值,并且put方法会将原有的值返回
2、集合框架Map应用
应用一:
1、将学生作为键,地址作为值进行存储,名字年龄相同则被认定为一个人,最后输
2、最后按年龄进行排序
3、需求改变、按姓名进行排序
分 析:
1.1封装出一个学生的实体类
1.2将不同的学生作为key,不同的地址作为value,存放到map集合中
1.3需要将重复的key给剔除掉
TreeMap<Student, String> hm=new TreeMap<>(new stuComp());
hm.put(new Student("aa", 11), "aa");
hm.put(new Student("bb", 15), "bb");
hm.put(new Student("cc", 15), "cc");
hm.put(new Student("dd", 10), "dd");
System.out.println(hm.size());
Set<Entry<Student,String>> set = hm.entrySet();
for (Entry<Student, String> entry : set) {
System.out.println(entry.getKey()+"="+entry.getValue());
}
}
}
class Student implements Comparable<Student>{
private String name;
private int age;
@Override
public int hashCode() {
// TODO Auto-generated method stub
return this.getName().hashCode()+this.age;
}
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
if(obj instanceof Student) {
Student stu=(Student)obj;
return this.getName().equals(stu.getName())&&this.getAge()==stu.getAge();
}
return false;
}
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 compareTo(Student o) {
// TODO Auto-generated method stub
int nmu=this.getAge()-o.getAge();
if(nmu==0) {
return this.getName().compareTo(o.getName());
}
return nmu;
}
}
class stuComp implements Comparator<Student>{
@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
int num=o1.getName().compareTo(o2.getName());
if(num==0) {
return o1.getAge()-o2.getAge();
}
return num;
}
结果是:
应用二:
统计字符出现的次数
从a-z进行统计
分析:
- 1.字符是唯一的,可以将其作为map集合的key,次数减少map集合的value
- 2.将指定的字符串装到一个容器中进行筛选,将字符串转换一个字符数组
- 3.当字符第一次出现的时候,意味着在map集合中找不到对应的value值,给他赋值为一
第二次出现的时候意味着map集合存在对应的值,给对应的值加一
public static void main(String[] args) {
String s="asjdaaabaskjnfiudhsiuhgknksdhturhksnkniugherigsnkjvnsdkjuhti";
getRepeatTimes(s);
}
/**
* 统计次数的方法
*
* @param s
*/
private static void getRepeatTimes(String s) {
// TODO Auto-generated method stub
char[] ch = s.toCharArray();
Map<Character, Integer> map=new HashMap<>();
for (char c : ch) {
Integer val=map.get(c);
if(val==null) {
map.put(c, 1);
}else {
map.put(c,++val);
}
}
StringBuffer sb=new StringBuffer();
Set<Entry<Character, Integer>> set = map.entrySet();
for (Entry<Character, Integer> entry : set) {
sb.append(entry.getKey()+"("+entry.getValue()+")"+" ");
}
System.out.println(sb.toString());
}
结果是:
3、集合框架工具类(Collections、Arrays)
Collections.reverseOrder_____倒序
正常输出
// TreeMap<Student, String> hm=new TreeMap<>(new stuComp());
// 倒序输出Collections.reverseOrder
TreeMap<Student, String> hm=new TreeMap<>(Collections.reverseOrder(new stuComp()));
hm.put(new Student("aa", 11), "浪琴湾1");
hm.put(new Student("bb", 15), "浪琴湾2");
hm.put(new Student("cc", 15), "浪琴湾3");
hm.put(new Student("dd", 10), "浪琴湾4");
Set<Entry<Student,String>> set = hm.entrySet();
for (Entry<Student, String> entry : set) {
System.out.println("键:"+entry.getKey()+" 值:"+entry.getValue());
/**正常输出:
* 键:Student [name=aa, age=11] 值:浪琴湾1
键:Student [name=bb, age=15] 值:浪琴湾2
键:Student [name=cc, age=15] 值:浪琴湾3
键:Student [name=dd, age=10] 值:浪琴湾4
而倒序输出:
键:Student [name=dd, age=10] 值:浪琴湾4
键:Student [name=cc, age=15] 值:浪琴湾3
键:Student [name=bb, age=15] 值:浪琴湾2
键:Student [name=aa, age=11] 值:浪琴湾1
*/
}
}
}
class Student implements Comparable<Student>{
private String name;
private int age;
@Override
public int hashCode() {
// TODO Auto-generated method stub
return this.getName().hashCode()+this.age;
}
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
if(obj instanceof Student) {
Student stu=(Student)obj;
return this.getName().equals(stu.getName())&&this.getAge()==stu.getAge();
}
return false;
}
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 compareTo(Student o) {
// TODO Auto-generated method stub
int nmu=this.getAge()-o.getAge();
if(nmu==0) {
return this.getName().compareTo(o.getName());
}
return nmu;
}
}
class stuComp implements Comparator<Student>{
@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
int num=o1.getName().compareTo(o2.getName());
if(num==0) {
return o1.getAge()-o2.getAge();
}
return num;
}
倒序输出:
Arrays.toString_____返回指定数组内容用字符串表示形式
String[] ss=new String[] {
"aa","bb","11"
};
System.out.println(Arrays.toString(ss));
输出结果: