Skip to content

Commit

Permalink
Changed HashMap and HashSet to LinkedHashMap and LinkedHashSet respec…
Browse files Browse the repository at this point in the history
…tively for a more stable result (#1892)

First step towards fixing #1854
  • Loading branch information
tmelman authored Nov 14, 2023
1 parent a9194bd commit 5721517
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions src/main/java/picard/fingerprint/CrosscheckFingerprints.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -645,8 +644,8 @@ private void remapFingerprints(final Map<FingerprintIdDetails, Fingerprint> fpMa
});

if (CollectionUtil.makeSet(resultingSamples.toArray(new String[0])).size() != resultingSamples.size()) {
final Set<String> duplicates = new HashSet<>();
final Set<String> unique = new HashSet<>();
final Set<String> duplicates = new LinkedHashSet<>();
final Set<String> unique = new LinkedHashSet<>();
resultingSamples.forEach(s -> {
if (unique.add(s)) {
duplicates.add(s);
Expand All @@ -658,7 +657,7 @@ private void remapFingerprints(final Map<FingerprintIdDetails, Fingerprint> fpMa
}

// replace samples with their mapped values:
final Set<FingerprintIdDetails> ids = new HashSet<>(fpMap.keySet());
final Set<FingerprintIdDetails> ids = new LinkedHashSet<>(fpMap.keySet());
ids.forEach(id -> {
// if sample isn't in sampleMap, leave it alone
if (!sampleMap.containsKey(id.sample)) {
Expand Down Expand Up @@ -699,7 +698,7 @@ private void remapFingerprintsFromFiles(final Map<FingerprintIdDetails, Fingerpr

final Map<String, List<FingerprintIdDetails>> fileFpDetailSetMap = fpMap.keySet().stream().collect(Collectors.groupingBy(s -> s.file));

final Map<String, String> fileSampleMap = new HashMap<>();
final Map<String, String> fileSampleMap = new LinkedHashMap<>();
// check that each file in the map points only to one sample
fileFpDetailSetMap.forEach((key, fingerprintIdDetails) -> {
final Set<String> samples = fingerprintIdDetails.stream().map(id -> id.sample).collect(Collectors.toSet());
Expand All @@ -721,8 +720,8 @@ private void remapFingerprintsFromFiles(final Map<FingerprintIdDetails, Fingerpr
});

if (CollectionUtil.makeSet(resultingSamples.toArray(new String[0])).size() != resultingSamples.size()) {
final Set<String> duplicates = new HashSet<>();
final Set<String> unique = new HashSet<>();
final Set<String> duplicates = new LinkedHashSet<>();
final Set<String> unique = new LinkedHashSet<>();
resultingSamples.forEach(s -> {
if (unique.add(s)) {
duplicates.add(s);
Expand All @@ -734,7 +733,7 @@ private void remapFingerprintsFromFiles(final Map<FingerprintIdDetails, Fingerpr
}

// replace samples with their mapped values:
final Set<FingerprintIdDetails> ids = new HashSet<>(fpMap.keySet());
final Set<FingerprintIdDetails> ids = new LinkedHashSet<>(fpMap.keySet());
ids.forEach(id -> {
// if sample isn't in sampleMap, leave it alone
if (!sampleMap.containsKey(id.file)) {
Expand Down Expand Up @@ -772,7 +771,7 @@ private Map<String, String> getStringStringMap(final File sampleMapFile, final S

private Map<Path, Path> getSamplePathToIndexMap(final File indexMapFile, final String inputArgumentName) {
final Map<String, String> indexStringMap = getStringStringMap(indexMapFile, inputArgumentName);
final HashMap<Path, Path> indexPathMap = new LinkedHashMap<Path, Path>();
final LinkedHashMap<Path, Path> indexPathMap = new LinkedHashMap<Path, Path>();
for (Map.Entry<String, String> entry: indexStringMap.entrySet()) {
final Path inputPath;
final Path indexPath;
Expand Down Expand Up @@ -931,7 +930,7 @@ private int checkFingerprintsBySample(final Map<FingerprintIdDetails, Fingerprin
final Map<String, FingerprintIdDetails> sampleToDetail1 = fingerprints1BySample.keySet().stream().collect(Collectors.toMap(id -> id.group, id -> id));
final Map<String, FingerprintIdDetails> sampleToDetail2 = fingerprints2BySample.keySet().stream().collect(Collectors.toMap(id -> id.group, id -> id));

Set<String> samples = new HashSet<>();
Set<String> samples = new LinkedHashSet<>();
samples.addAll(sampleToDetail1.keySet());
samples.addAll(sampleToDetail2.keySet());

Expand Down

0 comments on commit 5721517

Please sign in to comment.