java-8

Java8 unsigned arithmetic

梦想与她 提交于 2021-02-07 14:42:42
问题 Java 8 is widely reported to have library support for unsigned integers. However, there seem to be no articles explaining how to use it and how much is possible. Some functions like Integer.CompareUnsigned are easy enough to find and seem to do what one would expect. However, I fail to write even a simple loop that loops over all powers of two within the range of unsigned long. int i = 0; for(long l=1; (Long.compareUnsigned(l, Long.MAX_VALUE*2) < 0) && i<100; l+=l) { System.out.println(l); i+

Java8 unsigned arithmetic

佐手、 提交于 2021-02-07 14:41:50
问题 Java 8 is widely reported to have library support for unsigned integers. However, there seem to be no articles explaining how to use it and how much is possible. Some functions like Integer.CompareUnsigned are easy enough to find and seem to do what one would expect. However, I fail to write even a simple loop that loops over all powers of two within the range of unsigned long. int i = 0; for(long l=1; (Long.compareUnsigned(l, Long.MAX_VALUE*2) < 0) && i<100; l+=l) { System.out.println(l); i+

Java Method reference not expected here

爷,独闯天下 提交于 2021-02-07 14:26:28
问题 How exactly do you chain method references for instances with Java 8? Example: Collections.sort(civs,Comparator.comparing(Civilization::getStrategy.getStrategLevel)); getStrategy of a Civilization instance returns a Strategy object instance which has the instance method getStrategyLevel . Why doesn't the Comparator.comparing method return a comparator with it's functional interface implemented by the lambda expression? 回答1: In that case, you should use a lambda, you can't apply a method

Java - parse binary to long [duplicate]

旧时模样 提交于 2021-02-07 13:14:14
问题 This question already has an answer here : Java 8: Why can't I parse this binary string into a long? (1 answer) Closed 4 years ago . I have a binary represenation of a number and want to convert it to long (I have Java 8) public class TestLongs { public static void main(String[] args){ String a = Long.toBinaryString(Long.parseLong("-1")); // 1111111111111111111111111111111111111111111111111111111111111111 System.out.println(a); System.out.println(Long.parseLong(a, 2));// ??? but Long

Java - parse binary to long [duplicate]

帅比萌擦擦* 提交于 2021-02-07 13:13:58
问题 This question already has an answer here : Java 8: Why can't I parse this binary string into a long? (1 answer) Closed 4 years ago . I have a binary represenation of a number and want to convert it to long (I have Java 8) public class TestLongs { public static void main(String[] args){ String a = Long.toBinaryString(Long.parseLong("-1")); // 1111111111111111111111111111111111111111111111111111111111111111 System.out.println(a); System.out.println(Long.parseLong(a, 2));// ??? but Long

Java 8 Functional interface assignment context

霸气de小男生 提交于 2021-02-07 12:43:06
问题 Question is regarding assignment context of functional interface- Predicate<String> p = String::isEmpty; Works fine where the isEmpty method declaration in String class is - public boolean isEmpty(){} . If I try to declare same in custom class like - class Test{ public boolean isEmpty(){ ... } } And make same assignment - Predicate<String> p = Test::isEmpty; It would be compilation error - The type Test does not define isEmpty(String) that is applicable here And Predicate<T> represents a

Trying to escape the “@” symbol in a {@code} block within a javadoc comment with Netbeans 8.0

时光总嘲笑我的痴心妄想 提交于 2021-02-07 12:35:24
问题 I'm trying to insert a {@code } annotation in a Javadoc comment using Netbeans 8.0 and it's not working properly. I've seen other questions on this from before (i.e., How can you escape the @ character in javadoc?) but the html escape @ and {@literal @} both don't seem to work. My comment looks like this (using both methods for sake of the example): /** * blah blah blah * <p> * For example: * <pre> * {@code * {@literal @}begin_specification * ... * @end_specification * } * </pre> */ I can hit

How to extract a List<D> from a HashMap<E,R> using stream

空扰寡人 提交于 2021-02-07 12:27:17
问题 I want to know how to extract a List<D> from a HashMap<E,R> considering these constraints: E is a custom class; R is a custom class containing a Set<D> of custom objects; What I have tried: I tried addressing the issue in this question. In that previous case, I had a simple Map<E,List<R>> , but in this case I have to access the R class which has the targeted Set<D> . What I want to do in the following portion of the code is to get the Elements of the Set<D> whose country names are equal to

Mockito: Verifying a method was called with a functional parameter

最后都变了- 提交于 2021-02-07 12:22:17
问题 I have a simple scenario in which am trying to verify some behavior when a method is called (i.e. that a certain method was called with given parameter, a function pointer in this scenario). Below are my classes: @SpringBootApplication public class Application { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(Application.class, args); AppBootStrapper bootStrapper = context.getBean(AppBootStrapper.class); bootStrapper.start(); } }

Java map an array of Strings to an array of Integers

牧云@^-^@ 提交于 2021-02-07 12:15:24
问题 I found this code on SO to map strings to ints Arrays.stream(myarray).mapToInt(Integer::parseInt).toArray(); But how do I make it map to Integer type not the primitive int? I tried switching from Integer.parseInt to Integer.valueOf , but it seems that the mapToInt() method forces the primitive type. I have an ArrayList of arrays of Integers, so I cannot use primitive ints. 回答1: Since String and Integer are both reference types you can simply call Stream::map to transform your array. Integer[]