lambda - Java 8 Streams: Analyze same list elements -
code explains better:
test case
@org.junit.test public void test() { list<integer> values = arrays.aslist(1, 2, 3, 4, 5, 6, 7, 8, 9); system.out.println(withoutstreams(values)); // output: 1,9 system.out.println(withstreamsol1error(values)); // compile time error system.out.println(withstreamsol2(values)); // output: 1,9 }
required solution (without stream):
private list<integer> withoutstreams(list<integer> values) { // list<integer> values = arrays.aslist(1, 2, 3, 4, 5, 6, 7, 8, 9); (int index1 = 0; index1 < values.size(); index1++) { int value1 = values.get(index1); if (value1 == 10) return arrays.aslist(value1); (int index2 = index1 + 1; index2 < values.size(); index2++) { int value2 = values.get(index2); if ((value1 + value2) == 10) { return arrays.aslist(value1, value2); } } } return collections.emptylist(); }
my attempt : stream: (compile time error - mismatch)
private list<integer> withstreamsol1error(list<integer> values) { list<integer> exactequal = values.stream().filter(value -> value.intvalue() == 10).limit(1) .collect(collectors.tolist()); // compile time error if (exactequal.isempty()) { intstream.range(0, values.size() - 1) .foreachordered(index -> intstream.range(index + 1, values.size() - 1) .filter(index2 -> values.get(index) + values.get(index2) == 10).limit(1) .collect(collectors.tolist())); } return collections.emptylist(); }
with stream (working)
private list<integer> withstreamsol2(list<integer> values) { list<integer> exactequal = values.stream().filter(value -> value.intvalue() == 10).limit(1) .collect(collectors.tolist()); int index1 = 0; final list<integer> result = new arraylist<>(); if (exactequal.isempty()) { values.stream().foreach(value1 -> { if(!result.isempty()) // query: how can stop outer foreach return; result.add(value1); result.addall(values.stream().skip(index1).filter(value2 -> (value1 + value2) == 10).limit(1) .collect(collectors.tolist())); }); } return result; }
can suggest solution problem?
private static list<integer> withstreamsol1(list<integer> values) { list<integer> result = new arraylist<>(); values.stream() .filter(i -> result.isempty() && == 10) .foreach(i -> result.add(i)); intstream.range(0, values.size()) .filter(i -> result.isempty()) .foreach(i -> intstream.range(i + 1, values.size()) .filter(j -> result.isempty() && values.get(i) + values.get(j) == 10) .foreach(j -> result.addall(arrays.aslist(values.get(i), values.get(j))))); return result; }
or
private static stream<int[]> streamofoneortwoelements(list<integer> list) { return stream.concat( list.stream() .map(i -> new int[] {i}), intstream.range(0, list.size()) .boxed() .flatmap(i -> intstream.range(i + 1, list.size()) .maptoobj(j -> new int[] {list.get(i), list.get(j)}))); } private static list<integer> withstreamsol3(list<integer> values) { optional<int[]> array = streamofoneortwoelements(values) .filter(a -> intstream.of(a).sum() == 10) .findfirst(); return array.ispresent() ? intstream.of(array.get()).boxed().collect(collectors.tolist()) : collections.emptylist(); }
Comments
Post a Comment