Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.function.Function;
  4. import java.util.function.Predicate;
  5.  
  6. public class ListCreator<T> {
  7.  
  8. private List<T> list;
  9. private List<T> listTmp;
  10.  
  11. public ListCreator(List<T> list) {
  12. this.list = list;
  13. }
  14.  
  15. public static <T> ListCreator<T> collectFrom(List<T> destinations) {
  16. ListCreator<T> lc = new ListCreator<T>(destinations);
  17. return lc;
  18. }
  19.  
  20. public ListCreator<T> when(Predicate<T> p) {
  21. listTmp = new ArrayList<T>();
  22.  
  23. for(int i = 0; i < list.size(); i++) {
  24. if(p.test(list.get(i))) {
  25. listTmp.add(list.get(i));
  26. }
  27. }
  28. this.list = listTmp;
  29. return this;
  30. }
  31.  
  32. public <R> List<T> mapEvery(Function<T,R> u) {
  33. listTmp = new ArrayList<T>();
  34.  
  35. for (T e : list) {
  36. listTmp.add( (T) u.apply(e));
  37. }
  38.  
  39. this.list = listTmp;
  40. return list;
  41. }
  42.  
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement