java-8

Is .collect guaranteed to be ordered on parallel streams?

六眼飞鱼酱① 提交于 2019-12-29 02:45:16
问题 Given I have a list of Strings List<String> toProcess . The results have to be in the order the original lines were given. I want to utilize the new parallel streams. Does the following code guarantee that the results will be in the same order they were in the original list? // ["a", "b", "c"] List<String> toProcess; // should be ["a", "b", "c"] List<String> results = toProcess.parallelStream() .map(s -> s) .collect(Collectors.toList()); 回答1: TL;DR Yes, the order is guaranteed. Stream.collect

when i use Java 8 Stream.of primitive type, the result is confused

て烟熏妆下的殇ゞ 提交于 2019-12-29 00:44:14
问题 byte[] a = {1,2,3}; System.out.println(Stream.of(a).count()); Byte[] b = {1,2,3}; System.out.println(Stream.of(b).count()); the result is 1 and 3, why? 回答1: Stream.of only accepts Objects as its arguments. A byte isn't an Object, but a byte array is. If a is an array of byte , then Stream.of(a) can only mean "stream of this one object, which is an array". If you have a Byte[] array, then each element of the array is an object, so the compiler can guess that's what you mean. There is

Reference to method is ambiguous when using lambdas and generics

↘锁芯ラ 提交于 2019-12-28 12:16:18
问题 I am getting an error on the following code, which I believe should not be there... Using JDK 8u40 to compile this code. public class Ambiguous { public static void main(String[] args) { consumerIntFunctionTest(data -> { Arrays.sort(data); }, int[]::new); consumerIntFunctionTest(Arrays::sort, int[]::new); } private static <T> void consumerIntFunctionTest(final Consumer<T> consumer, final IntFunction<T> generator) { } private static <T> void consumerIntFunctionTest(final Function<T, ?>

Java 8 Stream: difference between limit() and skip()

不打扰是莪最后的温柔 提交于 2019-12-28 11:44:51
问题 Talking about Stream s, when I execute this piece of code public class Main { public static void main(String[] args) { Stream.of(1,2,3,4,5,6,7,8,9) .peek(x->System.out.print("\nA"+x)) .limit(3) .peek(x->System.out.print("B"+x)) .forEach(x->System.out.print("C"+x)); } } I get this output A1B1C1 A2B2C2 A3B3C3 because limiting my stream to the first three components forces actions A , B and C to be executed only three times. Trying to perform an analogous computation on the last three elements

Java 8 Stream: difference between limit() and skip()

喜欢而已 提交于 2019-12-28 11:44:34
问题 Talking about Stream s, when I execute this piece of code public class Main { public static void main(String[] args) { Stream.of(1,2,3,4,5,6,7,8,9) .peek(x->System.out.print("\nA"+x)) .limit(3) .peek(x->System.out.print("B"+x)) .forEach(x->System.out.print("C"+x)); } } I get this output A1B1C1 A2B2C2 A3B3C3 because limiting my stream to the first three components forces actions A , B and C to be executed only three times. Trying to perform an analogous computation on the last three elements

Is there a Collector that collects to an order-preserving Set?

独自空忆成欢 提交于 2019-12-28 08:40:02
问题 Collectors.toSet() does not preserve order. I could use Lists instead, but I want to indicate that the resulting collection does not allow element duplication, which is exactly what Set interface is for. 回答1: You can use toCollection and provide the concrete instance of the set you want. For example if you want to keep insertion order: Set<MyClass> set = myStream.collect(Collectors.toCollection(LinkedHashSet::new)); For example: public class Test { public static final void main(String[] args)

Is there a Collector that collects to an order-preserving Set?

不问归期 提交于 2019-12-28 08:39:13
问题 Collectors.toSet() does not preserve order. I could use Lists instead, but I want to indicate that the resulting collection does not allow element duplication, which is exactly what Set interface is for. 回答1: You can use toCollection and provide the concrete instance of the set you want. For example if you want to keep insertion order: Set<MyClass> set = myStream.collect(Collectors.toCollection(LinkedHashSet::new)); For example: public class Test { public static final void main(String[] args)

How to provide ntlm authentication while calling any url?

时光怂恿深爱的人放手 提交于 2019-12-28 07:02:29
问题 I have a hosted url which authenticates using ntlm (windows Integrated authentication). I am on windows and using java 1.8 URL url = new URL("someUrl"); HttpURLConnection con = (HttpURLConnection) url.openConnection(); // con.setInstanceFollowRedirects(false); con.setRequestProperty("Content-Type", "application/json"); con.setRequestMethod("GET"); int responseCode = con.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { // read response ... in.close(); }else{ System.out

Java 8's orElse not working as expected

筅森魡賤 提交于 2019-12-28 06:41:38
问题 Consider the following method which returns a field if it exists or recursively calls itself until the field is found: private Field getField(Class<?> clazz, String p) { Optional<Field> field = Arrays.stream(clazz.getDeclaredFields()) .filter(f -> p.equals(f.getName())) .findFirst(); return field.isPresent() ? field.get() : getField(clazz.getSuperclass(), p); } While this works, I thought I could shorten it to: private Field getField(Class<?> clazz, String p) { return Arrays.stream(clazz

Please Explain Java 8 Method Reference to instance Method using class name

☆樱花仙子☆ 提交于 2019-12-28 06:33:12
问题 public interface MyFunc<T> { boolean func(T v1, T v2); } public class HighTemp { private int hTemp; HighTemp(){ } public HighTemp(int ht) { this.hTemp = ht; } boolean sameTemp(HighTemp ht2){ return hTemp == ht2.hTemp; } boolean lessThanTemp(HighTemp ht2){ return hTemp < ht2.hTemp; } } class InstMethWithObjRef { static <T> int counter(T[] vals, MyFunc<T> f, T v){ int count = 0; for (int i = 0; i < vals.length; i++) { if(f.func(vals[i], v)) count++; } return count; } public static void main