09Java语法回顾map集合

时间:2023-02-15 17:55:34

Java语法回顾Map集合


读了那么多年的书让我明白一个道理。人要稳重,不要想到啥就做啥。做一行越久即使你不会,几年之后慢慢的你也会了,加上一点努力你或许你能成为别人眼中的专家。

Map集合的简介

/*
* Map:最大的优点就是体现对应关系。
* Map是一个键值对形式的集合。它的数据不再是单个的了,必须同时有键和值组成。
*
* Map和Collection的区别?
* Map:是(键值对)双列形式的集合;键必须是唯一的,不能重复,值可以重复;看成是夫妻对的集合。
* Collection:是单列值的集合;Collection的List儿子,是可以重复,它的Set儿子是唯一的;看成是单身汉的集合。
*
* Map接口的功能:
* A:增加功能
* V put(K key,V value):当key在集合中不存在时,添加元素;当key在集合存在时候,替换元素。
* B:删除功能
* void clear():清除所有键值对数据。
* V remove(Object key):根据指定的键删除键值对。
* C:判断功能
* boolean containsKey(Object key):判断指定的键是否在集合中存在
* boolean containsValue(Object vlaue):判断指定的值是否在集合中存在
* boolean isEmpty():判断集合是否为空
* D:获取功能
* Set<Map.Entry<K,V>> entrySet():键值对对象的集合。
* Object get(Object key):根据键获取值
* Set<K> keySet():所有键的集合
* Collection<V> values():所有值的集合
* E:长度功能
* int size()
*
* 注意:Map集合中的具体实现类的数据结构,是针对键有效。
*/

Map集合的遍历

/*
* Set<K> keySet():所有键的集合
* Collection<V> values():所有值的集合
*/

Map集合的遍历的代码测试

public class MapDemo2 {
public static void main(String[] args) {
// 创建集合对象
Map<Integer, String> map = new HashMap<Integer, String>();

// 创建并添加元素
map.put(1, "貂蝉");
map.put(2, "黄忠");
map.put(3, "赵云");

// 显示数据
// System.out.println(map);

// Set<K> keySet():所有键的集合
Set<Integer> keySet = map.keySet();
for (Integer key : keySet) {
System.out.println(key);
}
//所有值的集合
Collection<String> values = map.values();
for (String value : values) {
System.out.println(value);
}
}

Map集合的遍历方式二的代码测试

/*
* Object get(Object key):根据键获取值
* Map集合的遍历。
* 方式1:丈夫找妻子
* A:把所有丈夫给集中起来。Set<K> keySet()
* B:遍历丈夫集合,获取到每一个丈夫。迭代器,增强for
* C:让丈夫去找妻子。get(Object key)
*/

Code
public class MapDemo3 {
public static void main(String[] args) {
// 创建集合对象
Map<String, String> map = new HashMap<String, String>();

// 创建并添加元素
map.put("杨过", "小龙女");
map.put("郭靖", "黄蓉");
map.put("梁山伯", "祝英台");
map.put("牛郎", "织女");
// 遍历
// A:把所有丈夫给集中起来。Set<K> keySet()
Set<String> husbandSet = map.keySet();
Iterator<String> iterator = husbandSet.iterator();
whiel(iterable.hasNext){
String key = iterable.next;
String value = map.get(key);
System.out.println(key+"---"+value);
}
// B:遍历丈夫集合,获取到每一个丈夫。迭代器,增强for
for (String husband : husbandSet) {
// C:让丈夫去找妻子。get(Object key)
String wife = map.get(husband);
System.out.println(husband + "***" + wife);
}
}

Map集合的遍历方式(Map.Entry)的代码测试

/*
* Map集合的遍历。
* 通过Map.Entry找key和value。
* A:获取所有Map.Entry的集合。Set<Map.Entry<K,V>> entrySet()
* class Map.Entry<K,V>
* {
* private K key;
* private V value;
*
* public Map.Entry(K key,V value)
* {
* this.key = key;
* this.value = value;
* }
*
* public K getKey()
* {
* return key;
* }
*
* public V getValue()
* {
* return value;
* }
* }
* B:遍历Map.Entry集合,获取到每一个Map.Entry对象。迭代器,增强for。
* C:通过Map.Entry对象获取key和value。
*/

Code部分

public class MapDemo4 {
public static void main(String[] args) {
// 创建集合对象
Map<String, String> map = new HashMap<String, String>();

// 创建并添加元素
map.put("杨过", "小龙女");
map.put("郭靖", "黄蓉");
map.put("梁山伯", "祝英台");
map.put("牛郎", "织女");

// Set<Map.Entry<K,V>> entrySet():键值对对象的集合。
//map.entrySet
Set<Entry<String,String>> entrySet = map.entrySet();
for (Entry<String, String> entry : entrySet) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key+"****"+value);
}
}
}

