groupingBy() does NOT preserve order by default (but we used to assume that it does)

This commit is contained in:
Reinhard Pointner 2017-02-12 20:08:54 +08:00
parent b28e81ca1e
commit 8a9a6c62bb
5 changed files with 9 additions and 5 deletions

View File

@ -16,6 +16,7 @@ import java.awt.event.ActionEvent;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Collection; import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask; import java.util.concurrent.FutureTask;
@ -57,7 +58,7 @@ public class UserFiles {
public static void revealFiles(Collection<File> files) { public static void revealFiles(Collection<File> files) {
if (isMacApp()) { if (isMacApp()) {
files.stream().collect(groupingBy(File::getParentFile)).forEach((parent, children) -> { files.stream().collect(groupingBy(File::getParentFile, LinkedHashMap::new, toList())).forEach((parent, children) -> {
try { try {
FileManager.revealInFinder(children.get(children.size() - 1)); FileManager.revealInFinder(children.get(children.size() - 1));
} catch (Exception e) { } catch (Exception e) {

View File

@ -1127,7 +1127,7 @@ public class MediaDetection {
} }
} }
return emptyList(); return emptyList();
})).forEach((group, videos) -> groups.add(videos)); }, LinkedHashMap::new, toList())).forEach((group, videos) -> groups.add(videos));
}); });
}); });

View File

@ -19,6 +19,7 @@ import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
@ -195,7 +196,7 @@ class MovieMatcher implements AutoCompleteMatcher {
} }
// map movies to (possibly multiple) files (in natural order) // map movies to (possibly multiple) files (in natural order)
Map<Movie, Set<File>> filesByMovie = movieByFile.entrySet().stream().collect(groupingBy(Entry::getValue, mapping(Entry::getKey, toCollection(TreeSet::new)))); Map<Movie, Set<File>> filesByMovie = movieByFile.entrySet().stream().collect(groupingBy(Entry::getValue, LinkedHashMap::new, mapping(Entry::getKey, toCollection(TreeSet::new))));
// collect all File/MoviePart matches // collect all File/MoviePart matches
List<Match<File, ?>> matches = new ArrayList<Match<File, ?>>(); List<Match<File, ?>> matches = new ArrayList<Match<File, ?>>();

View File

@ -1,6 +1,7 @@
package net.filebot.ui.subtitle.upload; package net.filebot.ui.subtitle.upload;
import static java.util.Collections.*; import static java.util.Collections.*;
import static java.util.stream.Collectors.*;
import static net.filebot.Logging.*; import static net.filebot.Logging.*;
import static net.filebot.media.MediaDetection.*; import static net.filebot.media.MediaDetection.*;
import static net.filebot.util.FileUtilities.*; import static net.filebot.util.FileUtilities.*;
@ -13,6 +14,7 @@ import java.awt.event.ActionEvent;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
@ -127,7 +129,7 @@ public class SubtitleUploadDialog extends JDialog {
} }
private List<SubtitleGroup> getUploadGroups(SubtitleMapping[] table) { private List<SubtitleGroup> getUploadGroups(SubtitleMapping[] table) {
return StreamEx.ofValues(StreamEx.of(table).groupingBy(SubtitleMapping::getGroup)).flatMap(this::groupRunsByCD).toList(); return StreamEx.ofValues(StreamEx.of(table).groupingBy(SubtitleMapping::getGroup, LinkedHashMap::new, toList())).flatMap(this::groupRunsByCD).toList();
} }
private Stream<SubtitleGroup> groupRunsByCD(Collection<SubtitleMapping> group) { private Stream<SubtitleGroup> groupRunsByCD(Collection<SubtitleMapping> group) {

View File

@ -328,7 +328,7 @@ public class TMDbClient implements MovieIdentificationService, ArtworkProvider {
return streamJsonObjects(titles, "titles").collect(groupingBy(it -> { return streamJsonObjects(titles, "titles").collect(groupingBy(it -> {
return getString(it, "iso_3166_1"); return getString(it, "iso_3166_1");
}, mapping(it -> { }, LinkedHashMap::new, mapping(it -> {
return getString(it, "title"); return getString(it, "title");
}, toList()))); }, toList())));
} }