Skip to content

Java02. ДЗ 04, Гусев Андрей#5

Open
ASGusev wants to merge 3 commits intomasterfrom
streams
Open

Java02. ДЗ 04, Гусев Андрей#5
ASGusev wants to merge 3 commits intomasterfrom
streams

Conversation

@ASGusev
Copy link
Owner

@ASGusev ASGusev commented Nov 18, 2016

No description provided.

Copy link

@sproshev sproshev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

надо исправлять

build.gradle Outdated

apply plugin: 'java'

sourceCompatibility = 1.5

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1.8

build.gradle Outdated
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile group: 'com.google.guava', name: 'guava', version: '19.0'
//testCompile 'com.google.guava:guava:19.0'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

эта строка эквивалентна той, что над ней

// Список треков, отсортированный лексикографически по названию, включающий все треки альбомов из 'albums'
public static List<String> allTracksSorted(Stream<Album> albums) {
return albums
.flatMap((Album album) -> { return album.getTracks().stream(); })

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return и скобки не нужны, т.к. всего одно выражение в лямбде

тип для альбома можно не указывать

итого:

.flatMap(album -> album.getTracks().stream())

return album.getTracks().stream().anyMatch((Track track) ->
{ return track.getRating() > 95; });
}
})

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

анонимные классы с одним методов в 8 джаве? как же лямбды? замечание к предыдущему методу здесь тоже актуально

{ return track.getRating() > 95; });
}
})
.sorted((Album a, Album b) -> a.getName().compareTo(b.getName()))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.sorted(Comparator.comparing(Album::getName))

final String[] EXPECTED_ANSWER = { "foo", "foobar" };

for (int i = 1; i <= 3; i++) {
String curFileName = "testFindQuotes" + String.valueOf(i) + ".txt";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"testFindQuotes" + i + ".txt"


for (String file: filesList) {
Files.delete(Paths.get(file));
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

если бы было на списках, не пришлось бы с массивов на списки переключаться и обратно

final double EXPECTED = Math.PI / 4;
final double EPS = 1e-4;

Assert.assertEquals("The probability of hit is " + String.valueOf(EXPECTED) + ".",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"The probability of hit is " + EXPECTED + "."

HashMap<String,List<String>> compositions = new HashMap<>();
for (int i = 0; i < printers.length; i++) {
compositions.put(printers[i], Arrays.asList(works[i]));
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

можно заиспользовать класс из google-guava и написать примерно так:

        ImmutableMap
                .<String, List<String>>builder()
                .put("p1", Arrays.asList("a", "b"))
                .build();

"summary amount of all orders.", allProductsAmounts[i],
result.get(allProductNames[i]));
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

этот тест тоже можно сделать читаемее с помощью предыдущих замечаний

@ASGusev
Copy link
Owner Author

ASGusev commented Nov 26, 2016 via email

Copy link

@sproshev sproshev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

10

см в приложении countAlbumDuplicates, piDividedBy4, filterIsInstance

1
2

public static Map<String, Integer> calculateGlobalOrder(List<Map<String, Integer>> orders) {
return orders
.stream()
.flatMap(map -> StreamSupport.stream(map.entrySet().spliterator(), false))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.flatMap(map -> map.entrySet().stream())

"p1", Arrays.asList("story", "story"),
"p2", Arrays.asList("so", "me", "sho", "rt", "sto", "ri", "es"),
"p3", Arrays.asList("a very-very long story", "and one more"),
"p4", Arrays.asList("just tale")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Collections.singletonList(...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants