java-8

Unable to use forEach and lambda in android min sdk version less than 24

不问归期 提交于 2021-02-07 05:54:05
问题 I am using jack compiler in my android project. My min sdk is set to 18. I am unable to use forEach and lambda in my code. It asks me to change my min sdk to 24 or higher. I was hoping that with the integration of JACK compiler I would be able to use this forEach with lambda easily. Is there any work around for this other than bumping up the min sdk? 来源: https://stackoverflow.com/questions/42276329/unable-to-use-foreach-and-lambda-in-android-min-sdk-version-less-than-24

How to safely serialize a lambda?

别说谁变了你拦得住时间么 提交于 2021-02-07 02:38:09
问题 Although it is possible to serialize a lambda in Java 8, it is strongly discouraged; even serializing inner classes is discouraged. The reason given is that lambdas may not deserialize properly on another JRE. However, doesn't this mean that there is a way to safely serialize a lambda? For example, say I define a class to be something like this: public class MyClass { private String value; private Predicate<String> validateValue; public MyClass(String value, Predicate<String> validate) { this

What's the best way of ensuring a Function argument is serializable?

折月煮酒 提交于 2021-02-06 15:39:18
问题 I'm writing a serializable class that takes several arguments, including a Function : public class Cls implements Serializable { private final Collection<String> _coll; private final Function<String, ?> _func; public Cls(Collection<String> coll, Function<String, ?> func) { _coll = coll; _func = func; } } func is stored in a member variable, and so needs to be serializable. Java lambdas are serializable if the type they're being assigned to is serializable. What's the best way to ensure that

Method reference in Java 8

假装没事ソ 提交于 2021-02-06 15:26:11
问题 public class Car { private int maxSpeed; public Car(int maxSpeed) { this.maxSpeed = maxSpeed; } public int getMaxSpeed() { return maxSpeed; } } We can sort a list of cars by, Car carX = new Car(155); Car carY = new Car(140); List<Car> cars = new ArrayList<>(); cars.add(carX); cars.add(carY); cars.sort(Comparator.comparing(Car::getMaxSpeed)); If we see the signature of the method Comparator.comparing , the input parameter type is Function<? super T, ? extends U> In the above example, how is

How To Use Classic Custom Data Structures As Java 8 Streams

有些话、适合烂在心里 提交于 2021-02-06 12:46:10
问题 I saw a SO question yesterday about implementing a classic linked list in Java. It was clearly an assignment from an undergraduate data structures class. It's easy to find questions and implementations for lists, trees, etc. in all languages. I've been learning about Java lambdas and trying to use them at every opportunity to get the idiom under my fingers. This question made me wonder: How would I write a custom list or tree so I could use it in all the Java 8 lambda machinery? All the

Difference between stream().map() and stream.map({}) in java 8 [duplicate]

旧街凉风 提交于 2021-02-06 11:26:46
问题 This question already has answers here : When are braces optional in Java 8 lambda syntax? (4 answers) Closed 5 years ago . Yesterday i stumbled over something i neither really understand nor i'm able to find an explanation: Consider the following operations: Stream.of(1, 2, 3).map(i -> i * 2).forEach(System.out::println); //This one won't compile Stream.of(1, 2, 3).map(i -> { i * 2; }).forEach(System.out::println); It appears that the second one can be extended to Stream.of(1, 2, 3).map(i ->

Difference between stream().map() and stream.map({}) in java 8 [duplicate]

最后都变了- 提交于 2021-02-06 11:25:26
问题 This question already has answers here : When are braces optional in Java 8 lambda syntax? (4 answers) Closed 5 years ago . Yesterday i stumbled over something i neither really understand nor i'm able to find an explanation: Consider the following operations: Stream.of(1, 2, 3).map(i -> i * 2).forEach(System.out::println); //This one won't compile Stream.of(1, 2, 3).map(i -> { i * 2; }).forEach(System.out::println); It appears that the second one can be extended to Stream.of(1, 2, 3).map(i ->

Java 8 Stream - parallel execution - different result - why?

99封情书 提交于 2021-02-06 10:15:06
问题 Let's say i have a List<Integer> ints = new ArrayList<>(); and i want to add values to it and compare the results of parallel execution using forEach() and Collectors.toList() . First i add to this list some values from an sequential IntStream and forEach: IntStream.range(0,10).boxed().forEach(ints::add); And i get the correct result: ints ==> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Now i .clear() the list and do the same thing in parallel: IntStream.range(0,10).parallel().boxed().forEach(ints::add);

Java - How can I disable a TLS cipher for only some protocols using JVM Config?

こ雲淡風輕ζ 提交于 2021-02-06 09:49:05
问题 I've seen lots of examples of disabling TLS ciphers in java using jdk.tls.disabledAlgorithms, for example: jdk.tls.disabledAlgorithms=MD2, RSA keySize < 1024, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 But how can I disable a cipher for only certain protocols, using jdk.tls.disabledAlgorithms or a similar config? For example, how can I disable TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 for TLSv1.1 only? It doesn't seem to support the opensssl way of doing this, which is like so: TLSv1.1:!TLS_ECDHE_RSA

Map alternative for primitive values

时光总嘲笑我的痴心妄想 提交于 2021-02-06 09:42:09
问题 I did some profiling on my application and one of the results turned out that about 18% of memory on the heap is used by objects of type Double . It turns out these objects are the values in Map s, where I cannot use the primitive type. My reasoning is that the primitive type of double consumes less memory than it's object Double . Is there a way to have a map like data structure, that would accept any type as key and a primitive double as values? Main operations would be: insertion (probably