java-stream

Producing histogram Map for IntStream raises compile-time-error

扶醉桌前 提交于 2019-12-06 10:24:41
问题 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>

Iterating over two lists using Java 8 streams

为君一笑 提交于 2019-12-06 10:22:00
问题 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; 回答1: 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

Filter on map of map

江枫思渺然 提交于 2019-12-06 09:49:40
I have below map of map and want to filter it based on a value. The result should be assigned back to same map. Please let know what is the best approach for this. Map<String, Map<String, Employee>> employeeMap; < dep1, <"empid11", employee11> <"empid12",employee12> dep2, <"empid21", employee21> <"empid22",employee22> > Filter: employee.getState="MI" I tried like below but i was not able to access the employee object currentMap = currentMap.entrySet().stream() **.filter(p->p.getValue().getState().equals("MI"))** .collect(Collectors.toMap(p -> p.getKey(),p->p.getValue())); If you want to modify

Java 8 Stream Filter - Sort based pdate

二次信任 提交于 2019-12-06 09:27:06
Am trying to sort the filed in filter. Input Document / Sample Record: DocumentList: [ Document{ { _id=5975ff00a213745b5e1a8ed9, u_id=, mailboxcontent_id=5975ff00a213745b5e1a8ed8, idmapping=Document{ {ptype=PDF, cid=00988, normalizedcid=00988, systeminstanceid=, sourceschemaname=, pid=0244810006} }, batchid=null, pdate=Tue Jul 11 17:52:25 IST 2017, locale=en_US } }, Document{ { _id=597608aba213742554f537a6, u_id=, mailboxcontent_id=597608aba213742554f537a3, idmapping=Document{ {platformtype=PDF, cid=00999, normalizedcid=00999, systeminstanceid=, sourceschemaname=, pid=0244810006} }, batchid

How do you build an infinite repeating stream from a finite stream in Java 8?

谁都会走 提交于 2019-12-06 09:15:27
How can I turn a finite Stream of things Stream<Thing> into an infinite repeating stream of things? Boris the Spider is right: a Stream can only be traversed once, so you need a Supplier<Stream<Thing>> or you need a Collection. <T> Stream<T> repeat(Supplier<Stream<T>> stream) { return Stream.generate(stream).flatMap(s -> s); } <T> Stream<T> repeat(Collection<T> collection) { return Stream.generate(() -> collection.stream()).flatMap(s -> s); } Example invocations: Supplier<Stream<Thing>> stream = () -> Stream.of(new Thing(1), new Thing(2), new Thing(3)); Stream<Thing> infinite = repeat(stream);

Performing an operation over the elements of one collection and iterating over the result to perform some other operation

陌路散爱 提交于 2019-12-06 08:53:57
If I have 2 collections, List<String> domainArr; List<Person> personArr; I would like to make a minor transformation on each of the elements in the String and then iterate over the personArr to List<String> urlArr = strArr.stream() .map(str -> "https://" + strArr) .collect(Collectors.toList()); I have a method like List<Person> getPersons(String url){ /*makes a restful call to the url and gets a List of objects for each URL.*/ } I would like to iterate over each of the urls from urlArr and pass it to the getPersons(url) method and for each of the obtained result (a List), I would like to

Convert InputStream into Stream<String> of strings of fixed length

混江龙づ霸主 提交于 2019-12-06 08:42:48
问题 Like in Convert InputStream into Stream<String> given a Charset I want to convert an InputStream is into a Stream<String> stream . But this time instead of splitting the InputStream at the new line characters, I want to split it into parts of equal length. So all strings of the stream would have the same length (with a possible exception on the last element of the stream, that may be shorter). 回答1: I don't think this is possible using class library methods only, so you'll have to write your

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

女生的网名这么多〃 提交于 2019-12-06 08:25:32
问题 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

May Java group&order&top in a call chain?

会有一股神秘感。 提交于 2019-12-06 08:15:32
问题 I have a POJO class class A { public int id; public String groupName; public String getGroupName() { return this.groupName; } public int value; public A(int id, String groupName, int value) { this.id = id; this.groupName = groupName; this.value = value; } } And id is Unique, but groupName is not. Then I have a List of A. List<A> list = new ArrayList<A>(); list.add(new A(1, "A", 3)); list.add(new A(2, "B", 5)); list.add(new A(3, "B", 7)); list.add(new A(4, "C", 7)); I want filter the list by

Is There Some Stream-Only Way To Determine The Index Of The Max Stream Element?

风流意气都作罢 提交于 2019-12-06 07:41:05
问题 I have a Stream<Set<Integer>> intSetStream . I can do this on it... Set<Integer> theSetWithTheMax = intSetStream.max( (x,y)->{ return Integer.compare( x.size(), y.size() ); } ).get( ); ...and I get a hold of the Set<Integer> that has the highest number of Integer elements in it. That's great. But what I really need to know is, is it the 1st Set in that Stream that's the max? Or is it the 10th Set in the Stream ? Or the i th Set ? Which one of them has the most elements in it? So my question