1、集合框架Map介绍
Hashtable:底层是哈希表数据结构,不可以存入null键null值,该集合石线程同步的,jdk1.0,效率低
HashMap:底层是哈希表数据结构,允许使用null值和null键,该集合是不同步的。将Hashtable替代;jdk1.2,效率高
TreeMap:底层是二叉树数据结构,线程不同步,可以用于给Map集合中的键进行排序
(面试题):
Map、list、set 这三个接口,哪个不继承与Collection接口? Map
- map集合中存放的都是一组组映射关系,每一个键只映射到一个值
map特有的方法:
a. put
如果遇到多表联查,就用map
1.特点:
往集合容器中添加键值对应关系,当集合中存在该键的映射关系,后来的映射关系会覆盖的前面的映射关系
package com.qukang.map;
import java.util.HashMap;
import java.util.Map;
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("mz","11");
map.put("zs","23");
System.out.println(map);
}
}
输出结果:
当你输出当前添加的键值对应关系的返回值的时候,如果键值对应关系中已存在,就会返回已存在的映射关系的值
package com.qukang.map;
import java.util.HashMap;
import java.util.Map;
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("mz","11");
String put = map.put("zs","23");
System.out.println(put);
System.out.println(map);
}
}
输出结果:
b. KeySet
KeySet是Map集合的第一种特有遍历方式
1.特点:
KeySet是根据键调用 get(key) 来拿相对隐射关系的值
package com.qukang.map;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
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("mz","11");
Set<String> keySet = map.keySet();
for (String key : keySet) {
System.out.println(key +" " +map.get(key));
}
}
}
结果:
c. entrySet
entrySet是Map集合的第二种特有遍历方式
1.特点:
entrySet是把集合里的映射关系一组一组的拿出来
package com.qukang.map;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
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("mz","11");
Set<Entry<String, String>> entrySet = map.entrySet();
for (Entry<String, String> entry : entrySet) {
System.out.println(entry.getKey() +" "+ entry.getValue());
}
}
}
结果:
2、TreeSet 二叉树数据结构
package com.qukang.map;
import java.util.Iterator;
import java.util.TreeSet;
public class TreexsMapDemo {
public static void main(String[] args) {
TreeSet<Integer> ts=new TreeSet<>();
ts.add(22);
ts.add(25);
ts.add(28);
ts.add(33);
ts.add(21);
ts.add(24);
ts.add(27);
ts.add(36);
ts.add(32);
ts.add(18);
Iterator<Integer> it = ts.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
结果:
二叉树的数据排序结构:
3、集合框架Map的应用
应用一:
1、将学生作为键,地址作为值进行存储,名字年龄相同则被认定为一个人,最后输出
注意:
需要重写HashCode和equals方法才能够打到去重效果
当你输出当前添加的键值对应关系的返回值的时候,如果键值对应关系中已存在,就会返回已存在的映射关系的值
package com.qukang.map;
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.qukang.map;
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.qukang.map;
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.qukang.map;
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="wqongqiovnqubczmqevsaavawqrqgqverhejtyiyynt";
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.qukang.map;
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.qukang.map;
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.qukang.map;
import java.util.Arrays;
public class ArraysDemo {
public static void main(String[] args) {
String [] str=new String[] {"QWE,RTY,UIO"};
System.out.println(Arrays.toString(str));
}
}
结果: