Map
是一种键-值对(key-value)
集合,Map 集合中的每一个元素都包含一个键对象和一个值对象
。其中,键对象不允许重复,而值对象可以重复,并且值对象还可以是 Map 类型的,就像数组中的元素还可以是数组一样。
Map 接口主要有两个实现类:HashMap 类和 TreeMap 类。
其中,HashMap 类按哈希算法来存取键对象,而 TreeMap 类可以对键对象进行排序。
Map接口的常用方法
方法 | Value |
---|---|
get(Object key) | 返回 Map 集合中指定键对象所对应的值。V 表示值的数据类型 |
V put(K key, V value) | 向 Map 集合中添加键-值对,返回 key 以前对应的 value,如果没有, 则返回 null |
V remove(Object key) | 从 Map 集合中删除 key 对应的键-值对,返回 key 对应的 value,如果没有,则返回null |
Set entrySet() | 返回 Map 集合中所有键-值对的 Set 集合,此 Set 集合中元素的数据类型为 Map.Entry |
Set keySet() | 返回 Map 集合中所有键对象的 Set 集合 |
boolean containsKey(Object key) | 检出map集合中有没有包含Key为key的元素,如果有则返回true,否则返回false |
boolean containsValue(Object value) | 检出map集合中有没有包含Value为value的元素,如果有则返回true,否则返回false |
Set<Map.Entry<K,V>> entrySet() | 返回map到一个Set集合中,以map集合中的Key=Value的形式返回到set中。 |
boolean equals(Object o) | 判断两个Set集合的元素是否相同 |
int hashCode() | 返回map集合的哈希码值 |
boolean isEmpty() | 检出map集合中是否有元素,如果没有则返回true,如果有元素则返回false |
hashMap的理解hashMap
Map集合
自定义义key
public class mapDemo2 {
public static void main(String[] args) {
//创建map集合在MyKey为key,Cat为Value上
Map<MyKey,Cat>map=new HashMap<MyKey,Cat>();
//集合内添加元素
map.put(new MyKey("tom-1"),new Cat("tom-1",12));
map.put(new MyKey("tom-2"),new Cat("tom-2",13));
map.put(new MyKey("tom-3"),new Cat("tom-3",14));
//判断是否存在
Cat c=map.get(new MyKey("tom-1"));
System.out.println(c);
}
}
//Cat类
class Cat{
private String name;
private int height;
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public Cat(String name, int height) {
super();
this.name = name;
this.height = height;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int hashCode() {
return 1;
}
public boolean equals(Object obj) {
return true;
}
}
//MyKey类
class MyKey{
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public MyKey(String name) {
super();
this.name = name;
}
public int hashCode(){
int namehash=name==null?0:name.hashCode();
int agehash=age==null?0:age.intValue();
return namehash+agehash;
}
public boolean equals(Object obj) {
MyKey key0=(MyKey)obj;
return name.equals(key0.name)&&age==key0.age;
}
}
结果:
fanyongjun.Cat@1
如果不创建MyKey类的话,重建一个寻找集合内的是找不到的,重新的也要重写hashcode()和equals()方法
简单的迭代map和key迭代
迭代map
Set<Entry<Integer,String>>entires=map.entrySet();
//增强for循环
for(Entry<Integer,String>entry : entires) {
Integer key=entry.getKey();
String value=entry.getValue();
System.out.println(key+"-"+value);
}
迭代key
Set<Integer>keys=map.keySet();
for(Integer key:keys) {
System.out.println(key+"=="+map.get(key));
}
集合的嵌套非Map
创建一个学校,6个年级,每个年级10个班级,每个班级60人
publiic class Demo{
public static void main(String []args){
//1.班级是名单(String)集合每个班60人
List<String>name=null;
//2:年级是班级集合,每个年级10个班
List<List<String>>grade=null;
//3:学校:6个年级
//创建学校:年级的集合
List<List<List<String>>>school=new ArrayList<List<List<String>>>();
for(int i=1;i<=6;i++){
//创建每个班级
grade=new ArrayList<List<String>>();
school.add(grade);
//处理每个年级的班级集合
for(int j=1;j<=10;j++){
//创建班级
name=new ArrayList<String>();
grade.add(name);
//每个人
for(int k=1;k<=60;k++){
name.add("toom-"+i+"-"+j+"-"+k);
}
}
//输出集合
outName(school);
}
}
public static void outName(List<List<List<String>>>allNames) {
//年级集合
for(int i=0;i<allNames.size();i++) {
//取得当前的年纪集合
List<List<String>>grade=allNames.get(i);
//每个年级就是班级集合
for(int j=0;j<grade.size();j++) {
List<String>classes=grade.get(j);
//每个班级,是集合
for(int k=0;k<classes.size();k++) {
System.out.println(classes.get(k));
}
}
}
}
//使用迭代器输出
public static void outNames2(List<List<List<String>>>allNames)
{
Iterator<List<List<String>>>it=allNames.iterator();
while(it.hasNext()) {
//每个年级
List<List<String>>grade=it.next();
//班级
Iterator<List<String>>classesIt=grade.iterator();
while(classesIt.hasNext()) {
List<String>names=classesIt.next();
Iterator<String>nameIt=names.iterator();
while(nameIt.hasNext()) {
System.out.println(nameIt.next());
}
}
}
}
}
集合的嵌套Map
先创建一个学生类:
public class Student {
private int age;
private int height;
private int weight;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public Student(int age, int height, int weight) {
super();
this.age = age;
this.height = height;
this.weight = weight;
}
//重写的原因参考之前的文章
public int hashCode() {
return this.height | this.weight<<8|this.age<<16;
}
public boolean equals(Object obj) {
if (obj==null) return false;
if(obj==this) return true;
if(obj instanceof Student) {
Student s0=(Student)obj;
return this.hashCode()==obj.hashCode();
}
return false;
}
}
App:
public class App {
public static void main(String []args) {
Student s1=new Student(12,13,14);
Student s2=new Student(12,13,14);
System.out.println(s1.equals(s2));
// 16进制 System.out.println(Integer.toHexString(s1.hashCode()));
//2进制 System.out.println(Integer.toBinaryString(s1.hashCode()));
}
}
结果:
true
c0e0d
11000000111000001101
Map集合的嵌套
public class MapNestedDemo {
public static void main(String []args) {
//名单集合
Map<String,String>names=null;
//班级集合
Map<String,Map<String,String>> classes=null;
//初始化年级集合
Map<String,Map<String,Map<String,String>>>grades=new HashMap<String,Map<String,Map<String,String>>>();
int no=0;
for(int i=1;i<=2;i++) {
//创建班级集合
classes=new HashMap<String,Map<String,String>>();
grades.put(i+"年级",classes);
for(int j=1;j<=3;j++) {
//创建名单集合---一个班
names=new HashMap<String,String>();
classes.put(i+"年纪"+j+"班",names);
for(int k=1;k<=4;k++) {
names.put("No"+i+"-"+j+"-"+k,"tom-"+(no++));
}
}
}
//迭代输出
for(Entry<String, Map<String, Map<String, String>>> entry:grades.entrySet()) {
//年级
String key=entry.getKey();
//班级集合
Map<String,Map<String,String>>classes0=entry.getValue();
//先输出年级名称
System.out.println(key);
//输出班级
for(Entry<String,Map<String,String>>e:classes0.entrySet()) {
//班级名称
String className=e.getKey();
//名单集合
Map<String,String> v=e.getValue();
System.out.println("\t"+className);
//名单集合
for(Entry<String,String>e2:v.entrySet()) {
String stuNo=e2.getKey();
String stuName=e2.getValue();
System.out.println("\t\t"+stuNo+"-"+stuName);
}
}
}
//迭代器
Iterator<Entry<String,Map<String,Map<String,String>>>>it1=grades.entrySet().iterator();
while(it1.hasNext()) {
Entry<String,Map<String,Map<String,String>>>e1=it1.next();
String k1=e1.getKey();//年级名称
Map<String,Map<String,String>> v1=e1.getValue();
System.out.println(k1);
//班级集合迭代
Iterator<Entry<String,Map<String,String>>>it2=v1.entrySet().iterator();
while(it2.hasNext()) {
Entry<String,Map<String,String>>e2=it2.next();
String k2=e2.getKey();//班级名称
Map<String,String>v2=e2.getValue();//一个班级
System.out.println("\t"+k2);
Iterator<Entry<String,String>>it3=v2.entrySet().iterator();
while(it3.hasNext()) {
Entry<String,String>e3=it3.next();
String k3=e3.getKey();
String v3=e3.getValue();
System.out.println("\t\t"+k3+"-"+v3);
}
}
}
}
}
set集合元素重复判断
元素重复的判断标准p.hash=hash&&((k=p.key)==key||(key!=null&&key.equals(k)));
11:(p.hash==hash&&((k=p.key)==key||(key!=null&&key.equals(k))));
a:new hash不同,对象不同
b:new hash相同,对象是同一对象,则相同对象
c: :new hash相同,对象不是同一对象 ,在判断equals方法,若相同则相同