java-stream

Lazy CSV Filtering / Parsing - Increasing Performance

蹲街弑〆低调 提交于 2019-12-04 15:43:12
Lazy Filtering CSV Files I had the need to filter through millions of log records, stored as numerous CSV files. The size of the records greatly exceeded my available memory so I wanted to go with a lazy approach. Java 8 Streams API With jdk8 we have the Streams API which paired with Apache commons-csv allows us to easily accomplish this. public class LazyFilterer { private static Iterable<CSVRecord> getIterable(String fileName) throws IOException { return CSVFormat .DEFAULT .withFirstRecordAsHeader() .parse(new BufferedReader(new FileReader(fileName))); } public static void main(String[] args

Why is the combiner of the Collector interface not consistent with the overloaded collect method?

巧了我就是萌 提交于 2019-12-04 15:39:08
问题 There is an overload method, collect() , in interface Stream<T> with the following signature: <R> R collect(Supplier<R> supplier, BiConsumer<R,? super T> accumulator, BiConsumer<R,R> combiner) There is another version of collect(Collector<? super T,A,R> collector) , which receives an object with the previous three functions. The property of the interface Collector corresponding to the combiner has the signature BinaryOperator<A> combiner() . In the latter case, the Java API 8 states that: The

How to use collect call in Java 8?

坚强是说给别人听的谎言 提交于 2019-12-04 15:33:46
问题 Lets say we have this boring piece of code that we all had to use: ArrayList<Long> ids = new ArrayList<Long>(); for (MyObj obj : myList){ ids.add(obj.getId()); } After switching to Java 8, my IDE is telling me that I can replace this code with collect call , and it auto-generates: ArrayList<Long> ids = myList.stream().map(MyObj::getId).collect(Collectors.toList()); However its giving me this error: collect(java.util.stream.Collector) in Steam cannot be applied to: (java.util.stream.Collector,

Producing histogram Map for IntStream raises compile-time-error

a 夏天 提交于 2019-12-04 15:29:31
I'm interested in building a Huffman Coding prototype. To that end, I want to begin by producing a histogram of the characters that make up an input Java String . I've seen many solutions on SO and elsewhere (e.g: here that depend on using the collect() methods for Stream s as well as static imports of Function.identity() and Collectors.counting() in a very specific and intuitive way. However, when using a piece of code eerily similar to the one I linked to above: private List<HuffmanTrieNode> getCharsAndFreqs(String s){ Map<Character, Long> freqs = s.chars().collect(Collectors.groupingBy

Java8 Stream : Collect elements after a condition is met

老子叫甜甜 提交于 2019-12-04 15:15:00
问题 My POJO is as follows class EventUser { private id; private userId; private eventId; } I retrieve EventUser object as follows: List<EventUser> eventUsers = eventUserRepository.findByUserId(userId); Say the 'eventUsers' is as follows: [ {"id":"id200","userId":"001","eventId":"1010"}, {"id":"id101","userId":"001","eventId":"4212"}, {"id":"id402","userId":"001","eventId":"1221"}, {"id":"id301","userId":"001","eventId":"2423"}, {"id":"id701","userId":"001","eventId":"5423"}, {"id":"id601","userId

Collectors.groupingBy() returns result sorted in ascending order java

▼魔方 西西 提交于 2019-12-04 15:13:04
I am sending result in descending order but I get output with ascending order List<myEntity> myData = new ArrayList<>(); Map<Integer,List<myEntity>> myid = new LinkedHashMap<>(); try { myData = myService.getData(id); myid = myData.stream().collect(Collectors.groupingBy(myEntity::getDataId)); Here mydata is sorted by desc order but after creating collections by group data id my list get sorted with ascending order. I want my collection list to be descending order not ascending order. As @Holger described in Java 8 is not maintaining the order while grouping , Collectors.groupingBy() returns a

Why is Stream.sorted not type-safe in Java 8?

你说的曾经没有我的故事 提交于 2019-12-04 14:59:36
问题 This is from the Stream interface from Oracle's implementation of JDK 8: public interface Stream<T> extends BaseStream<T, Stream<T>> { Stream<T> sorted(); } and it is very easy to blow this up at run time and no warning will be generated at compile time. Here is an example: class Foo { public static void main(String[] args) { Arrays.asList(new Foo(), new Foo()).stream().sorted().forEach(f -> {}); } } which will compile just fine but will throw an exception at run time: Exception in thread

Elegantly create map with object fields as key/value from object stream in Java 8

随声附和 提交于 2019-12-04 14:06:52
I have the following class class Person { public String name; public int age; public List<String> hobbies; Person(String name, int age, List<String> hobbies) {this.name = name; this.age = age; this.hobbies = hobbies;} } How do I create a Map of age to hobbies like Map<Integer, Set<String>> ? The Java 8 way I cooked up is: Map<Integer, Set<String>> collect8 = persons.stream() .collect( toMap( p -> p.age, p -> p.hobbies.stream().collect(toSet()), (hobbies1, hobbies2) -> Stream.concat(hobbies1.stream(), hobbies2.stream()).collect(toSet()) ) ); Is there a more idiomatic way of doing this with

Iterating over two lists using Java 8 streams

微笑、不失礼 提交于 2019-12-04 14:06:31
How can I write the following in Java 8 streams? int total = 0; for (ObjectA obja : rootObj.getListA()) { for (ObjectB objb : obja.getListB()) { total += objb.getCount() * obja.getCount(); } } return total; The canonical solution for converting nested for loops to Stream API usage is via flatMap : return rootObj.getListA().stream() .flatMapToInt(objA->objA.getListB().stream() .mapToInt(objB->objB.getCount() * objA.getCount())) .sum(); This allows you to perform an operation for each inner iteration. However, in the special case of summing you may simplify the operation as it doesn’t matter

Java 8 - Count of words and then arrange in desc order

时光怂恿深爱的人放手 提交于 2019-12-04 14:06:04
问题 I have a list of words, say List<String> words = Arrays.asList("Hello alan i am here where are you"+ "and what are you doing hello are you there"); How can I get the top seven words which are repeated more than once in the list in descending order? And then the single entry words should be arranged in alphabetical order. So the output for the above should be those top seven words you (3) are (2) hello (2) alan (1) am (1) and (1) doing (1) I am looking this to do in Java 8 using streams, lamda