Map之HashMap集合
多态格式: Map<键的泛型/Object,值的泛型/Object> Map集合自定义对象名 = new HashMap<>();
列如: Map<String,Integer> mapObj = new HashMap<>();
普通格式: HashMap<键的泛型/Object,值的泛型/Object> Map集合自定义对象名 = new HashMap<>();
列如: HaspMap<String,Integer> mapObj = new HashMap<>();
注意:1/同set集合一样,Map不保证元素顺序.
2/如果把泛型设置为所有类型的最高父类Object,那么将表示接下来的’键’/'值’类型将无限制,
但是如果接下来的KetSet/Entry等等集合要使用该集合的话,其泛型也要为’Object’.
二/方法:
1/格式:对象名.put(键,值);
[重点掌握]
列如: mapObj.put("中国",86 );
解释:1/添加一个键,和其键对应的值.键和值的数据类型以及顺序要和Map对象的泛型一致.
2/如果添加的’键’和此前添加的’键’发生重复,那么后来键的值会覆盖之前相同’键’的值.[多用于修改元素]
2/格式:对象名.get(键);
列如:mapObj.get("中国");
解释:根据’键’获取值.
2/格式:对象名.remove(键);
列如:mapObj.remove("美国");
解释:删除键所对应的元素,和其键本身.
3/格式:对象名.size();
列如:mapObj.size();
解释:取集合长度
4/格式:对象名.containsKey(键);
[重点掌握]
列如:boolean result = mapObj.containsKey("美国");
解释:判断集合中是否存在指定的’键;
5/格式:对象名.containsVulue(值);
列如:result = mapObj.containsValue(86);
解释:判断集合中是否存在指定的’值’
6/格式:对象名.clear();
列如:mapObj.clear();
解释:清空集合
2.Map之查询[KeySet集合]
意义:能够获取Map集合中所有的’键’
格式:Set<键的泛型/Object> KeySet集合自定义对象名 = Map集合自定义对象名.KeySet();
列如:Set<String> setMap = mapObj.keySet();
注意:其中左侧’Set’的尖括号’<>'内,'键的泛型’要同Map对象的键泛型一致,如果Map对象的泛型为Object,那么此处需要也为Object.
3.Map之查询[values集合]
意义:能够获取Map集合中所有的’值’
格式:Collection<值的泛型/Object> values集合自定义对象名 = Map集合自定义对象名.values();
列如:Collection<Integer> collectionMap = mapObj.values();
注意:左侧尖括号’<>'内,'值的泛型;要同Map对象的值泛型一致,如果Map对象的泛型为Object,那么此处需要也为Object.
4.Map之查询[Entry双列集合]
意义:能够同时获取Map集合的键和值,故为[双列集合]
格式:Set<Map.Entry<键的泛型/Object,值的泛型/Object>> Entry集合自定义对象名 = Map集合自定义对象名.entrySet();
列如:Set<Map.Entry<String,Student>> setMapObj = hashMapObj.entrySet();
注意:左侧尖括号’<>'内,'值的泛型;要同Map对象的值泛型一致,如果Map对象的泛型为Object,那么此处需要也为Object.
5.Colletions工具类
演示类:NewMap.java
演示HashMap/Map之查询KeySet集合/Map之查询values集合
public class NewMap {
public static void main(String[] args) {
//============================Map之HashMap集合============================
// Map<键的泛型,值的泛型> 自定义对象名 = new HashMap<>();
Map<String,Integer> mapObj = new HashMap<>();
//对象名.put(键,值);
mapObj.put("中国",86 );
mapObj.put("日本",81 );
mapObj.put("美国",2 );
mapObj.put("加拿大",1 );
mapObj.put("美国", 10001);//此键同之前的键发生重复,因此其值将覆盖之前键的值.[多用于修改元素]
// 2/同set集合一样,Map不保证元素顺序. {美国=10001, 中国=86, 加拿大=1, 日本=81}
System.out.println(mapObj);
System.out.println("===============");
//删除键所对应的元素和其本身.
mapObj.remove("美国");
System.out.println(mapObj);
System.out.println("===============");
//取集合长度
System.out.println(mapObj.size());
System.out.println("===============");
//判断集合是否含有指定的键
boolean result = mapObj.containsKey("美国");
System.out.println(result);
System.out.println("===============");
//判断集合是否含有指定的值
result = mapObj.containsValue(86);
System.out.println(result);
System.out.println("===============");
//根据键获取值
Integer zhongguo = mapObj.get("中国");
System.out.println(zhongguo);
System.out.println("===============");
//============================Map之查询[KeySet]============================
//获取其集合所有的'键'
//格式:Set<键的泛型> KeySet集合自定义对象名 = Map集合自定义对象名.KeySet();
Set<String> setMap = mapObj.keySet();
System.out.println(setMap);
System.out.println("===============");
//============================Map之查询[values]============================
//获取其集合所有的'值'
//Collection<值的泛型> values集合自定义对象名 = Map集合自定义对象名.values();
Collection<Integer> collectionMap = mapObj.values();
System.out.println(collectionMap);
System.out.println("===============");
//清空集合
mapObj.clear();
System.out.println(mapObj);
System.out.println("===============");
}
}
演示类:Entry.java
1/键为String,值为对象:利用实现KeySet + get()/Entry来完成Map集合中’键’和’值’的遍历
2/键为对象,值为String:利用实现KeySet + get()来完成Map集合中’键’和’值’的遍历
元素:姓名 年龄 地址
要求:如果存在姓名和年龄相同的键,那么让其值覆盖前者.
实现:需要重写方法.
public class Entry {
public static void main(String[] args) {
//entry小练习
Map<String,Integer> mapObj = new HashMap<>();
mapObj.put("韩国", 37);
mapObj.put("中国",86 );
mapObj.put("日本",81 );
mapObj.put("美国",2 );
mapObj.put("加拿大",1 );
Set<Map.Entry<String,Integer>> entrySetObj = mapObj.entrySet();
for (Map.Entry<String, Integer> stringIntegerEntry : entrySetObj) {
System.out.println(stringIntegerEntry.getKey()+" "+stringIntegerEntry.getValue());
}
System.out.println("==================================");
存对象练习
// 1/键为String,值为对象:利用实现KeySet + get()/Entry来完成Map集合中'键'和'值'的遍历
Student studentOne = new Entry().new Student("李京",17);
Student studentTwo = new Entry().new Student("张蕈",23);
Student studentThree = new Entry().new Student("刘菲",21);
Student studentFour = new Entry().new Student("郭轲",17);
Student studentFive = new Entry().new Student("董喆",18);
Map<String,Student> hashMapObj = new HashMap<>();
hashMapObj.put("2019期01号",studentOne );
hashMapObj.put("2019期02号",studentTwo );
hashMapObj.put("2019期03号",studentThree );
hashMapObj.put("2019期04号",studentFour );
hashMapObj.put("2019期05号",studentFive );
//方式一 Entry双列集合
Set<Map.Entry<String,Student>> setMapObj = hashMapObj.entrySet();
for (Map.Entry<String, Student> stringStudentEntry : setMapObj) {
System.out.println("键内容:"+stringStudentEntry.getKey()+" 值的对象姓名内容:"+stringStudentEntry.getValue().getName()+" 值的对象年龄内容:"+stringStudentEntry.getValue().getAge());
}
System.out.println("==================");
//方式二 KeySet获取所有键 + get();根据键获取期值;
Set<String> ketSetObj = hashMapObj.keySet();
for (String s : ketSetObj) {
System.out.println("键:"+s+" 对应的值对象之姓名:"+hashMapObj.get(s).getName()+" 对应的值对象之年龄:"+hashMapObj.get(s).getAge());
}
System.out.println("==================");
// 2/键为对象,值为String:利用实现KeySet + get()/Entry来完成Map集合中'键'和'值'的遍历
// 元素:姓名 年龄 地址
// 要求:如果存在姓名和年龄相同的键,那么让其值覆盖前者.
StudentTwo studentTwo1 = new Entry().new StudentTwo("刘亦菲",19);
StudentTwo studentTwo2 = new Entry().new StudentTwo("赵丽颖",22);
StudentTwo studentTwo3 = new Entry().new StudentTwo("张天爱",25);//成员变量重复
StudentTwo studentTwo4 = new Entry().new StudentTwo("黄圣依",21);
StudentTwo studentTwo5 = new Entry().new StudentTwo("张天爱",25);//成员变量重复
HashMap<StudentTwo,String> mapObjTwo = new HashMap<>();
mapObjTwo.put(studentTwo1, "北京");//刘亦菲
mapObjTwo.put(studentTwo2, "东京");//赵丽颖
mapObjTwo.put(studentTwo3, "南京");//张天爱
mapObjTwo.put(studentTwo4, "上海");//黄圣依
mapObjTwo.put(studentTwo5, "广州");//张天爱
Set<StudentTwo> setMapObjTwo = mapObjTwo.keySet();
for (StudentTwo two : setMapObjTwo) {
System.out.println("姓名:"+two.getName()+" 年龄:"+two.getAge()+" 地址:"+mapObjTwo.get(two));
}
}
class StudentTwo{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public StudentTwo() {
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
StudentTwo that = (StudentTwo) o;
if (name != null ? !name.equals(that.name) : that.name != null) return false;
return age != null ? age.equals(that.age) : that.age == null;
}
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + (age != null ? age.hashCode() : 0);
return result;
}
public StudentTwo(String name, Integer age) {
this.name = name;
this.age = age;
}
private Integer age;
}
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() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
}
}
演示类:HashMapOfArrayList.java Map嵌套ArrayList集合
public class HashMapOfArrayList {
public static void main(String[] args) {
HashMap<String, ArrayList> hashMapObj = new HashMap<>();
ArrayList<String> arrayListObj = new ArrayList<>();
arrayListObj.add("中国");
arrayListObj.add("日本");
arrayListObj.add("韩国");
arrayListObj.add("美国");
arrayListObj.add("加拿大");
hashMapObj.put("国家", arrayListObj);
ArrayList<String> arrayListObjTwo = new ArrayList<>();
arrayListObjTwo.add("宇宙");
arrayListObjTwo.add("地球");
arrayListObjTwo.add("板块");
arrayListObjTwo.add("国家");
hashMapObj.put("范围", arrayListObjTwo);
ArrayList<String> arrayListObjThree = new ArrayList<>();
arrayListObjThree.add("月球");
arrayListObjThree.add("火星");
arrayListObjThree.add("木星");
arrayListObjThree.add("水星");
hashMapObj.put("星际", arrayListObjThree);
Set<String> setObj = hashMapObj.keySet();//获取map集合的所有键
for (String s : setObj) {
ArrayList<String> a = hashMapObj.get(s);//通过键获取值
System.out.println(" 概述之键:"+s);
for (String s1 : a) {
System.out.println("目标之值:"+s1);
}
};
}
}
演示类:MapErgodic.java 二种遍历
public class MapErgodic {
public static void main(String[] args) {
Map<String,Integer> mapObj = new HashMap<>();
mapObj.put("中国",86 );
mapObj.put("日本",81 );
mapObj.put("美国",2 );
mapObj.put("加拿大",1 );
mapObj.put("新加坡",1 );
//方式一
//Set<键的泛型/Object> KeySet集合自定义对象名 = Map集合自定义对象名.KeySet(); 能够获取Map集合中所有的'键'
Set<String> setMapObj = mapObj.keySet();
//方式二
//对象名.get(键); 根据'键'获取值.
for (String s : setMapObj) {
Integer zhi = mapObj.get(s);//根据所有的键获取所有的值
System.out.println("键为:"+s+" 值为:"+zhi);
}
}
}
演示类:CollectionsOfList.java Collections工具应用
要求:用Collections工具的sort方法将对象中的年龄排序.
public class CollectionsOfList {
public static void main(String[] args) {
Student studentOne = new CollectionsOfList().new Student("李京",12);
Student studentTwo = new CollectionsOfList().new Student("按照",13);
Student studentThree = new CollectionsOfList().new Student("擦啊",21);
Student studentFour = new CollectionsOfList().new Student("终归",9);
ArrayList<Student> ar = new ArrayList<>();
ar.add(studentOne);
ar.add(studentTwo);
ar.add(studentThree);
ar.add(studentFour);
Collections.sort(ar, new Comparator<Student>() {//此处要研究格式
@Override
public int compare(Student o1, Student o2) {
int num = o1.getAge()-o2.getAge();
return num;
}
});
for (Student student : ar) {
System.out.println(student.getName()+" "+student.getAge());
}
}
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() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
}
}
演示类:ArrayListOfhashMap.java ArrayList嵌套Map集合.
ArrayList将Map集合当作元素嵌套.
public class ArrayListOfhashMap {
public static void main(String[] args) {
//ArrayList将Map集合当作元素嵌套.
ArrayList<HashMap> MapOfNesting = new ArrayList<>();
HashMap<Object, Object> hashMapObjOne = new HashMap<>();
hashMapObjOne.put(1, "真");
hashMapObjOne.put(2, "上");
hashMapObjOne.put(3, "下");
hashMapObjOne.put(4, "假");
hashMapObjOne.put(5, "中");
HashMap<Object,Object> hashMapObjTwo = new HashMap<>();
hashMapObjTwo.put(55, "东");
hashMapObjTwo.put(44, "南");
hashMapObjTwo.put(33, "西");
hashMapObjTwo.put(22, "北");
hashMapObjTwo.put(11, "中");
HashMap<Object,Object> hashMapObjThree = new HashMap<>();
hashMapObjThree.put("真真假假", "假假真真");
hashMapObjThree.put("多多少少", "不多不少");
hashMapObjThree.put("色即是空", "空即是色");
hashMapObjThree.put("天南地北", "两岸三地");
hashMapObjThree.put("世界大同", "混沌初始");
MapOfNesting.add(hashMapObjOne);
MapOfNesting.add(hashMapObjTwo);
MapOfNesting.add(hashMapObjThree);
for (HashMap hashMap : MapOfNesting) {//此处的'hashMap'是从ArrayList
Set<Object> getAllMap = hashMap.keySet();
for (Object s : getAllMap) {//这里的's'是对应着'getAllMa'的键.
System.out.println("键:"+s+" 值:"+hashMap.get(s));
}
}
}
}