一:Map(掌握)
(1)Map
将键映射到值的对象。一个映射不能包含重复的键;每个键最多只能映射到一个值。
(2)Map和Collection的区别?
A:Map 存储的是键值对形式的元素,键唯一,值可以重复。夫妻对
B:Collection 存储的是单独出现的元素,子接口Set元素唯一,子接口List元素可重复。光棍
(3)Map接口功能概述
A:添加功能: V put(K key,V value)
B:删除功能:
void clear():移除所有的键值对元素
V remove(Object key):根据键删除键值对元素,并返回值
C:判断功能:
boolean containsKey(Object key):判断集合是否包含指定的键
boolean containsValue(Object value):判断集合是否包含指定的值
boolean isEmpty():判断集合是否为空
D:获取功能:
V get(Object key):根据键获取值
Set<K> KeySet():获取集合中所有键的集合
Collection<V> values():获取集合中所有值得集合
Set<Map.Entry<K,V>> entrySet():返回的是键值对对象的集合
E:长度功能:int size():返回集合中键值对的对数
(4)Map集合的遍历
方法一:键找值
a:获取所有键的集合
b:遍历键的集合,得到每一个键
c:根据键到集合中找值
代码体现
public class MapDemo {
public static void main(String[] args) {
Map<String,String> hm = new HashMap<String,String>();
hm.put("001","hello");
hm.put("002","world");
hm.put("003","java");
// 获取所有键的集合
Set<String> set = hm.keySet();
// 遍历键的集合
for(String key : set) {
// 根据键到集合中找值
String value = hm.get(key);
System.out.println(key+"-----"+value);
}
}
}
方法二:键值对对象找键和值
a:获取所有键值对对象的集合
b:遍历键值对对象的集合,获取每一个键值对对象
c:根据键到集合中找值
代码体现
public class MapDemo {
public static void main(String[] args) {
Map<String,String> hm = new HashMap<String,String>();
hm.put("001","hello");
hm.put("002","world");
hm.put("003","java");
// 获取所有键值对对象的集合
Set<Map.Entry<String,String>> set2 = hm.entrySet();
// 遍历键值对对象的集合,获取每一个键值对对象
for(Map.Entry<String,String> me : set2) {
String key = me.getKey();
String value = me.getValue();
System.out.println(key+"-----"+value);
}
(5)HashMap集合的练习
A:HashMap<String,String>
import java.util.HashMap;
import java.util.Set;
public class HashMapTest {
public static void main(String[] args) {
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("001", "西施");
hm.put("002", "貂蝉");
hm.put("003", "王昭君");
hm.put("004", "杨玉环");
hm.put("001", "后裔");
Set<String> set = hm.keySet();
for(String key : set) {
String value = hm.get(key);
System.out.println(key+"-----"+value);
}
}
}
B:HashMap<Integer,String>
import java.util.HashMap;
import java.util.Set;
public class HashMapTest2 {
public static void main(String[] args) {
HashMap<Integer, String> hm = new HashMap<Integer,String>();
hm.put(30, "马云");
hm.put(50, "马化腾");
hm.put(60, "王健林");
hm.put(40, "周星驰");
hm.put(20, "吴亦凡");
Set<Integer> set = hm.keySet();
for(Integer key : set) {
String value = hm.get(key);
System.out.println(key+"-----"+value);
}
}
}
C:HashMap<String,Student>
import java.util.HashMap;
import java.util.Set;
public class HashMapTest3 {
public static void main(String[] args) {
HashMap<String, Student> hm = new HashMap<String,Student>();
Student s1 = new Student("百里守约",27);
Student s2 = new Student("百里玄策",29);
Student s3 = new Student("后裔",30);
Student s4 = new Student("嫦娥",18);
hm.put("T001", s1);
hm.put("T002", s2);
hm.put("T003", s3);
hm.put("T004", s4);
Set<String> set = hm.keySet();
for(String key : set) {
Student value = hm.get(key);
System.out.println(key+"---"+value.getName()+"---"+value.getAge());
}
}
}
D:HashMap<Student,String>
import java.util.HashMap;
import java.util.Set;
public class HashMapTest4 {
public static void main(String[] args) {
HashMap<Student, String> hm = new HashMap<Student, String>();
Student s1 = new Student("刘备",45);
Student s2 = new Student("关羽",40);
Student s3 = new Student("张飞",38);
Student s4 = new Student("赵云",25);
Student s5 = new Student("刘备",45);
hm.put(s1,"6666");
hm.put(s2,"7777");
hm.put(s3,"8888");
hm.put(s4,"9999");
hm.put(s5,"5555");
Set<Student> set = hm.keySet();
for(Student key : set) {
String value = hm.get(key);
System.out.println(key.getName()+"---"+key.getAge()+"---"+value);
}
}
}
(6)TreeMap集合的练习
A:TreeMap<String,String>
import java.util.Set;
import java.util.TreeMap;
public class TreeMapDemo {
public static void main(String[] args) {
TreeMap<String, String> tm = new TreeMap<String, String>();
tm.put("hello", "你好");
tm.put("world", "世界");
tm.put("java", "爪哇");
tm.put("world", "世界2");
tm.put("javaee", "爪哇EE");
Set<String> set = tm.keySet();
for (String key : set) {
String value = tm.get(key);
System.out.println(key + "---" + value);
}
}
}
B:TreeMap<Student,String>
import java.util.Comparator;
import java.util.Set;
import java.util.TreeMap;
public class TreeMapTest {
public static void main(String[] args) {
TreeMap<Student, String> tm = new TreeMap<Student, String>(
new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
int num = s1.getAge() - s2.getAge();
int num2 = num == 0 ? s1.getName().compareTo(s2.getName()) :num;
return num2;
}
});
Student s1 = new Student("潘安", 30);
Student s2 = new Student("柳下惠", 35);
Student s3 = new Student("唐伯虎", 33);
Student s4 = new Student("燕青", 32);
Student s5 = new Student("唐伯虎", 33);
Student s6 = new Student("啊三", 30);
tm.put(s1, "宋朝");
tm.put(s2, "元朝");
tm.put(s3, "明朝");
tm.put(s4, "清朝");
tm.put(s5, "汉朝");
tm.put(s6, "秦国");
Set<Student> set = tm.keySet();
for (Student key : set) {
String value = tm.get(key);
System.out.println(key.getName() + "---" + key.getAge() + "---"
+ value);
}
}
}
(7)练习
A:统计一个字符串中每个字符出现的次数
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
public class TreeMapDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String line = sc.nextLine();
TreeMap<Character, Integer> tm = new TreeMap<Character, Integer>();
char[] chs = line.toCharArray();
for(char ch : chs){
Integer i = tm.get(ch);
if(i == null){
tm.put(ch, 1);
}else {
i++;
tm.put(ch,i);
}
}
StringBuilder sb= new StringBuilder();
Set<Character> set = tm.keySet();
for(Character key : set){
Integer value = tm.get(key);
sb.append(key).append("(").append(value).append(")");
}
String result = sb.toString();
System.out.println("result:"+result);
}
}
B:集合的嵌套遍历
二:Collections(理解)
(1)是针对集合进行操作的工具类
(2)面试题:Collection和Collections的区别
A:Collection 是单列集合的顶层接口,有两个子接口List和Set
B:Collections 是针对集合进行操作的工具类,可以对集合进行排序和查找等
(3)常见的几个小方法:
A:public static <T> void sort(List<T> list)
B:public static <T> int binarySearch(List<?> list,T key)
C:public static <T> T max(Collection<?> coll)
D:public static void reverse(List<?> list)
E:public static void shuffle(List<?> list)
|