标签: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‘}
标签:div span code list nan simple strong get nbsp
原文地址:https://www.cnblogs.com/LUA123/p/9367657.html