Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. @Override
  2. public Iterator<T> iterator() {
  3. return new IteratorImpl(t -> t!=null && ((Automobile)t).getPrice() == 20.);
  4. }
  5.  
  6.  
  7. private class IteratorImpl implements Iterator<T> {
  8.  
  9. boolean condition = false;
  10. Predicate<T> p;
  11. int cursor;
  12.  
  13.  
  14. IteratorImpl() {
  15. p = t -> true;
  16. }
  17.  
  18. IteratorImpl(Predicate<T> pred) {
  19. p = pred;
  20. }
  21.  
  22. @Override
  23. public boolean hasNext() {
  24. while (cursor != size) {
  25. if(p.test(automobileList[cursor])) {
  26. return true;
  27. }
  28. cursor++;
  29. }
  30. return false;
  31. }
  32.  
  33. @Override
  34. public T next() {
  35. T auto;
  36. while (hasNext()) {
  37. if(p.test(auto = automobileList[cursor++])) {
  38. condition = true;
  39. return auto;
  40. }
  41. }
  42. throw new NoSuchElementException();
  43. }
  44.  
  45. @Override
  46. public void remove() {
  47. if (!condition) {
  48. throw new IllegalStateException();
  49. }
  50. condition = false;
  51. if (cursor != size) {
  52. System.arraycopy(automobileList, cursor, automobileList, cursor - 1, size - cursor);
  53. }
  54. cursor--;
  55. size--;
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement