码迷,mamicode.com
首页 > 编程语言 > 详细

java List<对象> 转 Set、Map(高级)

时间:2018-07-25 20:02:57      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:div   span   code   list   nan   simple   strong   get   nbsp   

package com.demo.entity;


public class Book {

    private int id;

    private String name;

    public Book() {
    }

    public Book(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", name=‘" + name + ‘\‘‘ +
                ‘}‘;
    }
}

Main

package com.demo;

import com.demo.entity.Book;

import java.util.*;
import java.util.stream.Collectors;


public class Main {

    private static List<String> simpleList = new ArrayList<>();
    private static List<Book> normalList = new ArrayList<>();
    static {
        simpleList.add("apple");
        simpleList.add("apple");
        simpleList.add("banana");
        simpleList.add("orange");

        normalList.add(new Book(1,"Apple"));
        normalList.add(new Book(2,"Banana"));
        normalList.add(new Book(3,"Orange"));
        normalList.add(new Book(4,"Pig"));
    }
    public static void main(String[] args){
        System.out.println("----------------简单List---------------");
        simpleList.forEach(e -> System.out.println(e));
        System.out.println("----------------简单List转Set---------------");
        Set<String> simpleSet = new HashSet<>(simpleList);
        simpleSet.forEach(e -> System.out.println(e));

        System.out.println("----------------普通List---------------");
        normalList.forEach(e -> System.out.println(e));
        System.out.println("----------------普通List转Set---------------");
        Set<String> normalSet = normalList.stream().map(Book::getName).collect(Collectors.toSet());
        normalSet.forEach(e -> System.out.println(e));
        System.out.println("----------------普通List转List---------------");
        List<Integer> list = normalList.stream().map(Book::getId).collect(Collectors.toList());
        list.forEach(e -> System.out.println(e));
        System.out.println("----------------普通List转Map---------------");
        Map<Integer,Book> normalMap = normalList.stream().collect(Collectors.toMap(Book::getId,(b)->b));
        normalMap.forEach((id, book) -> {
            System.out.println(id + "::" + book);
        });
    }

}

 

运行结果

----------------简单List---------------
apple
apple
banana
orange
----------------简单List转Set---------------
banana
orange
apple
----------------普通List---------------
Book{id=1, name=‘Apple‘}
Book{id=2, name=‘Banana‘}
Book{id=3, name=‘Orange‘}
Book{id=4, name=‘Pig‘}
----------------普通List转Set---------------
Apple
Orange
Banana
Pig
----------------普通List转List---------------
1
2
3
4
----------------普通List转Map---------------
1::Book{id=1, name=‘Apple‘}
2::Book{id=2, name=‘Banana‘}
3::Book{id=3, name=‘Orange‘}
4::Book{id=4, name=‘Pig‘}

 

java List<对象> 转 Set、Map(高级)

标签:div   span   code   list   nan   simple   strong   get   nbsp   

原文地址:https://www.cnblogs.com/LUA123/p/9367657.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!