java-stream

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<>();

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

Counting all elements in map of string to list

回眸只為那壹抹淺笑 提交于 2020-08-05 07:24:54
问题 Is there a better way to calculate size of all lists in map? So firstList.size() + secondList.size() ? I have done this, but creating a list just for counting doesn't feel right approach .. public static void main(String[] args) { List<String> firstList = new ArrayList<>(); firstList.add("first"); List<String> secondList = new ArrayList<>(); secondList.add("second"); Map<String, List<String>> myMap = new HashMap<>(); myMap.put("one", firstList); myMap.put("two", secondList); int size = myMap

Counting all elements in map of string to list

99封情书 提交于 2020-08-05 07:23:05
问题 Is there a better way to calculate size of all lists in map? So firstList.size() + secondList.size() ? I have done this, but creating a list just for counting doesn't feel right approach .. public static void main(String[] args) { List<String> firstList = new ArrayList<>(); firstList.add("first"); List<String> secondList = new ArrayList<>(); secondList.add("second"); Map<String, List<String>> myMap = new HashMap<>(); myMap.put("one", firstList); myMap.put("two", secondList); int size = myMap

Append object to list and return result in Java 8?

故事扮演 提交于 2020-08-05 02:33:17
问题 Is there a way of appending an object to a list and returning the result in one line in a functional non-imperative way? How would you do it if also the original list should not be mutated? Java 8 is allowed. I already know how to concat two lists in one line. (Source) List listAB = Stream.concat(listA.stream(), listB.stream()).collect(Collectors.toList()); I also know how to make a list out of objects in one line. List listO1 = Collections.singletonList(objectA); List listO2 = Stream.of

Append object to list and return result in Java 8?

谁都会走 提交于 2020-08-05 02:31:28
问题 Is there a way of appending an object to a list and returning the result in one line in a functional non-imperative way? How would you do it if also the original list should not be mutated? Java 8 is allowed. I already know how to concat two lists in one line. (Source) List listAB = Stream.concat(listA.stream(), listB.stream()).collect(Collectors.toList()); I also know how to make a list out of objects in one line. List listO1 = Collections.singletonList(objectA); List listO2 = Stream.of

Java 8 list to nested map

假装没事ソ 提交于 2020-08-02 07:10:00
问题 I hava a list of Class A like class A { private Integer keyA; private Integer keyB; private String text; } I want to transfer aList to nested Map mapped by keyA and keyB So I create below code. Map<Integer, Map<Integer,List<A>>> aMappedByKeyAAndKeyB = aList.stream() .collect(Collectors.collectingAndThen(Collectors.groupingBy(A::getKeyA), result -> { Map<Integer, Map<Integer, List<A>>> nestedMap = new HashMap<Integer, Map<Integer, List<A>>>(); result.entrySet().stream().forEach(e -> {nestedMap

Java 8 list to nested map

只愿长相守 提交于 2020-08-02 07:04:14
问题 I hava a list of Class A like class A { private Integer keyA; private Integer keyB; private String text; } I want to transfer aList to nested Map mapped by keyA and keyB So I create below code. Map<Integer, Map<Integer,List<A>>> aMappedByKeyAAndKeyB = aList.stream() .collect(Collectors.collectingAndThen(Collectors.groupingBy(A::getKeyA), result -> { Map<Integer, Map<Integer, List<A>>> nestedMap = new HashMap<Integer, Map<Integer, List<A>>>(); result.entrySet().stream().forEach(e -> {nestedMap

Spring repository method which are returning Java 8 stream doesn't close JDBC connection

大城市里の小女人 提交于 2020-08-02 06:33:42
问题 I have a Spring data repository: @Repository interface SomeRepository extends CrudRepository<Entity, Long> { Stream<Entity> streamBySmth(String userId); } I am calling that method in some Spring bean: @Scheduled(fixedRate = 10000) private void someMethod(){ someRepository.streamBySmth("smth").forEach(this::callSomeMethod); } I am using MySQL database. And when I am running application after some successful method invocations it throws an exception: o.h.engine.jdbc.spi.SqlExceptionHelper : SQL

Lambda in Stream.map/filter not called

陌路散爱 提交于 2020-07-30 05:09:54
问题 I'm trying to find separate the duplicates and non-duplicates in a List by adding them to a Set and List while using Stream.filter and Stream.map List<String> strings = Arrays.asList("foo", "bar", "foo", "baz", "foo", "bar"); Set<String> distinct = new HashSet<>(); List<String> extras = new ArrayList<>(); strings .stream() .filter(x -> !distinct.add(x)) .map(extra -> extras.add(extra)); At the end of this, I expect distinct to be [foo, bar, baz] and extras to be [foo, foo, bar] , since there