Created
October 31, 2021 08:28
-
-
Save alexradzin/f44db0b51ccba166ff820a57d378d92f to your computer and use it in GitHub Desktop.
Variables initialization
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
import java.util.AbstractMap; | |
import java.util.Arrays; | |
import java.util.Collections; | |
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; | |
import java.util.TreeMap; | |
import java.util.TreeSet; | |
import java.util.stream.Collectors; | |
import java.util.stream.Stream; | |
class Initialization { | |
private void primitives() { | |
int i = 0; | |
short s = 1; | |
byte b = 1; | |
long l = 0; | |
long time = 1635529440858L; | |
double d = 3.1415926; | |
float f = 3.1415926F; | |
boolean yes = true; | |
boolean now = false; | |
String str = "hello"; | |
} | |
private void arrays() { | |
int[] i = new int[] {1, 2, 3}; | |
String[] strings = {"hello", "world"}; | |
} | |
private void listsFromJava5ToJava8() { | |
List<String> empty1 = Collections.emptyList(); | |
List<String> single = Collections.singletonList("hello"); | |
List<String> multiple = Arrays.asList("hello", "world"); | |
} | |
private void setsFromJava5ToJava8() { | |
Set<String> empty1 = Collections.emptySet(); | |
Set<String> single = Collections.singleton("hello"); | |
Set<String> multiple = new HashSet<String>(Arrays.asList("hello", "world")); | |
} | |
private void mapsFromJava5ToJava8() { | |
Map<String, Integer> empty = Collections.emptyMap(); | |
Map<String, Integer> single = Collections.singletonMap("one", 1); | |
Map<String, Integer> multiple1 = new HashMap<>(); | |
multiple1.put("one", 1); | |
multiple1.put("two", 2); | |
Map<String, Integer> multiple2 = new HashMap<>() {{ | |
put("one", 1); | |
put("two", 2); | |
}}; | |
} | |
private void mapsInJava8() { | |
Map<String, Integer> multiple3 = Stream.of( | |
new AbstractMap.SimpleEntry<>("one", 1), | |
new AbstractMap.SimpleEntry<>("two", 2)) | |
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); | |
} | |
private void listsFromJava9() { | |
List<String> empty1 = List.of(); | |
List<String> single = List.of("hello"); | |
List<String> multiple = List.of("hello", "world"); | |
} | |
private void setsFromJava9() { | |
Set<String> empty1 = Set.of(); | |
Set<String> single = Set.of("hello"); | |
Set<String> multiple = Set.of("hello", "world"); | |
Set<String> caseInsensitiveSet = Stream.of("hello", "world") | |
.collect(Collectors.toCollection(() -> new TreeSet<>(String.CASE_INSENSITIVE_ORDER))); | |
Set<String> linkedSet = Stream.of("hello", "world") | |
.collect(Collectors.toCollection(LinkedHashSet::new)); | |
} | |
private void mapsFromJava9() { | |
Map<String, Integer> m0 = Map.of(); | |
Map<String, Integer> m1 = Map.of("one", 1); | |
Map<String, Integer> m2 = Map.of("one", 1, "two", 2); | |
Map<String, Integer> m3 = Map.of("one", 1, "two", 2, "three", 3); | |
// ........ | |
Map<String, Integer> m10 = Map.of( | |
"one", 1, "two", 2, "three", 3, "four", 4, "five", 5, | |
"six", 6, "seven", 7, "eight", 8, "nine", 9, "ten", 10); | |
// how to create map that contains more entries? | |
Map<String, Integer> mN = Map.ofEntries(Map.entry("one", 1), Map.entry("two", 2)); | |
Map<String, Integer> linkedMap = Stream.of(Map.entry("one", 1), Map.entry("two", 2)) | |
.collect(Collectors.toMap( | |
Map.Entry::getKey, | |
Map.Entry::getValue, | |
(one, two) -> two, | |
LinkedHashMap::new)); | |
Map<String, Integer> caseInsensitiveMap = Stream.of(Map.entry("one", 1), Map.entry("two", 2)) | |
.collect(Collectors.toMap( | |
Map.Entry::getKey, | |
Map.Entry::getValue, | |
(one, two) -> two, | |
() -> new TreeMap<>(String.CASE_INSENSITIVE_ORDER))); | |
} | |
} |
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
import java.util.AbstractMap.SimpleEntry; | |
import java.util.LinkedHashMap; | |
import java.util.LinkedHashSet; | |
import java.util.Map; | |
import java.util.Set; | |
import java.util.TreeMap; | |
import java.util.TreeSet; | |
import java.util.stream.Stream; | |
import static java.lang.String.CASE_INSENSITIVE_ORDER; | |
import static java.util.Map.Entry; | |
import static java.util.Map.entry; | |
import static java.util.stream.Collectors.toCollection; | |
import static java.util.stream.Collectors.toMap; | |
class InitializationWithStaticImports { | |
private void mapsInJava8() { | |
Map<String, Integer> multiple3 = Stream.of( | |
new SimpleEntry<>("one", 1), | |
new SimpleEntry<>("two", 2)) | |
.collect(toMap(Entry::getKey, Entry::getValue)); | |
} | |
private void setsFromJava9() { | |
Set<String> caseInsensitiveSet = Stream.of("hello", "world") | |
.collect(toCollection(() -> new TreeSet<>(CASE_INSENSITIVE_ORDER))); | |
Set<String> linkedSet = Stream.of("hello", "world") | |
.collect(toCollection(LinkedHashSet::new)); | |
} | |
private void mapsFromJava9() { | |
// how to create map that contains more entries? | |
Map<String, Integer> mN = Map.ofEntries(entry("one", 1), entry("two", 2)); | |
Map<String, Integer> linkedMap = Stream.of(entry("one", 1), entry("two", 2)) | |
.collect(toMap(Entry::getKey, Entry::getValue, (x, y) -> y, LinkedHashMap::new)); | |
Map<String, Integer> caseInsensitiveMap = Stream.of(entry("one", 1), entry("two", 2)) | |
.collect(toMap(Entry::getKey, Entry::getValue, (x, y) -> y, () -> new TreeMap<>(CASE_INSENSITIVE_ORDER))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment