Changed code style according to new Java versions

Changed long and ugly type declarations with "var" keyword (Java 10).  Also fixed warning about raw usage of HashSet without type specification.
This commit is contained in:
Alexander Danilchenko
2020-01-12 18:34:55 +02:00
committed by GitHub
parent d8da439590
commit 52a5a7cc24

View File

@@ -1,10 +1,9 @@
import java.util.*; import java.util.*;
public class SetCovering { public class SetCovering {
public static void main(String... args) {
public static void main(String[] args) { var statesNeeded = new HashSet<>(Arrays.asList("mt", "wa", "or", "id", "nv", "ut", "ca", "az"));
Set<String> statesNeeded = new HashSet(Arrays.asList("mt", "wa", "or", "id", "nv", "ut", "ca", "az")); var stations = new LinkedHashMap<String, Set<String>>();
Map<String, Set<String>> stations = new LinkedHashMap<>();
stations.put("kone", new HashSet<>(Arrays.asList("id", "nv", "ut"))); stations.put("kone", new HashSet<>(Arrays.asList("id", "nv", "ut")));
stations.put("ktwo", new HashSet<>(Arrays.asList("wa", "id", "mt"))); stations.put("ktwo", new HashSet<>(Arrays.asList("wa", "id", "mt")));
@@ -12,13 +11,13 @@ public class SetCovering {
stations.put("kfour", new HashSet<>(Arrays.asList("nv", "ut"))); stations.put("kfour", new HashSet<>(Arrays.asList("nv", "ut")));
stations.put("kfive", new HashSet<>(Arrays.asList("ca", "az"))); stations.put("kfive", new HashSet<>(Arrays.asList("ca", "az")));
Set<String> finalStations = new HashSet<String>(); var finalStations = new HashSet<String>();
while (!statesNeeded.isEmpty()) { while (!statesNeeded.isEmpty()) {
String bestStation = null; String bestStation = null;
Set<String> statesCovered = new HashSet<>(); var statesCovered = new HashSet<String>();
for (Map.Entry<String, Set<String>> station : stations.entrySet()) { for (var station : stations.entrySet()) {
Set<String> covered = new HashSet<>(statesNeeded); var covered = new HashSet<>(statesNeeded);
covered.retainAll(station.getValue()); covered.retainAll(station.getValue());
if (covered.size() > statesCovered.size()) { if (covered.size() > statesCovered.size()) {