1.初识Map
map集合中存放的都是一组组映射关系 key=value
Map集合两种遍历框架图
遍历代码块以及输出结果
package com.Map;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
* 初识map
* put
* 1.往集合容器中添加对映关系
* 2.当集合存在该键的映射关系,后来的映射关系会覆盖前面的映射关系
* 3.会返回上一个映射关系对应的值
* keySet
*
* entrySet
*
*
* 多表联查用 map list<map>
* @author 20190528
*
*/
public class MapDemo {
public static void main(String[] args) {
Map<String, String>map=new HashMap<>();
map.put("zs", "12");
map.put("ls", "22");
map.put("ww", "32");
map.put("mazi", "11");
map.put("zs", "32");
Set<String> keySet = map.keySet();
for (String key : keySet) {
System.out.println(key+ ":"+ map.get(key));
}
Set<Entry<String, String>> entrySet = map.entrySet();
for (Entry<String, String> entry : entrySet) {
System.out.println(entry.getKey()+":"+entry.getValue());
}
}
}
2、map集合的应用
应用一:
package com.Map;
import java.util.Comparator;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
/**
*
应用一:
1、将学生作为键,地址作为值进行存储,名字年龄相同则被认定为一个人,最后输出
2、最后按年龄进行排序
3、需求改变、按姓名进行排序
分析:
1.1封装出学生的实体类
1.2将不同的学生作为Key,不同的地址作为value,存放到Map集合中
1.3需要将重复的给剔除掉
* @author
*
*/
public class TreeMapDemo {
public static void main(String[] args) {
TreeMap<Student, String> hm=new TreeMap<>(new stuComp());
hm.put(new Student("zs", 12), "beijing");
hm.put(new Student("ls", 22), "guanzhou");
hm.put(new Student("ww", 21), "shanghai");
hm.put(new Student("mazi", 6), "chendu");
hm.put(new Student("zss", 12), "hangzhou");
hm.put(new Student("zss", 13), "hangzhou");
// System.out.println(hm.size());
Set<Entry<Student, String>> entrySet = hm.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;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
@Override
public int hashCode() {
return this.getName().hashCode()+ this.age;
}
@Override
public boolean equals(Object obj) {
if(obj instanceof Student) {
Student stu=(Student)obj;
return this.getName().equals(stu.getName())
&& this.getAge()==stu.getAge();
}
return false;
}
@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) {
// TODO Auto-generated method stub
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;
import java.util.Map.Entry;
import java.util.Set;
/**
*
应用二:
统计字符串中字符出现次数
按次数排序
addasdrefsdawwrefsdaesfdadswqwdssweferwdew
a(?)b(?)c(?)
分析:
1.字符是唯一的,可以将其作为map集合的key,次数就是map的值value;
2.将指定的字符装到一个容器中进行筛选,将字符串转成一个字符数组
3.当字符第一次出现的时候,意味着在map集合中找不到对应的value值,给他赋值为一
当字符第二次出现的时候,意味着map集合中存在对应的值,给他对应的值加一;
* @author
*
*/
public class HashMapDemo {
public static void main(String[] args) {
String s="addasdrefsdawwrefsdaesfdadswqwdssweferwdew";
//{'a','b','c',....}
getRepeatTimes(s);
}
// 统计次数的方法
private static void getRepeatTimes(String s) {
char[] charArray = s.toCharArray();
Map<Character, Integer>map=new HashMap<>();
for (char c : charArray) {
Integer var=(Integer)map.get(c);
if(var==null) {
map.put(c, 1);
}else {
map.put(c, ++var);
}
}
StringBuffer sb=new StringBuffer();
Set<Entry<Character, Integer>> entrySet = map.entrySet();
for (Entry<Character, Integer> entry : entrySet) {
sb.append(entry.getKey()+"a("+entry.getValue()+")");
}
System.out.println(sb.toString());
}
}
输出结果