Advertisement
Guest User

Untitled

a guest
Apr 9th, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.96 KB | None | 0 0
  1. public class User {
  2.   private final int id;
  3.   private final String firstName;
  4.   private final String lastName;
  5.   private final Boolean active;
  6.  
  7.   // Для краткости я пропустил конструкторы, геттеры и сеттеры
  8. }
  9.  
  10. public static List<String> activeById(List<User> us) {
  11.    List<User> users = new ArrayList<User>();
  12.  
  13.    for (User u: us) {
  14.      if (u.getActive()) users.add(u);
  15.    }
  16.  
  17.    Collections.sort(users, new Comparator<User>() {
  18.       public int compare(User a, User b) {
  19.         return a.getId() - b.getId();
  20.       }
  21.    });
  22.  
  23.    List<String> finalUsers = new ArrayList<String>();
  24.  
  25.    for (User u: users) {
  26.      finalUsers.add(u.getLastname());
  27.    }
  28.  
  29.    return finalUsers;
  30. }
  31.  
  32. List<User> inputUsers = new ArrayList<User>();
  33. inputUsers.add(new User(11, "Nick", "Smith", false));
  34. inputUsers.add(new User(89, "Ken", "Pratt", true));
  35. inputUsers.add(new User(23, "Jack", "Sparrow", true));
  36.  
  37. List<User> activeUsersById = activeById(inputUsers)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement