java-stream

How to code Stream.findNth()?

有些话、适合烂在心里 提交于 2020-01-09 10:54:07
问题 Similar to Stream.findFirst() , is there a way to write Stream.findNth() ? I'm practicing Java 8 by rewriting some legacy code. And, I'm wondering how the below function can be written using Stream API. static curPipeNumber = 0; /** skipToEntry() updates 'curPipeNumber' to 'pipeNumber' and returns the first byte position of the word before the ('pipeNumber')-th pipe. * It does so by reading and ignoring unnecessary bytes from the buffer. * e.g., */ static int skipToEntry(byte[] buf, int len,

Any filter-like lambda operation which does not discard?

时光总嘲笑我的痴心妄想 提交于 2020-01-09 10:32:37
问题 I basically would like to do something like: assertEquals(Arrays.asList(1,2,3).stream() .noDiscardingFilter(x -> x!=1) .map(x -> x*10) .collect(Collectors.toList()), Arrays.asList(1,20,30) ) This is an example, I don't need to get an answer on how to solve out that particular problem, it's just an example to show what's the fancy stuff I'm coming after. 回答1: Any intermediate step affects the entire stream pipeline. There is no recognizable rule behind your wish that the noDiscardingFilter

Picking elements of a list until condition is met with Java 8 Lambdas

心已入冬 提交于 2020-01-09 10:07:48
问题 I am trying to switch my mind to think the functional way and recently faced a situation in which I needed to pick up elements from a list until a condition is met and I could not find an easy natural way of achieving this. Obviously I am still learning. Say I have this list: List<String> tokens = Arrays.asList("pick me", "Pick me", "pick Me", "PICK ME", "pick me and STOP", "pick me", "pick me and Stop", "pick me"); // In a non lambdas was you would do it like below List<String> myTokens =

Picking elements of a list until condition is met with Java 8 Lambdas

落花浮王杯 提交于 2020-01-09 10:05:08
问题 I am trying to switch my mind to think the functional way and recently faced a situation in which I needed to pick up elements from a list until a condition is met and I could not find an easy natural way of achieving this. Obviously I am still learning. Say I have this list: List<String> tokens = Arrays.asList("pick me", "Pick me", "pick Me", "PICK ME", "pick me and STOP", "pick me", "pick me and Stop", "pick me"); // In a non lambdas was you would do it like below List<String> myTokens =

Java 8 lambdas group list into map

时光总嘲笑我的痴心妄想 提交于 2020-01-08 17:13:10
问题 I want to take a List<Pojo> and return a Map<String, List<Pojo>> where the Map 's key is a String value in Pojo , let's call it String key . To clarify, given the following: Pojo 1: Key:a value:1 Pojo 2: Key:a value:2 Pojo 3: Key:b value:3 Pojo 4: Key:b value:4 I want a Map<String, List<Pojo>> with keySet() sized 2, where key "a" has Pojos 1 and 2, and key "b" has pojos 3 and 4. How could I best achieve this using Java 8 lambdas? 回答1: It seems that the simple groupingBy variant is what you

String to 2D Array with streams [Java 8]

一个人想着一个人 提交于 2020-01-07 14:47:21
问题 I am currently learning how to do streams, while doing some assignments for fun (Advent of code). While I know how how to do it with temporary arrays, I do not know the correct syntax for streams or if it is even possible to do it in one line. public class M3 { public static void main(String[] args) { String s = "104\t240\t147\t246\t123\t175\t372\t71\t116\t230\t260\t118\t202\t270\t277\t292\n" + "740\t755\t135\t205\t429\t822\t844\t90\t828\t115\t440\t805\t526\t91\t519\t373\n" + "1630\t991\t1471

How to get the sum from nested stream

喜你入骨 提交于 2020-01-07 08:48:38
问题 I am new to Java 8 streams, I am unable to get the correct output from the nested loop operational sum. In the nested loop, I am calling a method which is returning an integer; by this doing finally summing up the resultant total This is what I tried: import java.util.ArrayList; import java.util.List; import java.util.stream.IntStream; public class NestedLoop { public void callTask(int i, int j) { System.out.println("i:"+i+"j:"+j); } public int getCount(Count count, String j) { if(count

convert method to java 8 lambda

扶醉桌前 提交于 2020-01-07 02:15:11
问题 How to write this method in functional programming by using Java 8 lambda / stream() in a short more intuitive way? int animation = R.style.DialogAnimationSlideHorizontal; String body; String subject = mSubjectView.getText().toString(); BaseDialog dialog = dialogFactory.getType(DialogTypes.DIALOG_FULL); if (!checkFields()) { // one of the recipients is invalid. body = getString(R.string.bad_address); dialog.showDialog(body, animation, new DialogBuilder.Positive() { @Override public void

Efficient way to group by a given list based on a key and collect in same list java 8

无人久伴 提交于 2020-01-06 12:39:07
问题 I have the below class: class A{ String property1; String property2; Double property3; Double property4; } So the property1 and property2 is the key. class Key{ String property1; String property2; } I already have a list of A like below: List<A> list=new ArrayList<>(); I want to group by using the key and add to another list of A in order to avoid having multiple items with same key in the list: Function<A, Key> keyFunction= r-> Key.valueOf(r.getProperty1(), r.getProperty2()); But then while

Efficient way to group by a given list based on a key and collect in same list java 8

放肆的年华 提交于 2020-01-06 12:38:18
问题 I have the below class: class A{ String property1; String property2; Double property3; Double property4; } So the property1 and property2 is the key. class Key{ String property1; String property2; } I already have a list of A like below: List<A> list=new ArrayList<>(); I want to group by using the key and add to another list of A in order to avoid having multiple items with same key in the list: Function<A, Key> keyFunction= r-> Key.valueOf(r.getProperty1(), r.getProperty2()); But then while