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的更多相关文章

  1. Java8 Map的遍历方式

    在这篇文章中,我将对Map的遍历方式做一个对比和总结,将分别从JAVA8之前和JAVA8做一个遍历方式的对比,亲测可行. public class LambdaMap { private Map< ...

  2. java代码之美(10)---Java8 Map中的computeIfAbsent方法

    Map中的computeIfAbsent方法 Map接口的实现类如HashMap,ConcurrentHashMap,HashTable等继承了此方法,通过此方法可以在特定需求下,让你的代码更加简洁. ...

  3. java代码(10) ---Java8 Map中的computeIfAbsent方法

    Map中的computeIfAbsent方法 一.案例说明 1.概述 在JAVA8的Map接口中,增加了一个computeIfAbsent,此方法签名如下: public V computeIfAbs ...

  4. Java8 Map computeIfAbsent方法说明

    // 方法定义 default V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) { . ...

  5. Java8 Map中新增的方法使用总结

    前言 得益于 Java 8 的 default 方法特性,Java 8 对 Map 增加了不少实用的默认方法,像 getOrDefault, forEach, replace, replaceAll, ...

  6. Java8 map和reduce

    map final List<Integer> numbers = Arrays.asList(1, 2, 3, 4); final List<Integer> doubleN ...

  7. java8 map flatmap

    map: 对于Stream中包含的元素使用给定的转换函数进行转换操作,新生成的Stream只包含转换生成的元素.这个方法有三个对于原始类型的变种方法,分别是:mapToInt,mapToLong和ma ...

  8. Java8 map value排序

    /** * Map value降序排序 * @param map * @param <K> * @param <V> * @return LinkedHashMap */ pu ...

  9. 2017年3月16工作日志【mysql更改字段参数、java8 map()调用方法示例】

    修改某个表的字段类型及指定为空或非空 >alter table 表名称 change 字段名称 字段名称 字段类型 [是否允许非空],变更字段名称及属性 >alter table 表名称 ...

  10. Java8中Map的遍历方式总结

    在这篇文章中,我将对Map的遍历方式做一个对比和总结,将分别从JAVA8之前和JAVA8做一个遍历方式的对比,亲测可行. public class LambdaMap { private Map< ...

随机推荐

  1. node 学习(一)

    什么是 Node.js Node.js就是运行在服务端的JavaScript. Node.js是一个基于Chrome JavaScript运行时建立的一个平台. Node.js是一个事件驱动 I/O ...

  2. Dapp开发教程一 Asch Dapp Hello World

    1 基本流程 Asch有三种net,localnet,testnet,mainnet,后两种是发布到线上的,可通过公网访问. 第一种localnet是运行在本地的.只有一个节点的私链,主要是为了方便本 ...

  3. linux 编译静态库及动态库例子--from周立功文档

    /* hello1.c */#include <stdio.h>int hello1 (void){printf("hello 1!\n");return 0;}/* ...

  4. java常用工具包

    1.国外 org.apache.commons.lang3 http://commons.apache.org/proper/commons-lang/javadocs/api-3.8.1/index ...

  5. django 缓存信号等

    参考别人的博客:写的挺好 http://www.cnblogs.com/wupeiqi/articles/5246483.html

  6. OpenDialog文件多选

    procedure TForm1.OpenFileListClick(Sender: TObject); var openDialog: TOpenDialog; I: Integer; begin ...

  7. [SpringMVC]自定义注解实现控制器访问次数限制

    我们需要根据IP去限制用户单位时间的访问次数,防止刷手机验证码,屏蔽注册机等,使用注解就非常灵活了 1 定义注解 @Retention(RetentionPolicy.RUNTIME) @Target ...

  8. 【VIM】-NO.140.VIM.1 -【VIM】

    Style:Mac Series:Java Since:2018-09-10 End:2018-09-10 Total Hours:1 Degree Of Diffculty:5 Degree Of ...

  9. ionic3 小记录

    cordova platform add ios@latest 安装最新ios ionic cordova build ios -- --buildFlag="-UseModernBuild ...

  10. Qt QSpinBox 和 QDoubleSpinBox

    展示一个效果: QDoubleSpinBox跟QSpinBox类似,只是多了一个decimal.