java-8

Combine keys and values of a HashMap to a Set

牧云@^-^@ 提交于 2021-02-16 14:19:27
问题 I have a HashMap<Integer, Integer> , There can be duplicate values for the unique keys. Is there a way to convert the HashMap to a Set<Integer> which contains unique Integers of both the keys and values. This can definitely be done in two loops by iterating over the keySet() and .values(). I'm would like to know whether this is possible in java 8 streams. 回答1: You can use the stream function to combine both the values and the keys: Map<Integer, Integer> map = ... Set<Integer> total = Stream

Java: Consumer interface in a stream doesn't work as expected [duplicate]

牧云@^-^@ 提交于 2021-02-16 14:13:31
问题 This question already has answers here : Java 8 Streams peek api (4 answers) How to use Streams api peek() function and make it work? (2 answers) Closed 2 years ago . I've got 2 statements, I expected that they should "print" same result: Arrays.stream("abc".split("")).forEach(System.out::println);//first Arrays.stream("abc".split("")).peek(new Consumer<String>() {//second @Override public void accept(String s) { System.out.println(s);//breakpoint } }); In fact, the first statement will print

Java: Consumer interface in a stream doesn't work as expected [duplicate]

荒凉一梦 提交于 2021-02-16 14:11:51
问题 This question already has answers here : Java 8 Streams peek api (4 answers) How to use Streams api peek() function and make it work? (2 answers) Closed 2 years ago . I've got 2 statements, I expected that they should "print" same result: Arrays.stream("abc".split("")).forEach(System.out::println);//first Arrays.stream("abc".split("")).peek(new Consumer<String>() {//second @Override public void accept(String s) { System.out.println(s);//breakpoint } }); In fact, the first statement will print

Following changes in JDK8 repository

我的梦境 提交于 2021-02-16 14:09:18
问题 I'd like to follow the development of JDK8, but what I see in the repo is strange at best: 6 days ago katleman Added tag jdk8-b60 for changeset e07f499b9dcc default tip changeset | manifest 7 days ago katleman Merge jdk8-b60 changeset | manifest Most changes look like "Added tag jdk8-b60 for changeset XXX" or "Merge XXX", there are a few entries looking like actual code changes, but no link to leads to code. Actually no single click brought me to anything marginally useful. I know nothing

Does the JLS require inlining of final String constants?

别等时光非礼了梦想. 提交于 2021-02-16 09:57:11
问题 I ran into an issue while manipulating some bytecode, where a certain final String constant was not inlined by the java compiler (Java 8), see the example below: public class MyTest { private static final String ENABLED = "Y"; private static final String DISABLED = "N"; private static boolean isEnabled(String key) { return key.equals("A"); } private static String getString(String key, String value) { return key + value; } public static void main(String[] args) throws Exception { String flag =

Java 8 Stream groupingBy with custom logic

帅比萌擦擦* 提交于 2021-02-16 04:42:04
问题 I have a list of Records . Which has two fields: LocalDateTime instant and a Double data . I want to groupBy all the records by Hour and create a Map<Integer, Double> . Where the keys (Integer) are hours and values(Double) are last Data of that hour - first Data of that hour. What I have done so far is following: Function<Record, Integer> keyFunc = rec->rec.getInstant().getHour(); Map<Integer, List<Record>> valueMap = records.stream().collect(Collectors.groupingBy(keyFunc)); I want the value

Why does Optional.map make this assignment work?

时间秒杀一切 提交于 2021-02-15 12:07:29
问题 Optional<ArrayList<String>> option = Optional.of(new ArrayList<>()); Optional<ArrayList<?>> doesntWork = option; Optional<ArrayList<?>> works = option.map(list -> list); The first attempted assignment does not compile, but the second one with the map does. It feels like the map shouldn't actually accomplish anything, but for some reason it turns my Optional<ArrayList<String>> into an Optional<ArrayList<?>> . Is there some sort of implicit cast going on? 回答1: If you look into the code of map

Why does Optional.map make this assignment work?

僤鯓⒐⒋嵵緔 提交于 2021-02-15 12:06:46
问题 Optional<ArrayList<String>> option = Optional.of(new ArrayList<>()); Optional<ArrayList<?>> doesntWork = option; Optional<ArrayList<?>> works = option.map(list -> list); The first attempted assignment does not compile, but the second one with the map does. It feels like the map shouldn't actually accomplish anything, but for some reason it turns my Optional<ArrayList<String>> into an Optional<ArrayList<?>> . Is there some sort of implicit cast going on? 回答1: If you look into the code of map

Why does Optional.map make this assignment work?

亡梦爱人 提交于 2021-02-15 12:06:16
问题 Optional<ArrayList<String>> option = Optional.of(new ArrayList<>()); Optional<ArrayList<?>> doesntWork = option; Optional<ArrayList<?>> works = option.map(list -> list); The first attempted assignment does not compile, but the second one with the map does. It feels like the map shouldn't actually accomplish anything, but for some reason it turns my Optional<ArrayList<String>> into an Optional<ArrayList<?>> . Is there some sort of implicit cast going on? 回答1: If you look into the code of map

Comparable VS <? extends Comparable>

北城以北 提交于 2021-02-13 12:15:07
问题 regarding the following code: public class Test <T extends Comparable>{ public static void main(String[] args){ List<String> lst = Array.asList("abc","def"); System.out.println(func(lst)); } public static boolean func(List<**here**> lst){ return lst.get(0).compareTo(lst.get(1)) == 0; } } why writing " ? extends Comparable" here would compile , and writing "Comparable" would not compile? thanks in advance. 回答1: This happens because generics are invariant . Even if String is a Comparable ,