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< ...
随机推荐
- 变量类型-Dict
教程:一:字典的创建 1:字典的介绍------>d = {key1:value1, key2:values2} (1)dictionary(字典) 是 Python 中最有用的数 ...
- spring-boot+mybatisPlus+shiro的集成demo 我用了5天
spring-boot + mybatis-plus + shiro 的集成demo我用了五天 关于shiro框架,我还是从飞机哪里听来的,就连小贱都知道,可我母鸡啊.简单百度了下,结论很好上手,比s ...
- makefile编写
概述 什么是makefile?或许非常多Winodws的程序猿都不知道这个东西,由于那些Windows的IDE都为你做了这个工作,但我觉得要作一个好的和professional的程序猿,makefil ...
- python中的基础2
2 2.1 字符串的索引与切片: a = 'ABCDEFGHIJK' print(a[0]) print(a[3]) print(a[5]) print(a[7]) 2.2 字符串的常用方法. pr ...
- 如何关闭git pull产生的merge 信息
编辑 ~/.gitconfig [core] mergeoptions = --no-edit 或者终端之行 git config --global core.mergeoptions --no-ed ...
- windows redis 自动启动
start.bat D: D:\dev\redis- redis-server.exe redis.conf redis.vbs createobject( 之后,把 start.bat 放在redi ...
- 关于Java8:StreamAPI的一点记录
关于 Stream ,Functional Interface 的一点记录 stream对于集合操作的便捷度提升: import java.util.ArrayList; import java.ut ...
- PHP 二维数组根据某个字段按指定排序方式排序
/** * 二维数组根据某个字段按指定排序方式排序 * @param $arr array 二维数组 * @param $field string 指定字段 * @param int $sort_or ...
- LeetCode 653 Two Sum IV - Input is a BST 解题报告
题目要求 Given a Binary Search Tree and a target number, return true if there exist two elements in the ...
- js考察this,作用域链和闭包
在严格版中的默认的this不再是window,而是undefined. 先看两个例子 example one var num = 20; var obj = { num: 30, fn: (funct ...