1、集合框架Map介绍
此接口取代dictionary类,后者完全是一个抽象类,而不是一个接口。
map集合中存放的都是一组组映射关系,一对一(对应的) key(键)=value(值)
如果遇到多表联查,就用map,或者list
1.1 map的特有三种方法,put、KeySet、entrtySet
* put的三种
1、往集合容器中添加键值对映射关系;
2、当集合中存在该键的映射关系后者的映射关系会覆盖前面的映射关系
3、会返回上一个映射关系对应的值。
//增加效果如下
1.往集合容器中添加键值对映射关系;
2、当集合中存在该键的映射关系后者的映射关系会覆盖前面的映射关系,后面覆盖前面的值
3、会返回上一个映射关系对应的值。
import java.util.Map;
public class MapDemo {
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("zs", "11");
map.put("ls", "15");
map.put("ww", "13");
map.put("aa", "18");
//返回上一个值
String put = map.put("aa", "20");
System.out.println(put);
效果如下,返回了aa的上一个值18
参照下图,返回的是你的上一个值,输出的是目前的值
2、删除 clear()
效果如下,删除之后输出为空
根据键删 remove(Object key)
效果如下,删除了所以键为zs的
KeySet(遍历)
public class MapDemo {
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("zs", "11");
map.put("ls", "15");
map.put("ww", "13");
map.put("aa", "18");
//键不能重复
Set<String> keySet = map.keySet();
for (String key : keySet) {
System.out.println(key+":"+map.get(key));
}
效果如下
ww:13
aa:18
ls:15
zs:11
第二种:entrtySet(遍历)
效果如下
以上两种遍历方式图解
小结:
Map
Hashtable:底层是哈希表数据结构,不可以存入null键null值,该集合石线程同步的,jdk1.0,效率低
HashMap:底层是哈希表数据结构,允许使用null值和null键,该集合是不同步的。将Hashtable替代;jdk1.2,效率高
TreeMap:底层是二叉树数据结构,线程不同步,可以用于给Map集合中的键进行排序
注意:
添加元素时,如果键已经在集合中存在,那么后添加的值会覆盖原来的值,并且put方法会将原有的值返回
2.二叉树数据结构介绍
***TreeMap***和treeset一样是根据二叉树自动排序的
效果如下图
二叉树图解
2、集合框架Map的应用
应用一:
1、将学生作为键,地址作为值进行存储,名字年龄相同则被认定为一个人,最后输出
注意:
需要重写HashCode和equals方法才能够打到去重效果
当你输出当前添加的键值对应关系的返回值的时候,如果键值对应关系中已存在,就会返回已存在的映射关系的值
package com.Hc;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
public class TreeMapDemo {
public static void main(String[] args) {
HashMap<Student,String> hm=new HashMap<>();
hm.put(new Student("zx",18), "shenzhen");
hm.put(new Student("ls",21), "guangzhou");
hm.put(new Student("ww",25), "beijing");
hm.put(new Student("mz",21), "shanghai");
hm.put(new Student("zx",18), "hangzhou");
Set<Entry<Student, String>> entrySet = hm.entrySet();
for (Entry<Student, String> entry : entrySet) {
System.out.println(entry.getKey()+" "+ entry.getValue());
}
System.out.println("长度为:"+hm.size());
}
}
class 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) {
this.name = name;
this.age = age;
}
public Student() {}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + 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 s=(Student)obj;
return this.getName().equals(s.getName()) && this.getAge()==s.getAge();
}
return false;
}
}
效果如下
2、最后按年龄进行排序
注意:排序需要用TreeMap集合 还要实现Comparable接口和重写CompareTo反法
package com.Hc;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
public class TreeMapDemo {
public static void main(String[] args) {
TreeMap<Student,String> hm=new TreeMap<>();
hm.put(new Student("zx",18), "shenzhen");
hm.put(new Student("ls",21), "guangzhou");
hm.put(new Student("ww",25), "beijing");
hm.put(new Student("mz",21), "shanghai");
hm.put(new Student("zxx",18), "hangzhou");
Set<Entry<Student, String>> entrySet = hm.entrySet();
for (Entry<Student, String> entry : entrySet) {
System.out.println(entry.getKey()+" "+ entry.getValue());
}
System.out.println("长度为:"+hm.size());
}
}
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) {
this.name = name;
this.age = age;
}
public Student() {}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + 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 s=(Student)obj;
return this.getName().equals(s.getName()) && this.getAge()==s.getAge();
}
return false;
}
@Override
public int compareTo(Student o) {
// TODO Auto-generated method stub
int num=this.getAge() -o.getAge();
if(num==0) {
return this.getName().compareTo(o.getName());
}
return num;
}
}
效果如下
3、需求改变、按姓名进行排序
注意:排序需要用TreeMap集合,需求改变 还要实现Comparator接口和重写compare反法
package com.Hc;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
public class TreeMapDemo {
public static void main(String[] args) {
TreeMap<Student,String> hm=new TreeMap<>(new StudentComp());
hm.put(new Student("zx",18), "shenzhen");
hm.put(new Student("ls",21), "guangzhou");
hm.put(new Student("ww",25), "beijing");
hm.put(new Student("mz",21), "shanghai");
hm.put(new Student("zxx",18), "hangzhou");
Set<Entry<Student, String>> entrySet = hm.entrySet();
for (Entry<Student, String> entry : entrySet) {
System.out.println(entry.getKey()+" "+ entry.getValue());
}
System.out.println("长度为:"+hm.size());
}
}
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) {
this.name = name;
this.age = age;
}
public Student() {}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + 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 s=(Student)obj;
return this.getName().equals(s.getName()) && this.getAge()==s.getAge();
}
return false;
}
@Override
public int compareTo(Student o) {
// TODO Auto-generated method stub
int num=this.getAge() -o.getAge();
if(num==0) {
return this.getName().compareTo(o.getName());
}
return num;
}
}
class StudentComp 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;
}
}
应用二:
1.统计字符串中字符出现次数
2.按次数排序
分析:
1.字符是唯一的,可以将作为map集合的键Key,次数就是map集合的值Value.
2.将指定的字符串装到一个容器中进行筛选,将字符串转成一个字符串组
3.当字符第一次出现的时候,意味着在map集合中找不到对应的value值,给它赋值为1,
当字符第二次出现的时候,意味着map集合中存在对应的值,就给它对应的值加一;
package com.Hc;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class HashMapDemo {
public static void main(String[] args) {
String str="sanjdasjkdnkjasdnkasdnaisfjiafnzxda";
getcharString(str);
}
private static void getcharString(String str) {
// TODO Auto-generated method stub
char [] charArray=str.toCharArray();
Map<Character,Integer> mp=new HashMap<>();
for (char c : charArray) {
Integer num=(Integer)mp.get(c);
if(mp.get(c)==null) {
mp.put(c,1);
}else {
mp.put(c,++num);
}
}
StringBuffer sb=new StringBuffer();
Set<Entry<Character, Integer>> entrySet = mp.entrySet();
for (Entry<Character, Integer> entry : entrySet) {
sb.append(entry.getKey()+"("+entry.getValue()+")");
}
System.out.println(sb.toString());
}
}
效果如下
4、集合框架工具类(Collections、Arrays)
Collections.reverseOrder() 反转
1.正常排序:
package com.Hc;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
public class TreeMapDemo {
public static void main(String[] args) {
TreeMap<Student,String> hm=new TreeMap<>(new StudentComp());
hm.put(new Student("zx",18), "shenzhen");
hm.put(new Student("ls",21), "guangzhou");
hm.put(new Student("ww",25), "beijing");
hm.put(new Student("mz",21), "shanghai");
hm.put(new Student("zxx",18), "hangzhou");
Set<Entry<Student, String>> entrySet = hm.entrySet();
for (Entry<Student, String> entry : entrySet) {
System.out.println(entry.getKey()+" "+ entry.getValue());
}
System.out.println("长度为:"+hm.size());
}
}
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) {
this.name = name;
this.age = age;
}
public Student() {}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + 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 s=(Student)obj;
return this.getName().equals(s.getName()) && this.getAge()==s.getAge();
}
return false;
}
@Override
public int compareTo(Student o) {
// TODO Auto-generated method stub
int num=this.getAge() -o.getAge();
if(num==0) {
return this.getName().compareTo(o.getName());
}
return num;
}
}
class StudentComp 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;
}
}
效果如下
2.反转倒序:
package com.Hc;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
public class TreeMapDemo {
public static void main(String[] args) {
TreeMap<Student,String> hm=new TreeMap<>(Collections.reverseOrder(new StudentComp()));
hm.put(new Student("zx",18), "shenzhen");
hm.put(new Student("ls",21), "guangzhou");
hm.put(new Student("ww",25), "beijing");
hm.put(new Student("mz",21), "shanghai");
hm.put(new Student("zxx",18), "hangzhou");
Set<Entry<Student, String>> entrySet = hm.entrySet();
for (Entry<Student, String> entry : entrySet) {
System.out.println(entry.getKey()+" "+ entry.getValue());
}
System.out.println("长度为:"+hm.size());
}
}
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) {
this.name = name;
this.age = age;
}
public Student() {}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + 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 s=(Student)obj;
return this.getName().equals(s.getName()) && this.getAge()==s.getAge();
}
return false;
}
@Override
public int compareTo(Student o) {
// TODO Auto-generated method stub
int num=this.getAge() -o.getAge();
if(num==0) {
return this.getName().compareTo(o.getName());
}
return num;
}
}
class StudentComp 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() 返回指定数组内容的字符串表示形式。
package com.Hc;
import java.util.Arrays;
public class ArraylistDemo {
public static void main(String[] args) {
String [] str=new String[] {"asd,ASDA,URYU"};
System.out.println(Arrays.toString(str));
}
}
效果如下