java-8

Java 8: How to convert List<String> to Map<String,List<String>>?

爷,独闯天下 提交于 2020-08-22 03:35:40
问题 I have a List of String like: List<String> locations = Arrays.asList("US:5423","US:6321","CA:1326","AU:5631"); And I want to convert in Map<String, List<String>> as like: AU = [5631] CA = [1326] US = [5423, 6321] I have tried this code and it works but in this case, I have to create a new class GeoLocation.java . List<String> locations=Arrays.asList("US:5423", "US:6321", "CA:1326", "AU:5631"); Map<String, List<String>> locationMap = locations .stream() .map(s -> new GeoLocation(s.split(":")[0

Iterating List of List in java8 [duplicate]

微笑、不失礼 提交于 2020-08-20 05:00:09
问题 This question already has answers here : How can I turn a List of Lists into a List in Java 8? (9 answers) Closed 5 years ago . I was wondering, How can we iterate a multilevel list using stream API in Java 8 For example, List<List<String>> multiList = new ArrayList<>(); List<String> names= Arrays.asList("a","b"); List<String> fewMoreNames= Arrays.asList("e","f"); multiList.add(names); multiList.add(fewMoreNames); As per Java 8, I should go something like below multiList.stream().... ? I

Java List<String> to Map<String, Integer> convertion

自闭症网瘾萝莉.ら 提交于 2020-08-19 06:45:56
问题 I'd like to convert a Map <String, Integer> from List<String> in java 8 something like this: Map<String, Integer> namesMap = names.stream().collect(Collectors.toMap(name -> name, 0)); because I have a list of Strings, and I'd like to to create a Map, where the key is the string of the list, and the value is Integer (a zero). My goal is, to counting the elements of String list (later in my code). I know it is easy to convert it, in the "old" way; Map<String,Integer> namesMap = new HasMap<>();

Is there any way to stop a Stream.generate from its Lambda closure?

随声附和 提交于 2020-08-19 04:13:16
问题 I just started playing around Java 8 and Lambda Expression and I am curious if I can stop the Stream generation from inside the Lambda expession by returning a specific value (like null). Is this possible with Stream.generate()? private int counter; private void generate() { System.out.println(Stream.generate(() -> { if (counter < 10) { counter++; return RandomUtils.nextInt(100); } else { return null; } }).count()); } Unfortunately this code does not terminate, so by simply returning null

Is there any way to stop a Stream.generate from its Lambda closure?

喜你入骨 提交于 2020-08-19 04:10:19
问题 I just started playing around Java 8 and Lambda Expression and I am curious if I can stop the Stream generation from inside the Lambda expession by returning a specific value (like null). Is this possible with Stream.generate()? private int counter; private void generate() { System.out.println(Stream.generate(() -> { if (counter < 10) { counter++; return RandomUtils.nextInt(100); } else { return null; } }).count()); } Unfortunately this code does not terminate, so by simply returning null

How to convert Optional<Object> to Optional<String> [closed]

柔情痞子 提交于 2020-08-18 19:30:47
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 12 months ago . Improve this question I have this code in Java 11 Object a = getObjectOrNullIfNotAvailable(); String value = a==null ? null : a.toString(); I want to write this code using Optional, the best I could come up with is. I haven't tried running it but I suspect it will work Optional<Object> oa =

How to convert Optional<Object> to Optional<String> [closed]

半世苍凉 提交于 2020-08-18 19:30:27
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 12 months ago . Improve this question I have this code in Java 11 Object a = getObjectOrNullIfNotAvailable(); String value = a==null ? null : a.toString(); I want to write this code using Optional, the best I could come up with is. I haven't tried running it but I suspect it will work Optional<Object> oa =

How to convert Optional<Object> to Optional<String> [closed]

巧了我就是萌 提交于 2020-08-18 19:28:38
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 12 months ago . Improve this question I have this code in Java 11 Object a = getObjectOrNullIfNotAvailable(); String value = a==null ? null : a.toString(); I want to write this code using Optional, the best I could come up with is. I haven't tried running it but I suspect it will work Optional<Object> oa =

How to convert Optional<Object> to Optional<String> [closed]

北城以北 提交于 2020-08-18 19:28:07
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 12 months ago . Improve this question I have this code in Java 11 Object a = getObjectOrNullIfNotAvailable(); String value = a==null ? null : a.toString(); I want to write this code using Optional, the best I could come up with is. I haven't tried running it but I suspect it will work Optional<Object> oa =

Java use void method for stream mapping?

て烟熏妆下的殇ゞ 提交于 2020-08-18 10:06:05
问题 Let's say I have a void method that just does transformation on an object, without returning any value, and I want to use it in a context of a stream map() function, like this: public List<MyObject> getList(){ List<MyObject> objList = ... return objList.stream().map(e -> transform(e, e.getUuid())).collect(Collectors.toList()); } private void transform(MyObject obj, String value){ obj.setUuid("prefix" + value); } The example is made up for simplicity - the actual method is doing something else