Map(双列集合的根接口)
1.介绍:
1.以键值对形势保存数据
2.键值唯一
2.Hashset 和 HashMap 之间的联系
1. 底层都是哈希算法 根据面向对象来分析 一套算法 俩类使用
2. HashSet底层 依赖 HashMap 去实现
3. 添加到set 中的值 实际上是添加 到 map 中健的值
4. HashMap:有去重功能 说的是健
5. TreeMap:有排序功能 说的也是健
3.Map的添加方法
public class text {
public static void main (String[] args) {
HashMap<String, Integer>map = new HashMap<>();
Integer i1 = map.put("东" , 19 );
Integer i2 = map.put("南" , 20 );
Integer i3 = map.put("西" , 21 );
Integer i4 = map.put("北" , 22 );
Integer i5 = map.put("北" , 23 );
System.out .println(map);
System.out .println(i1);
System.out .println(i2);
System.out .println(i3);
System.out .println(i4);
System.out .println(i5);
}
}
运行结果:
{南=20 , 北=23 , 东=19 , 西=21 }
null
null
null
null
22
结论: 返回的是被替换(覆盖)的值
4.判断键值key是否存在map;
public class text {
public static void main (String[] args) {
HashMap<String, Integer>map = new HashMap<>();
Integer i1 = map.put("东" , 19 );
Integer i2 = map.put("南" , 20 );
Integer i3 = map.put("西" , 21 );
Integer i4 = map.put("北" , 22 );
boolean b1 = map.containsKey("南" );
System.out .println(b1);
System.out .println(map);
运行结果:
true
{南=20 , 北=22 , 东=19 , 西=21 }
5.判断值value是否存在map
boolean b2 = map.containsValue (20 )
System.out .println (b2)
运行结果: true
6.通过键值查询对应的值
Integer c1 = map .get("东" );
Integer c2 = map .get("中" );
System.out .println(c1);
System.out .println(c2);
运行结果:
19
null
7.获取所有key键值的Set集合
Set <String>keySet = map.keySet ()
System.out .println (keySet)
运行结果:
[南, 北, 东, 西]
8.获取所有值value的集合
Collection<Integer>values = map .values ();
System.out.println(values );
运行结果:
[20 , 22 , 19 , 21 ]
9.通过键值key删除整个键值对
Integer remove = map.remove ("振南" );
System.out.println(remove );
运行结果:
20
{北=22 , 东=19 , 西=21 }
10.清空
map.clear ()
System.out .println (map)
运行结果:
{}
}
}
11.map遍历
public class text {
public static void main (String[] args) {
HashMap<String, Integer>map = new HashMap<>();
map.put("东" , 19 );
map.put("南" , 20 );
map.put("西" , 21 );
map.put("北" , 22 );
System.out .println(map);
Set<String>keySet = map.keySet();
System.out .println(keySet);
Iterator<String> iterator = keySet.iterator();
while (iterator.hasNext()) {
String key = iterator.next();
Integer value = map.get (key);
System.out .println(key + "=" + value );
}
Collection<Integer>values = map.values();
System.out .println(values);
}
}
运行结果:
{南=20 , 北=22 , 东=19 , 西=21 }
[南, 北, 东, 西]
南=20
北=22
东=19
西=21
[20 , 22 , 19 , 21 ]
12.增强for循环遍历for
public class text {
public static void main (String[] args) {
HashMap<String, Integer>map = new HashMap<>();
map.put("东" , 19 );
map.put("南" , 20 );
map.put("西" , 21 );
map.put("北" , 22 );
Set<String>keySet = map.keySet();
for (String key : keySet) {
Integer integer = map.get (key);
System.out .println(key+integer);
}
}
}
运行结果:
南20
北22
东19
西21
13.迭代器遍历map(Entry==从map中取出整个键值对)
public class text {
public static void main (String[] args) {
HashMap<String, Integer>map = new HashMap<>();
map.put("东" , 19 );
map.put("南" , 20 );
map.put("西" , 21 );
map.put("北" , 22 );
Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
Iterator<Map.Entry<String, Integer>> iterator = entrySet.iterator();
while (iterator.hasNext()) {
Entry<String, Integer> entry = iterator.next();
Integer value = entry.getValue();
String key = entry.getKey();
System.out .println(value +"--" + key);
}
}
}
运行结果:
20 --南
22 --北
19 --东
21 --西
13.增强for循环遍历map(Entry==从map中取出整个键值对)
public class text {
public static void main (String[] args) {
HashMap<String, Integer> map = new HashMap<>();
map.put("振北" , 20 );
map.put("振南" , 20 );
map.put("振西" , 21 );
map.put("振东" , 22 );
Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
for (Map.Entry<String, Integer> entry : entrySet ) {
String string = entry.getKey();
Integer integer = entry.getValue();
System.out .println(string + "=" + integer);
}
}}
运行结果:
振东=22
振北=20
振南=20
振西=21
14.map存对象
public class text {
public static void main (String[] args) {
HashMap<Student , String>hashMap = new HashMap<>();
hashMap.put(new Student("哈哈" ,12 ),"安徽" );
hashMap.put(new Student("小小" ,13 ),"安徽" );
hashMap.put(new Student("额额" ,15 ),"安徽" );
hashMap.put(new Student("小小" ,13 ),"安心" );
System.out .println(hashMap);
}}
运行结果:
{Student [name=额额, age=15 ]=安徽, Student [name=哈哈, age=12 ]=安徽, Student [name=小小, age=13 ]=安心}
Student类重写部
Student类重写部分:
@Override
public int compareTo (Student o) {
int num = this .age - o.getAge();
return num == 0 ? 1 : num;
@Override
public int hashCode () {
final int prime = 31 ;
int result = 1 ;
result = prime * result + age;
result = prime * result + ((name == null ) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals (Object obj) {
if (this == obj)
return true ;
if (obj == null )
return false ;
if (getClass() != obj.getClass())
return false ;
Student other = (Student) obj;
if (age != other.age)
return false ;
if (name == null ) {
if (other.name != null )
return false ;
} else if (!name.equals(other.name))
return false ;
return true ;
}
1.LinkedHashMap储存信息(按原输入顺序排序)
public class text {
public static void main (String[] args) {
LinkedHashMap<String, Integer> map = new LinkedHashMap<>();
map.put("哈哈" , 11 );
map.put("小小" , 12 );
map.put("多多" , 11 );
map.put("欧欧" , 11 );
System.out .println(map);
}}
运行结果:
{哈哈=11 , 小小=12 , 多多=11 , 欧欧=11 }
1.1创建一个TreeMaap 保存学生 value保存户籍,按年龄排序
public class text {
public static void main(String[] args) {
TreeMap<Student, String>map = new TreeMap<>();
map .put(new Student("丁鹏" ,11 ),"安徽" );
map .put(new Student("哈哈" ,15 ),"阿萨斯" );
map .put(new Student("小小" ,145 ),"斯蒂芬" );
map .put(new Student("人人" ,14 ),"塞上风" );
map .put(new Student("乖乖" ,71 ),"人工岛" );
System.out.println(map );
}}
运行结果:
{Student [name=丁鹏, age=11 ]=安徽,
Student [name=人人, age=14 ]=塞上风,
Student [name=哈哈, age=15 ]=阿萨斯,
Student [name=乖乖, age=71 ]=人工岛,
Student [name=小小, age=145 ]=斯蒂芬}
2.随机交换集合中值的位置(洗牌)
public class text {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
list .add(12 );
list .add(12 );
list .add(12 );
list .add(16 );
Collections.shuffle(list );
System.out.println(list );
运行结果:
[12 , 12 , 16 , 12 ]
3.排序
Collections.sort (list)
System.out .println (list)
运行结果:
[12 , 12 , 12 , 16 ]
4.二分查找
int index = Collections.binarySearch(list, 16 );
System.out.println(index );
运行结果:
3
5.反转集合
Collections.reverse (list)
System.out .println (list)
运行结果:
[16 , 12 , 12 , 12 ]
}}
6.排序
ArrayList<Student> list = new ArrayList<>();
list .add(new Student("小小" ,12 ));
list .add(new Student("多多" ,13 ));
list .add(new Student("哈哈" ,11 ));
Collections.sort(list );
System.out.println(list );
运行结果:
[Student [name=哈哈, age=11 ], Student [name=小小, age=12 ], Student [name=多多, age=13 ]]