web123456

Java,Stream,filter,sorted,map usage

  • import .*;
  • import ;
  • class User {
  • private Integer age;
  • private String name;
  • public User(Integer age, String name) {
  • this.age = age;
  • this.name = name;
  • }
  • public String getName() {
  • return name;
  • }
  • public Integer getAge() {
  • return age;
  • }
  • @Override
  • public String toString() {
  • return "User{" +
  • "age=" + age +
  • ", name='" + name + '\'' +
  • '}';
  • }
  • }
  • public class Main {
  • public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
  • List<User> userList = new ArrayList<>();
  • (new User(3, "Tom"));
  • (new User(2, "Ana"));
  • (new User(99, "Bob"));
  • (new User(1, "Lyly"));
  • (new User(4, "Lyly"));
  • // Correct, object maps to names
  • // List<String> nameList = ().map(User::getName).collect(());//[Tom, Ana, Bob, Lyly, Lyly]
  • // Correct, object maps to names
  • List<String> nameList = ().map(user -> ()).collect(());//[Tom, Ana, Bob, Lyly, Lyly]
  • //mistake
  • List<String> nameList = ().forEach(user -> ()).collect(());
  • (nameList);
  • }
  • }