java-8

Java 8 function that always return the same value without regarding to parameter

☆樱花仙子☆ 提交于 2020-01-11 08:17:54
问题 Is there a predefined Function in Java 8 that does something like this: static <T, R> Function<T, R> constant(R val) { return (T t) -> { return val; }; } To answer people's query on why I need this function here is the real usage when I am trying to parse an integer to an roman numerals: // returns the stream of roman numeral symbol based // on the digit (n) and the exponent (of 10) private static Stream<Symbol> parseDigit(int n, int exp) { if (n < 1) return Stream.empty(); Symbol base =

TLS extension “Server Name Indication” (SNI): value not available on server side

不想你离开。 提交于 2020-01-11 06:21:17
问题 Based on the JSSE examples, I'm trying to get the value of the TLS parameter "Server Name Indication" (SNI) on the server side - without success. I'm sure that the value is sent by the client since I used a network sniffer (Wireshark) that shows the value. But when I use the following code fragment, the list of server name parameters is empty (while the "protocols" are shown): public void connectionAccepted(Socket socket) { System.out.println("Connection accepted!"); try { /* get SNI

Understanding sequential vs parallel stream spliterators in Java 8 and Java 9

给你一囗甜甜゛ 提交于 2020-01-11 06:21:05
问题 A question about spliterators that at first glance is not straightforward. In streams, .parallel() changes the behaviour that the stream is processed. However I was expecting the spliterators created from sequential and parallel streams to be the same. For example, in sequential streams typically, the .trySplit() is never invoked , while in parallel streams it is, in order to hand over the split spliterator to another thread. Differences between stream.spliterator() vs stream.parallel()

Get Month Name of java.time.chrono.HijrahDate instance

眉间皱痕 提交于 2020-01-11 05:33:10
问题 HijrahDate hd=HijrahChronology.INSTANCE.date(LocalDate.of(2014,11, 25)); If we have HijrahDate Instance , it is expected to have a method in UmalquraCalendar API that shows the name of month : i inspect properties of this instance using groovy API : ['era':AH, 'class':class java.time.chrono.HijrahDate, 'prolepticMonth':17233, 'eraValue':1, 'dayOfWeek':2, 'leapYear':false, 'chronology':Hijrah-umalqura, 'dayOfYear':32] However we don't find the month name which must be one of the following list

Collect Lines using Multimap Collector

喜欢而已 提交于 2020-01-11 05:22:10
问题 Is there a way to covert the below to using collectors yet? List<String[]> lines = getLines(); Multimap<String,String> multimap = ArrayListMultimap.create(); lines.forEach(line -> multimap.put(line[0],line[1]); ); 回答1: You can use Multimaps.toMultimap collector: ListMultimap<String, String> multimap = lines.stream() .collect(Multimaps.toMultimap( l -> l[0], l -> l[1], ArrayListMultimap::create )); Or if you don't need mutability, use ImmutableListMultimap.toImmutableListMultimap collector:

How to use CompletableFuture.thenComposeAsync()?

最后都变了- 提交于 2020-01-11 05:09:09
问题 Given: public class Test { public static void main(String[] args) { int nThreads = 1; Executor e = Executors.newFixedThreadPool(nThreads); CompletableFuture.runAsync(() -> { System.out.println("Task 1. Thread: " + Thread.currentThread().getId()); }, e).thenComposeAsync((Void unused) -> { return CompletableFuture.runAsync(() -> { System.out.println("Task 2. Thread: " + Thread.currentThread().getId()); }, e); }, e).join(); System.out.println("finished"); } } I am expecting a single executor

Java 8 Stream filtering value of list in a list

守給你的承諾、 提交于 2020-01-11 04:43:21
问题 I have a an object which looks like the following class MyObject { String type; List<String> subTypes; } Is it possible, given a list of MyObject's to use Java 8 streams to filter on both the type and then the subtype? So far I have myObjects.stream() .filter(t -> t.getType().equals(someotherType) .collect(Collections.toList()); but within this I also want another filter on each of the subTypes filtering those on a particular subtype too. I can't figure out how to do this. An example would be

Is there a real reason to use Optional.of()?

喜欢而已 提交于 2020-01-11 04:43:13
问题 I've read here why Optional.of() should be used over Optional.ofNullable() , but the answer didn't satisfy me at all, so I ask slightly different: If you are SURE that your method does not return null , why should you use Optional at all? As far as I know, the more or less only purpose of it is to remind the "user of a method", that he might have to deal with null -values. If he does not have to deal with null -values, why should he be bothered with an Optional ? I ask, because I recently

Return value from Optional [closed]

元气小坏坏 提交于 2020-01-11 04:13:04
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 4 years ago . How to return a String value from an Optional<String> using ifPresent and avoiding NullPointerException ? Example: public String longestName() { Optional<String> longName = someList.stream().reduce((name1, name2) -> name1.length() > name2.length() ? name1 : name2); // If I do not want to use

Join strings with different last delimiter

£可爱£侵袭症+ 提交于 2020-01-11 02:10:15
问题 Using stream.collect(Collectors.joining(", ")) I can easily join all the strings of my stream delimited by a comma. A possible result would be "a, b, c" . But what if I want the last delimiter to be different. For example to be " and " such that I get "a, b and c" as result. Is there an easy solution? 回答1: If they are already in a list, no stream is needed; simply join a sublist of all but the last element and concat the other delimiter and the final element: int last = list.size() - 1;