Map接口: 存储双列集合.键值对对象
集合存储键值对应关系的数据
学习的三个子实现类: Map集合的数据结构只和键有关.
HashMap:哈希表 唯一性,无序性 不是Integer String()StringInteger默认重写equals()和Hashcode()方法. 需要重写equals()和hashcode()方法
HashMap:线程高效,不同步,不安全. HashTable:线程同步,安全,效率低
TreeMap:二叉树,唯一性.能排序:自然排序Compareable接口 实现compareTo()
比较器排序:Comparator接口 根据conpaerTo()方法的返回值排序.
LinkedHashMap:链表 和哈希表 有序性(链表),唯一性(哈希表)
Map功能概述: 添加功能 put(K key,V value):添加元素
当第一次使用put方法,返回的值为null.后面的返回值为上一个.
后面返回的为前一个键值对对象. 当键和值相同时.后面的键和值覆盖掉前面的值.
删除功能: 方法:clear()删除所有键值对
remove() 键值对从集合移除出键和值
判断 booleancontainKey(Object key) 是否包含键
Value(Object key ) 判断是否有值 isEmpty() 判断是否为空
获取功能(通过调用其中的功能遍历Map集合):
Set<Map.Entry<K,V>> entrySet():返回一个键值对的set集合
V get(Object key) :根据键获取值
Set<K>KeySet():获取集合中所有键的集合
Collection<V> values():获取集合中所有值得集合
遍历Map集合的方法: (快捷键 ctrl+1代码补全)
1. 获取所有的键和值 keyset方法(): 再使用增强for遍历
使用KeySet()方法 : 使用增强for 键拼接 使用get(key)
2.entrySet():返回一个值对应的Set集合 使用getkey和getvalue拼接
Eg:
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
public class Demo {
public static void main(String[] args) {
HashMap<String, Integer> hs = new HashMap<String, Integer>();
hs.put("刘备", 33);
hs.put("关羽",28);
hs.put("诸葛亮",25);
//第一种方法
Set<String> set = hs.keySet();
for(String str:set){//键
System.out.println(str+"--"+hs.get(str));
}
//第二种方法
Set<Entry<String,Integer>> set2= hs.entrySet();
for(Entry<String,Integer> s:set2){
System.out.println(s.getKey()+"--"+s.getValue());
}
}
}
HashMap存储Student类对象 遍历
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
public class MapDemo {
public static void main(String[] args) {
//学生对象作为键对象
HashMap<Student, String> str = new HashMap<Student,String>();
str.put(new Student("吴文龙",20) ,"0001");
str.put(new Student("杨启龙",20), "0002");
str.put(new Student("邓聪",23), "0003");
System.out.println(str);//需要重写toString方法:否则返回的为地址值
Set<Student> chs = str.keySet();//KeySet方法
for(Student s:chs){
System.out.println(s.getName()+"--"+s.getAge()+"--"+str.get(s));
}
//Entry方法调用getKey getValue
Set<Entry<Student, String>>set2 = str.entrySet();
for(Entry<Student, String> s:set2){
System.out.println(s.getKey().getName()+"--"+
"--"+s.getKey().getAge()+"--"+s.getValue());
}
}
}
建立Student类: Student类必须重写ToString类型,因为键为Student类,
不重写,默认返回的为地址值. put方法添加. 两种方法遍历
获取集长度:size方法.
Student类作为键: 遍历的对象排序:
年龄下来姓名
自然排序:Student类实现Comparable 重写CompatTo
学生类CompareTo()
@Override
public int compareTo(Studentstu) {
int num1=this.age-stu.age;
int num2=num1==0?this.name.compareTo(stu.name):num1;
return num2;
}
//测试类
import java.util.Set;
import java.util.TreeMap;
public class HashMapDemo {
public static void main(String[] args) {
//Student类年龄 姓名排序
//Student实现Comparable接口,自然排序,重写compareTo方法.
TreeMap<Student, String> str = new TreeMap<Student,String>();
str.put(new Student("吴文龙",20) ,"0001");
str.put(new Student("杨启龙",20), "0002");
str.put(new Student("邓聪",23), "0003");
str.put(new Student("彭楷",19), "0001");
str.put(new Student("杨晓洲",20), "0004");
Set<Student> set = str.keySet();//keySet方法遍历
for(Student s:set){
System.out.println(s.getName()+"-"+s.getAge()+"-"+str.get(s));
}
}
}
输出结果:
彭楷-19-0001
吴文龙-20-0001
杨启龙-20-0002
杨晓洲-20-0004
邓聪-23-0003
Student类作为键值得比较器排序
public static void main(String[] args) {
TreeMap<Student, String> str = new TreeMap<Student,String>();
new TreeMap(new Comparator<Student>(){
@Override
public int compare(Student s1, Student s2) {
int num1=s1.getAge()-s2.getAge();
int num2=num1==0?s1.getName().compareTo(s2.getName()):num1;
return num2;
}
});
str.put(new Student("吴文龙",20) ,"0001");
str.put(new Student("杨启龙",20), "0002");
str.put(new Student("邓聪",23), "0003");
str.put(new Student("彭楷",19), "0001");
str.put(new Student("杨晓洲",20), "0004");
Set<Entry<Student,String>> set = str.entrySet();
for(Entry<Student,String>s:set){
System.out.println(s.getKey().getName()+"--"+s.getKey().getAge()+
"-"+ s.getValue());
}
}
}
输出结果:
彭楷--19-0001
吴文龙--20-0001
杨启龙--20-0002
杨晓洲--20-0004
邓聪--23-0003
练习:用户输入一段字符串,统计字符串中每个字符出现的次数
输出结果位a(5)b(6)….
public class PracticeText {
public static void main(String[] args) {
Scannersc=new Scanner(System.in);
System.out.println("请输入一行字符串");
Stringstr=sc.nextLine();
char[] chr =str.toCharArray();
HashMap<Character,Integer>map = new HashMap<Character,Integer>();
for(char s :chr){
Integers1 = map.get(s);
if (s1 == null) {
map.put(s, 1);
} else {
s1++;
map.put(s, s1);
}
}
Set<Entry<Character,Integer>> Set = map.entrySet();
StringBuffer sb=new StringBuffer();
for(Entry<Character,Integer> ch:Set){
sb.append(ch.getKey()).append("(").append(ch.getValue()).append(")");
}
System.out.println(sb.toString());
}
}
输出结果:
请输入一行字符串
grgrrgrggr
g(5)r(5)
HashMap的集合嵌套:
public class ParcticeText2 {
public static void main(String[] args) {
//Map集合嵌套便利
HashMap<String,Integer> map1 = new HashMap<String,Integer>();
HashMap<String,Integer> map2 = new HashMap<String,Integer>();//创建两个小集合
HashMap<String,HashMap<String, Integer>> Bigmap = new HashMap<String,HashMap<String,Integer>>();
map1.put("女娲", 10000);
map1.put("荆轲", 20);
map2.put("鳄鱼", 30);
map2.put("剑姬", 20);
Bigmap.put("wz", map1);
Bigmap.put("lm", map2);//将值放进去
Set<String> set = Bigmap.keySet();//获取大的集合键的集合
for(String s:set){
System.out.println(s); //遍历
HashMap<String, Integer> set2 =Bigmap.get(s); //获取大集合的值赋给set2
Set<String> minSet = set2.keySet();//keyset方法遍历小集合
for(String s1:minSet){
System.out.println(s1+"--"+set2.get(s1));
}
}
}
}
输出结果:
wz
女娲--10000
荆轲--20
lm
鳄鱼--30
剑姬—20
HashMap集合嵌套ArrayList集合
public class PracticalText3 {
public static void main(String[] args) {
ArrayList<String> List = new ArrayList<String>();
ArrayList<String> List1=new ArrayList<String>();
HashMap<String,ArrayList<String>> map = new HashMap<String,ArrayList<String>>();
List.add("RNG");
List.add("UZI");
List1.add("大嘴");
List1.add("女警");
map.put("lol", List);
map.put("YX", List1); //添加进去
Set<Entry<String,ArrayList<String>>> set =map.entrySet();
for(Entry<String,ArrayList<String>> en:set){
String key = en.getKey();
System.out.println(key);
ArrayList<String> value = en.getValue();
for(Stringsmall:value){
System.out.println(small+"");
}
}
}
}
输出结果:
lol
RNG
UZI
YX
大嘴
女警
HashMap和Hashtable的区别:查看API可以知道
HashMap:线程不安全,效率高.允许null值和null值.
HashTable:线程安全,效率低,不允许null值和null值
Collections类概述:针对集合操作的工具类
方法:
public static <T> voidsort(List<T> list): 排序,默认按照自然顺序
public static <T> intbinarySearch(List<?> list,T key): 二分查找
public static <T> Tmax(Collection<?> coll): 获取最大值
public static void reverse(List<?>list): 反转
public static void shuffle(List<?>list): 随机置换