大数据之路week02--day03 Map集合、Collections工具类的用法
1、Map(掌握)
(1)将键映射到值的对象。一个映射不能包含重复的键:每个键最多只能映射到一个值。
(2)Map和Collection的区别?
A: Map 存储的是键值对形式的元素,键唯一,值可以重复。 理解为:夫妻对
B: Collection存储的是单独出现的元素,子接口List元素可重复,子接口Set元素唯一。 理解为: 光棍
(3)Map接口功能概述
A: 添加功能
V put(K key, V value) 将指定的值与该映射中的指定键相关联(可选操作)。
B: 删除功能
void clear() 从该地图中删除所有的映射(可选操作)。
V remove(Object key) 如果存在(从可选的操作),从该地图中删除一个键的映射。
C: 判断功能
boolean containsKey(Object key) 如果此映射包含指定键的映射,则返回 true 。
boolean containsValue(Object Value) 如果此地图将一个或多个键映射到指定的值,则返回 true 。
boolean isEmpty() 如果此地图不包含键值映射,则返回 true 。
D: 获取功能
Set<Map.Entry<K,V>> entrySet() 返回此地图中包含的映射的Set视图。
V get(Object key) 返回到指定键所映射的值,或 null如果此映射包含该键的映射。
Set<K> keySet() 返回此地图中包含的键的Set视图。
Collection<V> values() 返回此地图中包含的值的Collection视图。
E: 长度功能
int size() 返回此地图中键值映射的数量。
package com.wyh.mapSummary; import java.util.HashMap;
import java.util.Map;
import java.util.Set; /**
* @author WYH
* @version 2019年11月20日 上午9:44:41
*
* * 1、添加功能
* V put(K key, V value) 将指定的值与该映射中的指定键相关联(可选操作)。
* 2、删除功能
* void clear() 从该地图中删除所有的映射(可选操作)。
* V remove(Object key) 如果存在(从可选的操作),从该地图中删除一个键的映射。
* 3、判断功能
* boolean containsKey(Object key) 如果此映射包含指定键的映射,则返回 true 。
* boolean containsValue(Object Value) 如果此地图将一个或多个键映射到指定的值,则返回 true 。
* boolean isEmpty() 如果此地图不包含键值映射,则返回 true 。
* 4、获取功能
* Set<Map.Entry<K,V>> entrySet() 返回此地图中包含的映射的Set视图。
* V get(Object key) 返回到指定键所映射的值,或 null如果此映射包含该键的映射。
* Set<K> keySet() 返回此地图中包含的键的Set视图。
* Collection<V> values() 返回此地图中包含的值的Collection视图。
* 5、长度功能
* int size() 返回此地图中键值映射的数量。
*/
public class Map01 {
public static void main(String[] args) {
//
Map<String,String> map = new HashMap<String,String>(); //V put(K key, V value) 将指定的值与该映射中的指定键相关联(可选操作)。
map.put("王友虎", "超人");
map.put("赵以浩", "绿巨人");
map.put("李宏灿", "奇异博士");
map.put("齐博源", "雷神"); //void clear() 从该地图中删除所有的映射(可选操作)。
//map.clear(); //V remove(Object key) 如果存在(从可选的操作),从该地图中删除一个键的映射。
//map.remove("王友虎"); //boolean containsKey(Object key) 如果此映射包含指定键的映射,则返回 true 。
//System.out.println("containsKey:"+map.containsKey("王友虎")); //boolean isEmpty() 如果此地图不包含键值映射,则返回 true 。
//System.out.println("isEmpty:"+map.isEmpty()); //int size() 返回此地图中键值映射的数量。
//System.out.println("size:"+map.size()); //Set<K> keySet() 返回此地图中包含的键的Set视图。
Set<String> set = map.keySet();
for(String s : set) {
//V get(Object key) 返回到指定键所映射的值,或 null如果此映射包含该键的映射。
System.out.println(s+"---"+map.get(s));
} System.out.println(map); } }
(4)Map集合的遍历
A: 键找值
a: 获取所有键的集合
b: 遍历键的集合,得到每一个键
c: 根据键到集合中去找值
package com.wyh.map; import java.util.HashMap;
import java.util.Map;
import java.util.Set; /**
* @author WYH
* @version 2019年11月17日 下午7:51:02 *
*
* 根据键找值
*/
public class MapDemo3
{
public static void main(String[] args) {
//创建map集合
Map<String,String> map = new HashMap<String,String>(); //添加元素
map.put("小月", "小美");
map.put("李晨", "范冰冰");
map.put("邓超","孙俪");
map.put("黄晓明", "杨颖"); //获取到所有的key
Set<String> set = map.keySet(); //遍历
for(String key : set) {
String s = map.get(key);
System.out.println(key+"---"+s);
} } }
B: 键值对对象找键和值
a: 获取所有的键值对对象的集合
b: 遍历键值对对象的集合,获取每一个键值对对象
c: 根据键值对对象去获取键和值
package com.wyh.map; import java.util.HashMap;
import java.util.Map;
import java.util.Set; /**
* @author WYH
* @version 2019年11月17日 下午8:13:52
*
* 根据键值对对象找键和值
*/
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<String,String>> set = map.entrySet(); for(Map.Entry<String,String> me : set) {
System.out.println(me.getKey()+"---"+me.getValue());
}
} }
(5)HashMap集合的练习
A: HashMap<String,String>
package com.wyh.mapSummary; import java.util.HashMap;
import java.util.Map;
import java.util.Set; /**
* @author WYH
* @version 2019年11月20日 上午10:38:29
*/
public class Map02 {
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<String, String>> set = map.entrySet(); for (Map.Entry<String, String> me : set) {
System.out.println(me.getKey() + "---" + me.getValue());
}
} }
B: HashMap<Integer,String>
package com.wyh.mapSummary; import java.util.HashMap;
import java.util.Set; /**
* @author WYH
* @version 2019年11月20日 上午11:02:16
*/
public class Map03 {
public static void main(String[] args) {
HashMap<Integer,String> hm = new HashMap<Integer,String>(); hm.put(1, "王友虎");
hm.put(2, "赵以浩");
hm.put(3, "齐博源");
hm.put(4, "李先锋");
hm.put(5, "李宏灿");
hm.put(6, "张国兴"); Set<Integer> set = hm.keySet(); for(Integer i : set) {
System.out.println(i+"---"+hm.get(i));
} } }
C: HashMap<String,Student>
package com.wyh.mapSummary; import java.util.HashMap;
import java.util.Set; import com.wyh.map.Student; /**
* @author WYH
* @version 2019年11月20日 上午11:13:55
*/
public class Map04 {
public static void main(String[] args) {
HashMap<String, Student> hm = new HashMap<String, Student>(); // 创建对象
Student s1 = new Student("王友虎", 22);
Student s2 = new Student("赵以浩", 24);
Student s3 = new Student("齐博源", 21);
Student s4 = new Student("李先锋", 22);
Student s5 = new Student("李宏灿", 22); // 添加到集合中去
hm.put("1", s1);
hm.put("2", s2);
hm.put("3", s3);
hm.put("4", s4);
hm.put("5", s5); Set<String> set = hm.keySet();
for(String s : set) {
System.out.println(s+"--"+hm.get(s).getName()+"--"+hm.get(s).getAge());
} } }
D: HashMap<Student,String> 容易忽略一点,为了保证键值相同,是一个学生类,而且是由HashMap存储, 就要重写,hashCode()和equals()方法
学生类:
package com.wyh.mapSummary; /**
* @author WYH
* @version 2019年11月19日 下午7:01:40
*/
public class Student {
private String name;
private int age;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
@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;
} }
测试类:
package com.wyh.map; import java.util.HashMap;
import java.util.Set; /**
* @author WYH
* @version 2019年11月19日 下午7:20:14
*
* <Student,String>
*
* 容易忽略一点,为了保证键值相同,是一个学生类,而且是由HashMap存储, 就要重写,hashCode()和equals()方法
*/
public class HashMapDemo2 {
public static void main(String[] args) {
//创建集合对象
HashMap<Student,String> hm = new HashMap<Student,String>(); //创建对象
Student s1 = new Student("王友虎",22);
Student s2 = new Student("赵以浩",24);
Student s3 = new Student("齐博源",21);
Student s4 = new Student("李先锋",22);
Student s5 = new Student("李宏灿",22);
Student s6 = new Student("王友虎",22); //添加到集合
hm.put(s1, "001");
hm.put(s2, "002");
hm.put(s3, "003");
hm.put(s4, "004");
hm.put(s5, "005");
hm.put(s6, "006"); Set<Student> set = hm.keySet(); for(Student key : set) {
String s = hm.get(key);
System.out.println(key.getName()+"---"+key.getAge()+"---"+s);
} } }
(6)TreeMap的练习
A: TreeMap<String,String>
package com.wyh.mapSummary; import java.util.Set;
import java.util.TreeMap; /**
* @author WYH
* @version 2019年11月20日 下午2:06:17
*/
public class TreeMapDemo {
public static void main(String[] args) {
TreeMap<String,String> tm = new TreeMap<String,String>(); tm.put("123", "张三");
tm.put("143", "李四");
tm.put("123", "张三");
tm.put("1123", "489");
tm.put("1423", "789");
tm.put("133", "999"); Set<String> s = tm.keySet(); for(String s1 : s) {
System.out.println(tm.get(s1)); } } }
B: TreeMap<Student,String> 无参构造方法默认调用的是自然排序,但是这里要自己重写方法
package com.wyh.map; import java.util.Comparator; import java.util.Set;
import java.util.TreeMap; /**
* @author WYH
* @version 2019年11月19日 下午7:54:29
* 无参构造方法默认调用的是自然排序,但是这里要自己重写方法
*
*
*/
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.getName().length() - s2.getName().length();
int num1 = num == 0 ? s1.getName().compareTo(s2.getName()) : num;
int num2 = num1 == 0 ? s1.getAge() - s2.getAge() : num1;
return num2;
} }); //创建对象
Student s1 = new Student("王友虎",22);
Student s2 = new Student("赵以浩",24);
Student s3 = new Student("齐博源",21);
Student s4 = new Student("李先锋",22);
Student s5 = new Student("李宏灿",22);
Student s6 = new Student("王友虎",22); 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 s : set) {
System.out.println(s.getName()+"---"+s.getAge()+"---"+tm.get(s));
} } }
大数据之路week02--day03 Map集合、Collections工具类的用法的更多相关文章
- Java集合——Collections工具类
Java集合——Collections工具类 摘要:本文主要学习了Collections工具类的常用方法. 概述 Collections工具类主要用来操作集合类,比如List和Set. 常用操作 排序 ...
- Java容器类Collection,List,Set,Map.,Iterator,Collections工具类,Arrays工具类,Comparable
Java容器类Collection,List,Set,Map.,Iterator,Collections工具类,Arrays工具类,Comparable接口,泛型 Collection,List,Se ...
- java 集合Collections 工具类:排序,查找替换。Set、List、Map 的of方法创建不可变集合
Collections 工具类 Java 提供1个操作 Set List Map 等集合的工具类 Collections ,该工具类里提供了大量方法对集合元素进行排序.查询和修改等操作,还提供了将集合 ...
- Java基础Map接口+Collections工具类
1.Map中我们主要讲两个接口 HashMap 与 LinkedHashMap (1)其中LinkedHashMap是有序的 怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...
- Java中的集合Collections工具类(六)
操作集合的工具类Collections Java提供了一个操作Set.List和Map等集合的工具类:Collections,该工具类里提供了大量方法对集合元素进行排序.查询和修改等操作,还提供了将集 ...
- 大数据之路week02 List集合的子类
1:List集合的子类(掌握) (1)List的子类特点 ArrayList: 底层数据结构是数组,查询快,增删慢. 线程不安全,效率高. Vector: 底层数据结构是数组,查询快,增删慢. 线程安 ...
- 大数据之路week01--自学之集合_1(Collection)
经过我个人的调查,发现,在今后的大数据道路上,集合.线程.网络编程变得尤为重要,为什么? 因为大数据大数据,我们必然要对数据进行处理,而这些数据往往是以集合形式存放,掌握对集合的操作非常重要. 在学习 ...
- 线程高级应用-心得8-java5线程并发库中同步集合Collections工具类的应用及案例分析
1. HashSet与HashMap的联系与区别? 区别:前者是单列后者是双列,就是hashmap有键有值,hashset只有键: 联系:HashSet的底层就是HashMap,可以参考HashSe ...
- Java 集合-Collections工具类
2017-11-05 23:41:53 Collections类 Collections类:Collections类是针对集合进行操作的工具类,都是静态方法. 常用方法: public static ...
随机推荐
- 【VS开发】fatal error C1001:编译器中发生内部错误
自己编译boost的库文件时遇到这个错误的,大概报错情况如下: mp_defer.hpp<50>:fatal error C1001:编译器中发生内部错误. 1> 要解决此问题, ...
- csu 1770: 按钮控制彩灯实验
1770: 按钮控制彩灯实验 Submit Page Summary Time Limit: 1 Sec Memory Limit: 128 Mb Submitted: 341 ...
- VBA来实现已存在的数据库,取得所有表的结构
问题描述 用VBA来取出MySQL数据库中的所有表的结构后生成一个Excel的文档 首先创建MySQL的数据源,如何创建数据源在前章已经写过,之后把下面的信息填写上即可 在window7 64位系统上 ...
- python 内置函数input/eval(22)
python的内置函数其实挺多的,其中input和eval算得上比较特殊,input属于交互式内置函数,eval函数能直接执行字符串表达式并返回表达式的值. 一.input函数 input是Pytho ...
- C++:盾神与条状项链
实现代码如下: #include<cstdlib> #include <string> #include <iostream> using namespace st ...
- Excel常用操作1
1.数据透视 所在选项卡:插入-数据透视表 例如:查看下表中各个工龄的平均工资 数据透视:选择所有数据--数据透视表--数据透视字段:选择工作经验和salary 切片器的使用,根据工作经验进行切片(还 ...
- Golang不会自动把slice转换成interface{}类型的slice
目录 例子 原因 如何去实现 例子 我们时常会写一些interface,例如: type A interface{ Print() } type B struct { } func (b *B) Pr ...
- Eureka 注册中心一直报Connect to localhost:8761 time out 的问题
忽略了配置eureka.client.service-url.defaultZone而导致的异常,重新覆盖配置就好 client: fetch-registry: false register-wit ...
- (十)springmvc之文件的处理
一.同步上传文件 导入common-fileupload这个jar包. 配置 springmvc-servlet.xml <?xml version="1.0" en ...
- java注解日志记录到数据库
1. pom添加依赖包 <!--添加aop依赖--><dependency> <groupId>org.springframework.boot</group ...