1.问题:java8 list转Map

报错Collectors.toMap :: results in "Non-static method cannot be refernced from static context"

解决:将第二个参数传入function

原因:Collectors.toMap参数接收为function

2.解决key重复问题:

        Map<String,String> map2=list.stream().collect(Collectors.toMap(Person::getName,o->"",(key1,key2)->key2));

输出:{cc=, bb=, aa22=, aa11=}

3.value为对象本身,注意map泛型为Person

Person

public  class Person {
private String id;
private String name; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public Person(String name) {
this.name = name;
} public Person(String id, String name) {
this.id = id;
this.name = name;
} @Override
public String toString() {
return "Person{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
'}';
}

map

List<Person> list=new ArrayList<>();

list.add(new Person("aa","aa11"));
list.add(new Person("bb",""));
list.add(new Person("aa","aa22"));
list.add(new Person("cc","")); Map<String,Person> map2=list.stream().collect(Collectors.toMap(o->o.getId(),o->o,(key1,key2)->key2));//重复时取后面的
//Map<String,Person> map2=list.stream().collect(Collectors.toMap(o->o.getId(), Function.identity(),(key1, key2)->key2));//优雅写法
System.out.println(map2);

输出:{cc=Person{id='cc', name=''}, bb=Person{id='bb', name=''}, aa=Person{id='aa', name='aa22'}}

4.排序:

        Map<String,String> map2=list.stream().collect(Collectors.toMap(Person::getId,Person::getName ,(key1, key2)->key2,TreeMap<String,String>::new));

输出{aa=aa22, bb=, cc=}

其它:java8--List转为Map、分组、过滤、求和等操作

java8 list转Map报错Collectors.toMap :: results in "Non-static method cannot be refernced from static context"的更多相关文章

  1. ISE MAP报错: Unsupported programming for BSCAN block and JTAG_CHAIN attribute value 1的解决方法

    2014-04-16 17:35:30 ISE MAP报错: Unsupported programming for BSCAN block and JTAG_CHAIN attribute valu ...

  2. Xcode报错Expected selector for Objective-C and Expected method body

    昨天把键盘拿起来拍一下清清灰,然后就发现Xcode报错了,Xcode报错Expected selector for Objective-C and Expected method body,也不知道什 ...

  3. ceph集群jewel版本 rbd 块map 报错-故障排查

    测试信息如下: [root@ceph_1 ~]# ceph osd pool lsrbdchy_123swimmingpool #新建rbd 块: rbd create swimmingpool/ba ...

  4. vue项目中使用echarts map报错Cannot read property 'push' of undefined nanhai.js

    在vue中绘制地图需要加载一个本地china.json文件,我用的是get请求的方法加载的,而不是直接import,因为我怕import请求到的部署到线上的时候会有问题.如下是get请求方法: thi ...

  5. SSH项目练习的时候报错:[applicationContext.xml]: Invocation of init method failed;

    这里是控制台的报错信息:org.springframework.beans.factory.BeanCreationException: Error creating bean with name ' ...

  6. Spring Scheduled定时任务报错 java.lang.IllegalStateException: Encountered invalid @Scheduled method 'xxx': For input string: "2S"

    报错信息如下: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ding ...

  7. laravel Tinker报错 BadMethodCallException with message 'Call to undefined method Illuminate\Database\Query\Builder

    进行模型关联操作, php artisan tinker 执行 $user = App\Models\User::find(1) $user->followings()->attach([ ...

  8. Jmeter BeanShell 引用变量报错jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval Parse error at line 14, column 181 : Error or number too big for integer

    如果你通过CSV Data Set Config或者_StringFromFile函数来参数化你的请求,需要特别注意当参数为纯数字时,jmeter会默认将其识别成int型数据,说明jmeter并不是默 ...

  9. 关于Flutter启动项目白屏,报错[ERROR:flutter/shell/gpu/gpu_surface_gl.cc(58)] Failed to setup Skia Gr context.问题的解决方案

    首先,环境如下: 1.系统:windows10 64位   Android SDK version: 28.0.3   Flutter SDK: v1.5.4-hotfix.2   模拟器: 网易Mu ...

随机推荐

  1. windows套接字阻塞模式编程实例

    一.阻塞模式套接字服务端和客户端的运行流程如下: 1.1 服务器运行过程如下: 1.服务器启动后,等待客户端的连接请求.2.当收到客户端的请求后,在界面上显示该客户端的IP地址和端口,以及“Hello ...

  2. [七月挑选]使用idea创建spring boot 项目

    title: 使用idea创建spring boot 项目 参考lindaZ的IntelliJ IDEA 创建spring boot 的Hello World 项目 1.Open IDEA,choos ...

  3. 伟大的GIL

    GIL 首先需要明确的一点是GIL并不是Python的特性,它是在实现Python解析器(CPython)时所引入的一个概念.就好比C++是一套语言(语法)标准,但是可以用不同的编译器来编译成可执行代 ...

  4. Java 学习输入1234 求和

    输入任何整数都能求和 import java.util.Scanner; /** * @author 作者anil E-mail:888@yirose.com * @date 创建时间:2017年3月 ...

  5. 【前端】JavaScript基础

    1 什么是js JavaScript是一种运行在浏览器中的解释型的编程语言 1.1 js引用 使用<script></script>标签 <script src=&quo ...

  6. 【NOIP2016提高A组8.12】礼物

    题目 夏川的生日就要到了.作为夏川形式上的男朋友,季堂打算给夏川买一些生日礼物. 商店里一共有种礼物.夏川每得到一种礼物,就会获得相应喜悦值Wi(每种礼物的喜悦值不能重复获得). 每次,店员会按照一定 ...

  7. layui数据表格分页加载动画,自己定义加载动画,"加载中..."

    记录思路,仅供参考 在表格渲染完成后,在done回调函数中给分页动态加点击事件, 关闭"加载中..."动画也是在 done回调函数中关闭 这是我实现的思路,记录给大家参考. , d ...

  8. margin属性以及垂直外边距重叠问题

       盒子的margin属性         盒子的外边距margin 指的是当前盒子与其他盒子之间的距离,环绕在盒子周围的空白区域,属于不可见的区域,,不会影响到可见框的大小,而是会影响到盒子的位置 ...

  9. ExoPlayer + 边缓存边播放

    在此基础上改动:https://www.cnblogs.com/candyzhmm/p/9957928.html private void openPlayer(String videoUrl) { ...

  10. ios input readonly失效(点击的时候会有光标出现)/禁止输入法弹出问题

    苹果端用1,2,之后解决不了readonly失效问题(点击的时候会有光标出现)(且不方便用disabled的时候),就用3, 1,    $("#appDateTime").foc ...