Advertisement
Guest User

Untitled

a guest
Jul 31st, 2014
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. Stream<Pair> pairStream = Streams.iterate(0, (i) -> i + 1).map( // natural numbers
  2. new Function<Integer, Pair>() {
  3. Integer previous;
  4.  
  5. @Override
  6. public Pair apply(Integer integer) {
  7. Pair pair = null;
  8. if (previous != null) pair = new Pair(previous, integer);
  9. previous = integer;
  10. return pair;
  11. }
  12. }).substream(1); // drop first null
  13.  
  14. pairStream.limit(1_000_000).forEach(i -> System.out.println(i));
  15.  
  16. IntStream.range(1, arrayList.size())
  17. .mapToObj(i -> new Pair(arrayList.get(i-1), arrayList.get(i)))
  18. .forEach(System.out::println);
  19.  
  20. public class ConsecutiveSpliterator<T> implements Spliterator<List<T>> {
  21.  
  22. private final Spliterator<T> wrappedSpliterator;
  23.  
  24. private final int n;
  25.  
  26. private final Deque<T> deque;
  27.  
  28. private final Consumer<T> dequeConsumer;
  29.  
  30. public ConsecutiveSpliterator(Spliterator<T> wrappedSpliterator, int n) {
  31. this.wrappedSpliterator = wrappedSpliterator;
  32. this.n = n;
  33. this.deque = new LinkedList<>();
  34. this.dequeConsumer = new Consumer<T>() {
  35. @Override
  36. public void accept(T t) {
  37. deque.addLast(t);
  38. }
  39. };
  40. }
  41.  
  42. @Override
  43. public boolean tryAdvance(Consumer<? super List<T>> action) {
  44. deque.pollFirst();
  45. fillDeque();
  46. if (deque.size() == n) {
  47. List<T> list = new ArrayList<>(deque);
  48. action.accept(list);
  49. return true;
  50. } else {
  51. return false;
  52. }
  53. }
  54.  
  55. private void fillDeque() {
  56. while (deque.size() < n && wrappedSpliterator.tryAdvance(dequeConsumer))
  57. ;
  58. }
  59.  
  60. @Override
  61. public Spliterator<List<T>> trySplit() {
  62. return null;
  63. }
  64.  
  65. @Override
  66. public long estimateSize() {
  67. return wrappedSpliterator.estimateSize();
  68. }
  69.  
  70. @Override
  71. public int characteristics() {
  72. return wrappedSpliterator.characteristics();
  73. }
  74. }
  75.  
  76. public <E> Stream<List<E>> consecutiveStream(Stream<E> stream, int n) {
  77. Spliterator<E> spliterator = stream.spliterator();
  78. Spliterator<List<E>> wrapper = new ConsecutiveSpliterator<>(spliterator, n);
  79. return StreamSupport.stream(wrapper, false);
  80. }
  81.  
  82. consecutiveStream(Stream.of(0, 1, 2, 3, 4, 5), 2).map(
  83. new Function<List<Integer>, Pair>() {
  84. public Pair apply(List<Integer> list) {
  85. return new Pair(list.get(0), list.get(1));
  86. }
  87. }).forEach(System.out::println);
  88.  
  89. import java.util.function.IntFunction;
  90. import java.util.stream.IntStream;
  91.  
  92. public class PairFunction implements IntFunction<PairFunction.Pair> {
  93.  
  94. public static class Pair {
  95.  
  96. private final int first;
  97. private final int second;
  98.  
  99. public Pair(int first, int second) {
  100. this.first = first;
  101. this.second = second;
  102. }
  103.  
  104. @Override
  105. public String toString() {
  106. return "[" + first + "|" + second + "]";
  107. }
  108. }
  109.  
  110. private int last;
  111. private boolean first = true;
  112.  
  113. @Override
  114. public Pair apply(int value) {
  115. Pair pair = !first ? new Pair(last, value) : null;
  116. last = value;
  117. first = false;
  118. return pair;
  119. }
  120.  
  121. public static void main(String[] args) {
  122.  
  123. IntStream intStream = IntStream.of(0, 1, 2, 3, 4);
  124. final PairFunction pairFunction = new PairFunction();
  125. intStream.mapToObj(pairFunction)
  126. .filter(p -> p != null) // filter out the null
  127. .forEach(System.out::println); // display each Pair
  128.  
  129. }
  130.  
  131. }
  132.  
  133. List<Integer> input = Arrays.asList(0, 1, 2, 3, 4);
  134. Stream<Pair> pairStream = Streams.zip(input.stream(),
  135. input.stream().substream(1),
  136. (a, b) -> new Pair(a, b)
  137. );
  138.  
  139. public static void main(String[] args) {
  140. Stream<Integer> numbers = IntStream.rangeClosed(0, 4).boxed();
  141. AtomicInteger counter = new AtomicInteger(Integer.MIN_VALUE);
  142. List<Pair> collect = numbers.map(n -> {
  143. int i = counter.getAndSet(n);
  144. return i == Integer.MIN_VALUE ? null : new Pair(i, n);
  145. })
  146. .filter(p -> p != null)
  147. .collect(toList());
  148. System.out.println(collect);
  149. }
  150.  
  151. class Pair {
  152. private int left, right;
  153. Pair(int left, int right) { this.left = left; this.right = right; }
  154. @Override public String toString() { return "{" + left + "," + right + '}'; }
  155. }
  156.  
  157. for(int i = 0 ; i < stream.length-1 ; i++)
  158. {
  159. Pair pair = new Pair(stream[i], stream[i+1]);
  160. // then add your pair to an array
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement