Skip to main content
Code Review

Return to Question

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

Follow up to Duplicate like a weapon, arrays like heaven Duplicate like a weapon, arrays like heaven.

@rolfl suggested in chat that I try to complete this challenge again in a Java 8 friendly way. I took the opportunity to also employ the algorithm suggested in this brilliant answer answer.

I'm not familiar with Java 8 features, but am definitely interested in using it more and finding the best ways to implement it.

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
public class ArrayAbsurdityJava8 {
 public static void main(String[] args) throws IOException {
 Files.lines(new File(args[0]).toPath())
 .filter(s -> !s.isEmpty())
 .forEach(ArrayAbsurdityJava8::printDuplicate);
 }
 private static void printDuplicate(String line) {
 int length = Integer.parseInt(line.split(";")[0]);
 int total = Arrays.stream(line.split(";")[1].split(","))
 .mapToInt(Integer::parseInt)
 .sum();
 System.out.println(total - (length - 1) * (length - 2) / 2);
 }
}

This ended up shorter than I expected. Is this best? I was thinking of doing it my way as well, but the boolean comparison requires a for loop (am I wrong?).

Follow up to Duplicate like a weapon, arrays like heaven.

@rolfl suggested in chat that I try to complete this challenge again in a Java 8 friendly way. I took the opportunity to also employ the algorithm suggested in this brilliant answer.

I'm not familiar with Java 8 features, but am definitely interested in using it more and finding the best ways to implement it.

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
public class ArrayAbsurdityJava8 {
 public static void main(String[] args) throws IOException {
 Files.lines(new File(args[0]).toPath())
 .filter(s -> !s.isEmpty())
 .forEach(ArrayAbsurdityJava8::printDuplicate);
 }
 private static void printDuplicate(String line) {
 int length = Integer.parseInt(line.split(";")[0]);
 int total = Arrays.stream(line.split(";")[1].split(","))
 .mapToInt(Integer::parseInt)
 .sum();
 System.out.println(total - (length - 1) * (length - 2) / 2);
 }
}

This ended up shorter than I expected. Is this best? I was thinking of doing it my way as well, but the boolean comparison requires a for loop (am I wrong?).

Follow up to Duplicate like a weapon, arrays like heaven.

@rolfl suggested in chat that I try to complete this challenge again in a Java 8 friendly way. I took the opportunity to also employ the algorithm suggested in this brilliant answer.

I'm not familiar with Java 8 features, but am definitely interested in using it more and finding the best ways to implement it.

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
public class ArrayAbsurdityJava8 {
 public static void main(String[] args) throws IOException {
 Files.lines(new File(args[0]).toPath())
 .filter(s -> !s.isEmpty())
 .forEach(ArrayAbsurdityJava8::printDuplicate);
 }
 private static void printDuplicate(String line) {
 int length = Integer.parseInt(line.split(";")[0]);
 int total = Arrays.stream(line.split(";")[1].split(","))
 .mapToInt(Integer::parseInt)
 .sum();
 System.out.println(total - (length - 1) * (length - 2) / 2);
 }
}

This ended up shorter than I expected. Is this best? I was thinking of doing it my way as well, but the boolean comparison requires a for loop (am I wrong?).

added 4 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Sending duplicate to heaven with javaJava 8

Follow up to Duplicate like a weapon, arrays like heaven.

@rolfl suggested in chat that I try to complete this challenge again in a javaJava 8 friendly way. I took the opportunity to also employ the algorithm suggested in this brilliant answer.

I'm not familiar with javaJava 8 features, but am definitely interested in using it more and finding the best ways to implement it.

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
public class ArrayAbsurdityJava8 {
 public static void main(String[] args) throws IOException {
 Files.lines(new File(args[0]).toPath())
 .filter(s -> !s.isEmpty())
 .forEach(ArrayAbsurdityJava8::printDuplicate);
 }
 private static void printDuplicate(String line) {
 int length = Integer.parseInt(line.split(";")[0]);
 int total = Arrays.stream(line.split(";")[1].split(","))
 .mapToInt(Integer::parseInt)
 .sum();
 System.out.println(total - (length - 1) * (length - 2) / 2);
 }
}

This ended up shorter than I expected. Is this best? I was thinking of doing it my way as well, but the booleanboolean comparison requires a forfor loop (am I wrong?).

Sending duplicate to heaven with java 8

Follow up to Duplicate like a weapon, arrays like heaven.

@rolfl suggested in chat that I try to complete this challenge again in a java 8 friendly way. I took the opportunity to also employ the algorithm suggested in this brilliant answer.

I'm not familiar with java 8 features, but am definitely interested in using it more and finding the best ways to implement it.

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
public class ArrayAbsurdityJava8 {
 public static void main(String[] args) throws IOException {
 Files.lines(new File(args[0]).toPath())
 .filter(s -> !s.isEmpty())
 .forEach(ArrayAbsurdityJava8::printDuplicate);
 }
 private static void printDuplicate(String line) {
 int length = Integer.parseInt(line.split(";")[0]);
 int total = Arrays.stream(line.split(";")[1].split(","))
 .mapToInt(Integer::parseInt)
 .sum();
 System.out.println(total - (length - 1) * (length - 2) / 2);
 }
}

This ended up shorter than I expected. Is this best? I was thinking of doing it my way as well, but the boolean comparison requires a for loop (am I wrong?).

Sending duplicate to heaven with Java 8

Follow up to Duplicate like a weapon, arrays like heaven.

@rolfl suggested in chat that I try to complete this challenge again in a Java 8 friendly way. I took the opportunity to also employ the algorithm suggested in this brilliant answer.

I'm not familiar with Java 8 features, but am definitely interested in using it more and finding the best ways to implement it.

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
public class ArrayAbsurdityJava8 {
 public static void main(String[] args) throws IOException {
 Files.lines(new File(args[0]).toPath())
 .filter(s -> !s.isEmpty())
 .forEach(ArrayAbsurdityJava8::printDuplicate);
 }
 private static void printDuplicate(String line) {
 int length = Integer.parseInt(line.split(";")[0]);
 int total = Arrays.stream(line.split(";")[1].split(","))
 .mapToInt(Integer::parseInt)
 .sum();
 System.out.println(total - (length - 1) * (length - 2) / 2);
 }
}

This ended up shorter than I expected. Is this best? I was thinking of doing it my way as well, but the boolean comparison requires a for loop (am I wrong?).

Tweeted twitter.com/#!/StackCodeReview/status/565030200686428160
Source Link
Legato
  • 9.9k
  • 4
  • 50
  • 118

Sending duplicate to heaven with java 8

Follow up to Duplicate like a weapon, arrays like heaven.

@rolfl suggested in chat that I try to complete this challenge again in a java 8 friendly way. I took the opportunity to also employ the algorithm suggested in this brilliant answer.

I'm not familiar with java 8 features, but am definitely interested in using it more and finding the best ways to implement it.

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
public class ArrayAbsurdityJava8 {
 public static void main(String[] args) throws IOException {
 Files.lines(new File(args[0]).toPath())
 .filter(s -> !s.isEmpty())
 .forEach(ArrayAbsurdityJava8::printDuplicate);
 }
 private static void printDuplicate(String line) {
 int length = Integer.parseInt(line.split(";")[0]);
 int total = Arrays.stream(line.split(";")[1].split(","))
 .mapToInt(Integer::parseInt)
 .sum();
 System.out.println(total - (length - 1) * (length - 2) / 2);
 }
}

This ended up shorter than I expected. Is this best? I was thinking of doing it my way as well, but the boolean comparison requires a for loop (am I wrong?).

lang-java

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