Java8-Map
1、Staff实体
public class Staff {
private String name;
private int age;
private String address;
public Staff() {
}
public Staff(String name, int age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
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 String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Staff{" +
"name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
'}';
}
}
2、将字符串列表转换为大写
public class TextJava8Map1 {
public static void main(String[] args) {
List<String> alpha = Arrays.asList("a", "b", "c", "d");
// 在Java8之前
List<String> alphaUpper = new ArrayList<>();
for (String s :alpha) {
alphaUpper.add(s.toUpperCase());
}
System.out.println(alpha);
System.out.println(alphaUpper);
// Java8 map
List<String> collect1 = alpha.stream()
.map(String::toUpperCase)
.collect(Collectors.toList());
System.out.println(collect1);
}
}
3、获取List对象列表中某个属性的集合(Developer在博客-Java8比较器)
public class TextJava8Map2 {
public static void main(String[] args) {
List<Developer> developers = Arrays.asList(
new Developer("java", new BigDecimal(23231), 32),
new Developer("c++", new BigDecimal(32432), 30),
new Developer("spring", new BigDecimal(23121), 34)
);
// 在Java8之前
List<String> names1 = new ArrayList<>();
for (Developer developer : developers) {
names1.add(developer.getName());
}
System.out.println(names1);
// Java8 map
List<String> names2 = developers.stream()
.map(developer -> developer.getName())
.collect(Collectors.toList());
System.out.println(names2);
}
}
4、将一个List对象列表转换为另一个List对象列表
public class TextJava8Map3 {
public static void main(String[] args) {
List<Developer> developers = Arrays.asList(
new Developer("java", new BigDecimal(23231), 32),
new Developer("c++", new BigDecimal(32432), 30),
new Developer("spring", new BigDecimal(23121), 34)
);
// 在Java8之前
List<Staff> staffs = convertToStaff(developers);
System.out.println(staffs);
// Java8 map
List<Staff> result = developers.stream()
.map(developer -> {
Staff staff = new Staff();
staff.setName(developer.getName());
staff.setAge(developer.getAge());
if ("c++".equals(developer.getName())) {
staff.setAddress("us");
}
return staff;
})
.collect(Collectors.toList());
System.out.println(result);
}
private static List<Staff> convertToStaff(List<Developer> developers) {
List<Staff> staffs = new ArrayList<>();
for (Developer developer :developers) {
Staff staff = new Staff();
staff.setName(developer.getName());
staff.setAge(developer.getAge());
if ("java".equals(developer.getName())) {
staff.setAddress("us");
}
staffs.add(staff);
}
return staffs;
}
}
Java8-Map的更多相关文章
- Java8 Map的遍历方式
在这篇文章中,我将对Map的遍历方式做一个对比和总结,将分别从JAVA8之前和JAVA8做一个遍历方式的对比,亲测可行. public class LambdaMap { private Map< ...
- java代码之美(10)---Java8 Map中的computeIfAbsent方法
Map中的computeIfAbsent方法 Map接口的实现类如HashMap,ConcurrentHashMap,HashTable等继承了此方法,通过此方法可以在特定需求下,让你的代码更加简洁. ...
- java代码(10) ---Java8 Map中的computeIfAbsent方法
Map中的computeIfAbsent方法 一.案例说明 1.概述 在JAVA8的Map接口中,增加了一个computeIfAbsent,此方法签名如下: public V computeIfAbs ...
- Java8 Map computeIfAbsent方法说明
// 方法定义 default V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) { . ...
- Java8 Map中新增的方法使用总结
前言 得益于 Java 8 的 default 方法特性,Java 8 对 Map 增加了不少实用的默认方法,像 getOrDefault, forEach, replace, replaceAll, ...
- Java8 map和reduce
map final List<Integer> numbers = Arrays.asList(1, 2, 3, 4); final List<Integer> doubleN ...
- java8 map flatmap
map: 对于Stream中包含的元素使用给定的转换函数进行转换操作,新生成的Stream只包含转换生成的元素.这个方法有三个对于原始类型的变种方法,分别是:mapToInt,mapToLong和ma ...
- Java8 map value排序
/** * Map value降序排序 * @param map * @param <K> * @param <V> * @return LinkedHashMap */ pu ...
- 2017年3月16工作日志【mysql更改字段参数、java8 map()调用方法示例】
修改某个表的字段类型及指定为空或非空 >alter table 表名称 change 字段名称 字段名称 字段类型 [是否允许非空],变更字段名称及属性 >alter table 表名称 ...
- Java8中Map的遍历方式总结
在这篇文章中,我将对Map的遍历方式做一个对比和总结,将分别从JAVA8之前和JAVA8做一个遍历方式的对比,亲测可行. public class LambdaMap { private Map< ...
随机推荐
- 隐藏状态栏,toolbar前面空格问题,editText圆角
EditText圆角: 一.在drawable下面添加xml文件rounded_editview.xml 复制代码 代码如下: <?xml version="1.0" enc ...
- CentOS / RHEL 7 : How to setup yum repository using locally mounted DVD
1. Mount the RHEL 7 installation media ISO to some directory. For example /mnt : # mount -o loop rhe ...
- jquery $.ajax({});参数详解
用到过的: type:请求方式,默认 GET: url:请求路径: data:请求参数,类型是String:JSON.stringify({"name":tom,"age ...
- 聚焦AI实践,2019 A2M峰会将在上海举行!
18年,BERT重磅发布,刷新了很多NLP的任务的最好性能:KENSHO等智能化应用的成功应用,让知识图谱在证券行业的建设思路和应用实践成为业内较为关注的问题:强化学习也在与人类的对战游戏中独领风骚: ...
- 使用sessionStorage进行数据存值
<!DOCTYPE html> <head> <meta charset="UTF-8" /> <meta name="view ...
- Autohotkey常用命令
//输入密码#1::send test1234sleep 600send {enter}return //打开程序; win + t: open total cmd#t::IfWinNotExist ...
- Jmeter跨线程组传递参数
Jmeter的线程组之间是相互独立的,各个线程组互不影响,所以线程组A中输出的参数,是无法直接在线程组B中被调用的. 但有时候为了方便,可以把不同模块接口放在不同线程组,就涉及不同线程组传参问题,比如 ...
- Win10问题汇总
1.重置网络连接命令 netsh winsock reset ipconfig /flushdns 2.WIN10去除我的电脑上面的6个文件夹 把下面代码复制,保存到.reg中,然后执行即可(修改注册 ...
- JavaScript中的this所引用的对象和如何改变这个引用
this是函数内部的一个特殊对象,它引用的是函数执行环境对象.也就是运行是基于函数的执行环境绑定. 1.在网页全局作用域中调用函数时,this引用window var color='black'; f ...
- mysql8.0.13免安装版配置
一.下载 下载地址:https://dev.mysql.com/downloads/mysql/ 二.解压到某个目录,例如:D:/mysql/mysql-8.0.13-winx64 三.配置环境变量 ...