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. 计算机体系结构总结_Pipeline

    Textbook:<计算机组成与设计——硬件/软件接口>  HI<计算机体系结构——量化研究方法>          QR 在前面一节里我们有了一块简单的RISC CPU,包括 ...

  2. how to install protobuff python

    当前环境: operate system: Ubuntu 14.04.1 LTS protoc --version: libprotoc 2.5.0    protocol-buffers versi ...

  3. ab压测

    安装:yum install -y httpd-tools 验证:ab -V ab -help:-n requests 要执行请求总数,默认会执行一个请求 -c concurrency 一次执行多个请 ...

  4. Python RSA操作

    公钥加密.私钥解密 # -*- coding: utf-8 -*- import rsa # rsa加密 def rsaEncrypt(str): # 生成公钥.私钥 (pubkey, privkey ...

  5. 电子邮件协议:SMTP、POP3、IMAP4

    常见的电子邮件协议:SMTP.POP3.IMAP4   邮件发送协议:SMTP协议 邮件读取协议:POP3.IMAP4协议   SMTP协议(simple mail transfer protocol ...

  6. 仿scikit-learn模式写的kNN算法

    一.什么是kNN算法 k邻近是指每个样本都可以用它最接近的k个邻居来代表. 核心思想:如果一个样本在特征空间中的k个最相邻的样本中大多数属于一个某类别,则该样本也属于这个类别. 二.将kNN封装成kN ...

  7. Cannot find the declaration of element 'ehcache'.

    ehchahe.xml中报错: Cannot find the declaration of element 'ehcache'. 由于 <?xml version="1.0" ...

  8. postman实现Base64加密

    1.新建一个Collection 2.新建一个request 3.新增一个环境变量(全局变量也可以) 4.在variable中填入需要加密的变量名称,比如password 5.在body中填好参数,需 ...

  9. heroinfo_set.all 函数

    如果是 一对多 关系 即使用 heroinfo_set.all  此时关联字段类型通用,即上边的字段通用,但是需要添加many=True的参数heroinfo_set = serializers.Pr ...

  10. java 用RGB生成图片动态命名

    import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java. ...