Advertisement
Guest User

Untitled

a guest
Apr 20th, 2014
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. this.stops.stream().filter(Stop s-> s.getStation().getName().equals(name));
  2.  
  3. public class Train {
  4.  
  5. private final String name;
  6. private final SortedSet<Stop> stops;
  7.  
  8. public Train(String name) {
  9. this.name = name;
  10. this.stops = new TreeSet<Stop>();
  11. }
  12.  
  13. public void addStop(Stop stop) {
  14. this.stops.add(stop);
  15. }
  16.  
  17. public Stop getFirstStation() {
  18. return this.getStops().first();
  19. }
  20.  
  21. public Stop getLastStation() {
  22. return this.getStops().last();
  23. }
  24.  
  25. public SortedSet<Stop> getStops() {
  26. return stops;
  27. }
  28.  
  29. public SortedSet<Stop> getStopsAfter(String name) {
  30.  
  31.  
  32. // return this.stops.subSet(, toElement);
  33. return null;
  34. }
  35. }
  36.  
  37.  
  38. import java.util.ArrayList;
  39. import java.util.List;
  40.  
  41. public class Station {
  42. private final String name;
  43. private final List<Stop> stops;
  44.  
  45. public Station(String name) {
  46. this.name = name;
  47. this.stops = new ArrayList<Stop>();
  48.  
  49. }
  50.  
  51. public String getName() {
  52. return name;
  53. }
  54.  
  55. }
  56.  
  57. yourStream
  58. .filter(/* your criteria */)
  59. .findFirst()
  60. .get();
  61.  
  62. public static void main(String[] args) {
  63. class Stop {
  64. private final String stationName;
  65. private final int passengerCount;
  66.  
  67. Stop(final String stationName, final int passengerCount) {
  68. this.stationName = stationName;
  69. this.passengerCount = passengerCount;
  70. }
  71. }
  72.  
  73. List<Stop> stops = new LinkedList<>();
  74.  
  75. stops.add(new Stop("Station1", 250));
  76. stops.add(new Stop("Station2", 275));
  77. stops.add(new Stop("Station3", 390));
  78. stops.add(new Stop("Station2", 210));
  79. stops.add(new Stop("Station1", 190));
  80.  
  81. Stop firstStopAtStation1 = stops.stream()
  82. .filter(e -> e.stationName.equals("Station1"))
  83. .findFirst()
  84. .get();
  85.  
  86. System.out.printf("At the first stop at Station1 there were %d passengers in the train.", firstStopAtStation1.passengerCount);
  87. }
  88.  
  89. At the first stop at Station1 there were 250 passengers in the train.
  90.  
  91. this.stops.stream().filter(Stop s-> s.getStation().getName().equals(name));
  92.  
  93. this.stops.stream().filter((Stop s)-> s.getStation().getName().equals(name));
  94.  
  95. this.stops.stream().filter(s -> s.getStation().getName().equals(name));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement