-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Java updates #1
Open
pfarner
wants to merge
4
commits into
evmorov:source
Choose a base branch
from
pfarner:JavaUpdates
base: source
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Java updates #1
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
cbb872e
Eliminating redundant type specification
oxpresto 3cf8253
Use try-with-resources in preference to unprotected close() calls
oxpresto 848161c
Used Java streams when that was a reasonable match and other code exa…
oxpresto f0ef15c
sirex points out that I missed a filter. Added.
oxpresto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
import java.util.*; | ||
import java.util.Arrays; | ||
|
||
class ArrayIterate { | ||
public static void main(String[] args) { | ||
for (int num : Arrays.asList(1, 2)) { | ||
System.out.println(num); | ||
} | ||
Arrays.asList(1, 2).stream().sequential().forEach(System.out::println); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,10 @@ | ||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
class ArrayMap { | ||
public static void main(String[] args) { | ||
List<Integer> arr = Arrays.asList(1, 5, 10, 20); | ||
List<Integer> mapped = new ArrayList<Integer>(); | ||
for (int n : arr) | ||
mapped.add(n * 2); | ||
List<Integer> arr = Arrays.asList(1, 5, 10, 20); | ||
List<Integer> mapped = arr.stream().map(n -> n * 2).collect(Collectors.toList()); | ||
System.out.println(mapped); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,8 @@ | ||
import java.util.*; | ||
import java.util.stream.IntStream; | ||
|
||
class ArrayReduce { | ||
public static void main(String[] args) { | ||
int sum = 0; | ||
for (int n : Arrays.asList(1, 2, 3)) | ||
sum += n; | ||
int sum = IntStream.of(1, 2, 3).sum(); | ||
System.out.println(sum); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,10 @@ | ||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
class ArraySelect { | ||
public static void main(String[] args) { | ||
List<Integer> arr = Arrays.asList(1, 5, 10, 20); | ||
List<Integer> all = new ArrayList<Integer>(); | ||
for (int n : arr) | ||
if (n % 2 == 0) | ||
all.add(n); | ||
List<Integer> all = arr.stream().filter(n -> n % 2 == 0).collect(Collectors.toList()); | ||
System.out.println(all); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import java.util.stream.IntStream; | ||
|
||
class LoopFor { | ||
public static void main(String[] args) { | ||
for (int i = 1; i <= 3; i++) | ||
System.out.println(i + ". Hi"); | ||
IntStream.range(1, 3+1).sequential().mapToObj(i -> i + ". Hi").forEach(System.out::println); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import java.util.stream.IntStream; | ||
|
||
class LoopNext { | ||
public static void main(String[] args) { | ||
for (int i = 0; i < 3; i++) { | ||
if (i == 1) | ||
continue; | ||
System.out.println((i + 1) + ". Hi"); | ||
} | ||
IntStream.range(0, 3) | ||
.sequential() | ||
.filter(i -> i != 1) | ||
.mapToObj(i -> (i + 1) + ". Hi") | ||
.forEach(System.out::println); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import java.util.stream.IntStream; | ||
|
||
class LoopTimes { | ||
public static void main(String[] args) { | ||
for (int i = 0; i < 3; i++) | ||
System.out.println("Hi"); | ||
IntStream.range(0, 3).sequential().mapToObj(i -> "Hi").forEach(System.out::println); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
import java.util.stream.IntStream; | ||
|
||
class LoopUntil { | ||
public static void main(String[] args) { | ||
int i = 0; | ||
while (i != 3) | ||
i++; | ||
int i = IntStream.iterate(0, n -> n + 1).filter(n -> n == 3).findFirst().getAsInt(); | ||
System.out.println(i); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,16 @@ | ||
import java.io.*; | ||
import java.util.stream.Collectors; | ||
|
||
class OtherRunCommand { | ||
public static void main(String[] args) throws IOException, InterruptedException { | ||
String result = ""; | ||
final String result; | ||
ProcessBuilder ps = new ProcessBuilder("java", "-version"); | ||
ps.redirectErrorStream(true); | ||
Process pr = ps.start(); | ||
BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream())); | ||
String line; | ||
while ((line = in.readLine()) != null) | ||
result += line + "\n"; | ||
pr.waitFor(); | ||
in.close(); | ||
try (BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream()))) { | ||
result = in.lines().collect(Collectors.joining("\n")); | ||
pr.waitFor(); | ||
} | ||
System.out.println(result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not just count, but filtered count. You should count only those values greater than 10. In Python this looks something like this:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I missed that. Some unit tests might've caught it.