Skip to main content
Code Review

Return to Question

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

While reviewing Multiplying Lists, I got the impression that the problem was naturally suited to Java 8 streams. Unfortunately, since there is no convenient way to zip two streams no convenient way to zip two streams, the solution ended up being uglier than I expected.

While reviewing Multiplying Lists, I got the impression that the problem was naturally suited to Java 8 streams. Unfortunately, since there is no convenient way to zip two streams, the solution ended up being uglier than I expected.

While reviewing Multiplying Lists, I got the impression that the problem was naturally suited to Java 8 streams. Unfortunately, since there is no convenient way to zip two streams, the solution ended up being uglier than I expected.

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

While reviewing Multiplying Lists Multiplying Lists, I got the impression that the problem was naturally suited to Java 8 streams. Unfortunately, since there is no convenient way to zip two streams, the solution ended up being uglier than I expected.

While reviewing Multiplying Lists, I got the impression that the problem was naturally suited to Java 8 streams. Unfortunately, since there is no convenient way to zip two streams, the solution ended up being uglier than I expected.

While reviewing Multiplying Lists, I got the impression that the problem was naturally suited to Java 8 streams. Unfortunately, since there is no convenient way to zip two streams, the solution ended up being uglier than I expected.

Tweeted twitter.com/#!/StackCodeReview/status/558948743102599168
Renamed as suggested by @vnp
Source Link
200_success
  • 145.6k
  • 22
  • 190
  • 479

The task is to take lines of input, in the form a0 a1 a2 ... an | b0 b1 b2 ... bn, where the ai and bi are integers, and output the dot productproducts a0b0 a1b1 a2b2 ... anbn.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.StreamSupport;
public class StreamMultiplier {
 public static IntStream dotProductmultiply(IntStream aStream, IntStream bStream) {
 PrimitiveIterator.OfInt a = aStream.iterator(),
 b = bStream.iterator();
 PrimitiveIterator.OfInt product = new PrimitiveIterator.OfInt() {
 public boolean hasNext() { return a.hasNext() && b.hasNext(); }
 public int nextInt() { return a.next() * b.next(); }
 };
 return StreamSupport.intStream(
 Spliterators.spliteratorUnknownSize(product, Spliterator.ORDERED),
 false
 );
 }
 public static IntStream toIntStream(String spaceDelimitedInts) {
 return Arrays.stream(spaceDelimitedInts.trim().split("\\s+"))
 .mapToInt(Integer::parseInt);
 }
 public static void main(String[] args) throws FileNotFoundException {
 Scanner input = (args.length > 0) ? new Scanner(new File(args[0]))
 : new Scanner(System.in);
 while (input.hasNextLine()) {
 String[] halves = input.nextLine().split("\\|", 2);
 System.out.println(
 dotProductmultiply(toIntStream(halves[0]), toIntStream(halves[1]))
 .mapToObj(String::valueOf)
 .collect(Collectors.joining(" "))
 );
 }
 }
}

The task is to take lines of input, in the form a0 a1 a2 ... an | b0 b1 b2 ... bn, where the ai and bi are integers, and output the dot product a0b0 a1b1 a2b2 ... anbn.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.StreamSupport;
public class StreamMultiplier {
 public static IntStream dotProduct(IntStream aStream, IntStream bStream) {
 PrimitiveIterator.OfInt a = aStream.iterator(),
 b = bStream.iterator();
 PrimitiveIterator.OfInt product = new PrimitiveIterator.OfInt() {
 public boolean hasNext() { return a.hasNext() && b.hasNext(); }
 public int nextInt() { return a.next() * b.next(); }
 };
 return StreamSupport.intStream(
 Spliterators.spliteratorUnknownSize(product, Spliterator.ORDERED),
 false
 );
 }
 public static IntStream toIntStream(String spaceDelimitedInts) {
 return Arrays.stream(spaceDelimitedInts.trim().split("\\s+"))
 .mapToInt(Integer::parseInt);
 }
 public static void main(String[] args) throws FileNotFoundException {
 Scanner input = (args.length > 0) ? new Scanner(new File(args[0]))
 : new Scanner(System.in);
 while (input.hasNextLine()) {
 String[] halves = input.nextLine().split("\\|", 2);
 System.out.println(
 dotProduct(toIntStream(halves[0]), toIntStream(halves[1]))
 .mapToObj(String::valueOf)
 .collect(Collectors.joining(" "))
 );
 }
 }
}

The task is to take lines of input, in the form a0 a1 a2 ... an | b0 b1 b2 ... bn, where the ai and bi are integers, and output the products a0b0 a1b1 a2b2 ... anbn.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.StreamSupport;
public class StreamMultiplier {
 public static IntStream multiply(IntStream aStream, IntStream bStream) {
 PrimitiveIterator.OfInt a = aStream.iterator(),
 b = bStream.iterator();
 PrimitiveIterator.OfInt product = new PrimitiveIterator.OfInt() {
 public boolean hasNext() { return a.hasNext() && b.hasNext(); }
 public int nextInt() { return a.next() * b.next(); }
 };
 return StreamSupport.intStream(
 Spliterators.spliteratorUnknownSize(product, Spliterator.ORDERED),
 false
 );
 }
 public static IntStream toIntStream(String spaceDelimitedInts) {
 return Arrays.stream(spaceDelimitedInts.trim().split("\\s+"))
 .mapToInt(Integer::parseInt);
 }
 public static void main(String[] args) throws FileNotFoundException {
 Scanner input = (args.length > 0) ? new Scanner(new File(args[0]))
 : new Scanner(System.in);
 while (input.hasNextLine()) {
 String[] halves = input.nextLine().split("\\|", 2);
 System.out.println(
 multiply(toIntStream(halves[0]), toIntStream(halves[1]))
 .mapToObj(String::valueOf)
 .collect(Collectors.joining(" "))
 );
 }
 }
}
Source Link
200_success
  • 145.6k
  • 22
  • 190
  • 479
Loading
lang-java

AltStyle によって変換されたページ (->オリジナル) /