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. python语法_元组

    tuple 元组 被称为只读列表 tup = (1,3,4,'234') 只能读,不能进行修改操作. 与列表的区分就是 () [] 中括号和小括号的区别,

  2. Python 学习笔记7 条件语句 If

    Python中条件语句if 是通过一条或者多条的执行语句的结果,来判断是否执行其包含的代码块. 通常会配合else.elif一起使用,达到根据条件进行多个代码块的执行操作. 简单的if score = ...

  3. init();

    <script language="javascript" type="text/javascript">window.onload = funct ...

  4. Kafka: Exactly-once Semantics

    https://www.confluent.io/blog/enabling-exactly-kafka-streams/ https://cwiki.apache.org/confluence/di ...

  5. 学习ActiveMQ(三):发布/订阅模式(topic)演示

    1.在这个项目中新增两个java类,主题生产者和主题消费者: 2.和点对点的代码差别并不大,所以将消费者和生产者的分别代码拷入新增的java类中,再修改就好了. appProducerTopic代码: ...

  6. 用sort方法输出数组

  7. TestNG Suite 运行出现中文乱码如何解决

    场景: 用TestNG框架运行测试类,控制台视图输出出现中文乱码. 解决方案: 1.eclipse属性>workspace>other>utf-8 2.修改eclipse.ini 文 ...

  8. thinkphp ckeditor与ckfinder

    thinkphp ckeditor与ckfinder 下载 ckeditor下载地址 ckfinder下载地址 整合 将ckeditor与findeditor下载完成后,放到public目录下,配置c ...

  9. .Net Core知识点

    1:const,readonly,和get访问器,三者都可在自己的生命域里赋值,但是编译器也是可以在构造函数里进行初始化赋值的 2:Debugger.IsAttached 属性 http://msdn ...

  10. Docker Machine批量安装docker host

    Dokcer Machine Docker Machine 可以批量安装和配置 docker host   提高docker的安装效率   同时减少人工安装操作的失误 [root@localhost ...