java-stream

How to find unique value of a column of a 2D ArrayList in java?

試著忘記壹切 提交于 2019-12-07 04:51:15
问题 import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Set; import java.util.function.Function; import java.util.stream.Collectors; public class Delete { public static void main(String[] args) { List<List<String>> list = new ArrayList<>(); list.add(List.of("A","B","C","R")); list.add(List.of("E","F","G","F")); list.add(List.of("A","B","C","D")); System.out.println(list.stream().distinct().count()); Map<String, Long> countMapOfColumn

Parallel stream doesn't set Thread.contextClassLoader after tomcat upgrade

不问归期 提交于 2019-12-07 03:59:25
问题 After tomcat upgrade from 8.5.6 to 8.5.28 parallel stream stopped supplying Threads with contextClassLoader: Because of it Warmer::run can't load classes in it. warmers.parallelStream().forEach(Warmer::run); Do you have any ideas what Tomcat was supplying for contextClassLoaders for new Threads? ParallelStream uses ForkJoinPool in newest Tomcat. 回答1: Common ForkJoin pool is problematic and could be responsible for memory leaks and for applications being able to load classes and resources from

Java 8 complex custom collector

≯℡__Kan透↙ 提交于 2019-12-07 03:37:21
问题 I have a stream of objects which I would like to collect the following way. Let's say we are handling forum posts: class Post { private Date time; private Data data } I want to create a list which groups posts by a period. If there were no posts for X minutes, create new group. class PostsGroup{ List<Post> posts = new ArrayList<> (); } I want to get a List<PostGroups> containing the posts grouped by the interval. example: interval 10 minutes. posts: [{time:x, data:{}}, {time:x + 3, data:{}} ,

Java Streams - Get a “symmetric difference list” from two other lists

徘徊边缘 提交于 2019-12-07 03:25:19
问题 Im trying to use Java 8 streams to combine lists. How can I get a "symmetric difference list" (all object that only exist in one list) from two existing lists. I know how to get an intersect list and also how to get a union list. In the code below I want the disjoint Cars from the two lists of cars (bigCarList,smallCarList). I expect the result to be a list with the 2 cars ("Toyota Corolla" and "Ford Focus") Example code: public void testDisjointLists() { List<Car> bigCarList =

Performance for Java Stream.concat VS Collection.addAll

廉价感情. 提交于 2019-12-07 03:20:59
问题 For the purpose of combining two sets of data in a stream. Stream.concat(stream1, stream2).collect(Collectors.toSet()); Or stream1.collect(Collectors.toSet()) .addAll(stream2.collect(Collectors.toSet())); Which is more efficient and why? 回答1: For the sake of readability and intention, Stream.concat(a, b).collect(toSet()) is way clearer than the second alternative. For the sake of the question, which is " what is the most efficient ", here a JMH test (I'd like to say that I don't use JMH that

Java 8 extending stream<T>

試著忘記壹切 提交于 2019-12-07 02:56:50
问题 I'm attempting to extend Java 8's Stream implementation. I have this interface: public interface StreamStuff<T> extends Stream<T> { Stream<T> delegate(); default Stream<T> biggerThanFour() { return delegate().filter(i -> ((Double)i > 4)); } } And in my main method: int arr [] = {1,2,3,4,5,6}; Object array [] = ((StreamStuff)Arrays .stream(arr)) .biggerThanFour() .toArray(); I'm trying to cast the Stream, to my interface StreamStuff, and use my method. Im getting the following error: Exception

How to filter the age while grouping in map with list

喜欢而已 提交于 2019-12-07 02:27:20
问题 Similar to my previous question here, the User objects I have are these new User("ayush","admin",23) new User("ashish","guest",19) new User("ashish","admin",20) new User("garima","guest",29) new User("garima","super",45) new User("garima","guest",19) Now I am trying to get the name to varying ages trend for these users. But I need to filter them above a threshold age. I could get the trend using Map<String, List<Integer>> userNameAndAgeTrend = users.stream().collect(Collectors.groupingBy(user

Invert Map with list value Map<Key, List<Value>> to Map <Value, Key> in Java 8

一世执手 提交于 2019-12-07 02:13:22
问题 I have a map kind of grouping values by key Map<String, List<Integer>> , i want to revert in order to map each value to the corresponding key Example: I want to transform the code below Map<String, List<Integer>> mapOfIntList = new HashMap<String, List<Integer>>(); mapOfIntList.put("UNIT", Arrays.asList(1, 2, 3, 8, 7, 0, 8, 6)); mapOfIntList.put("TEN", Arrays.asList(24, 90, 63, 87)); mapOfIntList.put("HUNDRED", Arrays.asList(645, 457, 306, 762)); mapOfIntList.put("THOUSAND", Arrays.asList

Java stream - groupingBy by a nested list (list in a second order)

假装没事ソ 提交于 2019-12-07 01:11:57
问题 I have the following data structure - List of Students that each holds a lists of States that each holds a list of cities. public class Student { private int id; private String name; private List<State> states = new ArrayList<>(); } public class State { private int id; private String name; private List<City> Cities = new ArrayList<>(); } public class City { private int id; private String name; } I want to get the following. Map<String, Students> citiesIdsToStudensList; I write the following

Use of double colons - difference between static and non-static method references [duplicate]

安稳与你 提交于 2019-12-07 00:42:14
问题 This question already has answers here : :: (double colon) operator in Java 8 (17 answers) Closed 3 years ago . Edit: My question here was answered. To summarize, I was confused about the usage of non-static method references. There the functional interface and referenced method have a different number of parameters. What answered my question is the comment and the accepted answer. I am currently reading the Java Tutorial about Stream reduction methods (https://docs.oracle.com/javase/tutorial