Java使用stream流,list转map

时间:2025-04-20 22:58:04
package ;

import ;
import ;
import ;
import ;

public class Demo {

    private String key;

    private String value;

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
         = key;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
         = value;
    }


    public static void main(String[] args) {
        List<Demo> list = new ArrayList<>();
        Demo demo = new Demo();
        ("key1");
        ("value1");
        (demo);
        Demo demo1 = new Demo();
        ("key2");
        ("value2");
        (demo1);
        Map<String, Demo> mapDemo = ().collect((Demo::getKey, a -> a));
        Map<String, String> map = ().collect((Demo::getKey, a -> ()));
    }
}