HashMap存储自定义对象并遍历

/*
* HashMap存储键和值。并遍历。
* 键:String 学号
* 值:Student (name,age)
*/

CODE
public class HashMapDemo2 {
public static void main(String[] args) {
// 创建集合对象
HashMap<String, Student> hm = new HashMap<String, Student>();

// 创建元素对象
Student s1 = new Student("李世民", 30);
Student s2 = new Student("朱元璋", 40);
Student s3 = new Student("武则天", 50);

// 添加元素
hm.put("it001", s1);
hm.put("it002", s2);
hm.put("it003", s3);

// 遍历
Set<String> keySet = hm.keySet();
for (String key : keySet) {
Student student = hm.get(key);
System.out.println(key+"****"+student.getName()+student.getAge());
}
// 遍历2
Set<Map.Entry<String, Student>> hmSet = hm.entrySet();
for (Map.Entry<String, Student> me : hmSet) {
String key = me.getKey();
Student value = me.getValue();
System.out.println(key + "***" + value.getName() + "***"
+ value.getAge());
}
}
}

TreeMap集合(有序的)

/*
* TreeMap存储自定义对象并遍历。
* 键:Student
* 值:String
*
* 如果一个自定义对象做键,用TreeMap集合。
* 就必须要实现排序。
* 两种方式:
* A:让自定义对象所属的类去实现Comparable接口
* B:使用带参构造方法,创建TreeMap,接收Comparator接口参数。
*/

CODE
public class TreeMapDemo2 {
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 num = s2.getAge() - s1.getAge();
int num2 = (num == 0) ? s1.getName().compareTo(
s2.getName()) : num;
return num2;
}
});

// 创建元素对象
Student s1 = new Student("李世民", 30);
Student s2 = new Student("朱元璋", 40);
Student s3 = new Student("武则天", 50);
Student s4 = new Student("武则天", 50);
Student s5 = new Student("武则天", 30);

// 添加元素
tm.put(s1, "it001");
tm.put(s2, "it002");
tm.put(s3, "it003");
tm.put(s4, "it004");
tm.put(s5, "it005");

// 遍历
Set<Student> set = tm.keySet();
for (Student key : set) {
String value = tm.get(key);
System.out.println(key.getName() + "***" + key.getAge() + "***"
+ value);
}
}
}

Hashtable和HashMap的区别

/*
* HashtableHashMap的区别?
* A:Hashtable线程安全,效率低;不允许null键和值。
* B:HashMap线程不安全,效率高;允许null键和值。
*/

HashMap集合循环嵌套使用

/*
* czbk:
* yr 预热班
* jy 就业班
* 预热班:
* 01 zhangsan
* 02 lisi
* 就业班:
* 01 wangwu
* 02 zhaoliu
*
* Map的嵌套。
*/

HashMap集合循环嵌套使用代码测试

public class HashMapDemo {
public static void main(String[] args) {
//最外层HashMap czbk
HashMap<String, HashMap<String, String>> czbk = new HashMap<String, HashMap<String, String>>();
//预热班HashMap yr
HashMap<String, String> yr = new HashMap<String,String>();
yr.put("01", "zhangsan");
yr.put("02", "lisi");
//就业班HashMap jy
HashMap<String, String> jy = new HashMap<String,String>();
jy.put("01", "wangwu");
jy.put("02", "zhaoliu");
czbk.put("就业班", jy);
czbk.put("预热班", yr);
//获取总集合 键的集合
Set<String> keySet = czbk.keySet();
for (String key : keySet) {
System.out.println(key);
//根据键的集合获取值
HashMap<String, String> czbkKey = czbk.get(key);
// 内部还是一个集合。再获取内部集合的键
Set<String> bjKey = czbkKey.keySet();
for (String key2 : bjKey) {
String value = czbkKey.get(key2);
System.out.println("\t"+key2+" "+value);
}
}
}
}