java-8

.forEach and .sort don't work and cannot set breakpoints in blocks

馋奶兔 提交于 2020-01-09 11:52:56
问题 I am using Java 8 (build 1.8.0_25), Netbeans 8.0.2 and am incorporating some of the Java 8 features into an existing app. Sorting and .forEach is not working so I have created some test code to ensure I understand lambdas, etc. and to diagnose the problem. Below is a mix of new code as well as code to interact with the data from my system: public void test(Registration reg) { /* new code */ List<String> family = new ArrayList<>(); family.add("Mom"); family.add("Dad"); family.add("Brother");

.forEach and .sort don't work and cannot set breakpoints in blocks

╄→гoц情女王★ 提交于 2020-01-09 11:52:46
问题 I am using Java 8 (build 1.8.0_25), Netbeans 8.0.2 and am incorporating some of the Java 8 features into an existing app. Sorting and .forEach is not working so I have created some test code to ensure I understand lambdas, etc. and to diagnose the problem. Below is a mix of new code as well as code to interact with the data from my system: public void test(Registration reg) { /* new code */ List<String> family = new ArrayList<>(); family.add("Mom"); family.add("Dad"); family.add("Brother");

.forEach and .sort don't work and cannot set breakpoints in blocks

懵懂的女人 提交于 2020-01-09 11:52:14
问题 I am using Java 8 (build 1.8.0_25), Netbeans 8.0.2 and am incorporating some of the Java 8 features into an existing app. Sorting and .forEach is not working so I have created some test code to ensure I understand lambdas, etc. and to diagnose the problem. Below is a mix of new code as well as code to interact with the data from my system: public void test(Registration reg) { /* new code */ List<String> family = new ArrayList<>(); family.add("Mom"); family.add("Dad"); family.add("Brother");

Creating String representation of lambda expression [duplicate]

北城以北 提交于 2020-01-09 11:35:06
问题 This question already has answers here : Is it possible to retrieve lambda expression at runtime (2 answers) Closed 5 years ago . For debugging purposes I am trying to create string representations of lambda expressions (specifically of Predicate s, though it would be interesting for other lambda expressions too) in Java 8. My idea would be something like this: public class Whatever { private static <T> String predicateToString(Predicate<T> predicate) { String representation = ... // do magic

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 =

Save a method into a variable, java 8

こ雲淡風輕ζ 提交于 2020-01-09 09:57:13
问题 Is it possible to save a method into a variable? I have a class which is called MyFilter and it filters Items on different fields. The constructor of MyFilter should ask 2 things: String, for example filter Items by language, "English" This should be a method for example: I have an Item and I want to check if the language == String given to the filter So I need to get the language of that Item, so Item.getLanguage()... I also need it for Item.getTitle(), Item.getId() and so on. I think this

Java 8: Interface with static methods instead of static util class

自古美人都是妖i 提交于 2020-01-09 07:12:27
问题 What is best practice in Java 8 when I need a bunch of stateless utility methods. Is it right to have an interface that will not be implemented by anyone i.e. public interface Signatures and public interface Environments , or is it better to do it the old way - have public final class Signatures and public final class Environments with private constructors || enums? 回答1: The main purpose of interfaces is to provide a type and a vocabulary of operations (methods) on that type. They're useful