java-8

How to monitor changes on objects contained in an ObservableList JavaFX

我只是一个虾纸丫 提交于 2019-12-29 06:45:09
问题 I really have difficulties to understand how the ObservableList object is working in JavaFX. I want to monitor if an object in the List has been modified. So far, I only see that I can monitor if the List , as an entity itself, has been modified... but not the objects within the List : ObservableList<Stuff> myList = FXCollections.<Stuff>observableArrayList(); myList.add(someStuff); myList.addListener((ListChangeListener.Change<? extends Stuff> change) -> { while(change.next()){ if(change

Should I use put() or putIfAbsent() after using getOrDefault()?

倖福魔咒の 提交于 2019-12-29 06:44:22
问题 Java8 introduced those nice methods getOrDefault() and putIfAbsent() , allowing to write code like: Map<Foo, List<Bar>> itemsByFoo = ... List<Bar> bars = itemsByFoo.getOrDefault(key, new ArrayList<>()); bars.add(someNewBar); Now I am wondering if there are good factual reasons to either do: itemsByFoo.put(key, bars); or itemsByFoo.putIfAbsent(key, bars); Both would work: option 1 might do a lot of unnecessary "put" calls when adding elements to lists happens often option2 might do a lot of

How to install oracle-java8-installer on docker debian:jessie

偶尔善良 提交于 2019-12-29 06:44:09
问题 I am trying to install java 8 through oracle-java8-installer on a debian:jessie docker container. The following is my Dockerfile: FROM debian:jessie ENV JAVA_VERSION 1.8.0 RUN echo "deb http://ppa.launchpad.net/webupd8team/java/ubuntu trusty main" > /etc/apt/sources.list.d/webupd8team-java.list RUN echo "deb-src http://ppa.launchpad.net/webupd8team/java/ubuntu trusty main" >> /etc/apt/sources.list.d/webupd8team-java.list RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys

How to sort a LinkedHashMap by value in decreasing order in java stream?

偶尔善良 提交于 2019-12-29 06:40:08
问题 To sort it int ascending order I can use: myMap.entrySet().stream() .sorted(Map.Entry.comparingByValue()) .collect(Collectors.toMap(Entry::getKey, Entry::getValue)); How can I do it in decreasing order? 回答1: To sort in reverse order, pass Comparator.reverseOrder() as parameter to comparingByValue . To get a LinkedHashMap , you must specifically request one with the 4-argument toMap() . If you don't specify what kind of a map you want, you will get whatever the default is, which currently

How to convert List to Map with indexes using stream - Java 8?

♀尐吖头ヾ 提交于 2019-12-29 06:38:29
问题 I've created method whih numerating each character of alphabet. I'm learning streams(functional programming) and try to use them as often as possible, but I don't know how to do it in this case: private Map<Character, Integer> numerateAlphabet(List<Character> alphabet) { Map<Character, Integer> m = new HashMap<>(); for (int i = 0; i < alphabet.size(); i++) m.put(alphabet.get(i), i); return m; } So, how to rewrite it using streams of Java 8? 回答1: Avoid stateful index counters like the

Throwing exception from lambda [duplicate]

亡梦爱人 提交于 2019-12-29 04:59:50
问题 This question already has answers here : Java 8 Lambda function that throws exception? (24 answers) Closed 4 years ago . Given this java 8 code public Server send(String message) { sessions.parallelStream() .map(Session::getBasicRemote) .forEach(basic -> { try { basic.sendText(message); } catch (IOException e) { e.printStackTrace(); } }); return this; } how do we properly make this IOException be delegated up the stack of the method call? (in nutshell how to make this method throw this

Throwing exception from lambda [duplicate]

╄→гoц情女王★ 提交于 2019-12-29 04:59:05
问题 This question already has answers here : Java 8 Lambda function that throws exception? (24 answers) Closed 4 years ago . Given this java 8 code public Server send(String message) { sessions.parallelStream() .map(Session::getBasicRemote) .forEach(basic -> { try { basic.sendText(message); } catch (IOException e) { e.printStackTrace(); } }); return this; } how do we properly make this IOException be delegated up the stack of the method call? (in nutshell how to make this method throw this

Stream Filter of 1 list based on another list

可紊 提交于 2019-12-29 04:29:12
问题 I am posting my query after having searched in this forum & google, but was unable to resolve the same. eg: Link1 Link2 Link3 I am trying to filter List 2 (multi column) based on the values in List 1. List1: - [Datsun] - [Volvo] - [BMW] - [Mercedes] List2: - [1-Jun-1995, Audi, 25.3, 500.4, 300] - [7-Apr-1996, BMW, 35.3, 250.2, 500] - [3-May-1996, Porsche, 45.3, 750.8, 200] - [2-Nov-1998, Volvo, 75.3, 150.2, 100] - [7-Dec-1999, BMW, 95.3, 850.2, 900] expected o/p: - [7-Apr-1996, BMW, 35.3, 250

Java Streams: group a List into a Map of Maps

假如想象 提交于 2019-12-29 03:57:26
问题 How could I do the following with Java Streams? Let's say I have the following classes: class Foo { Bar b; } class Bar { String id; String date; } I have a List<Foo> and I want to convert it to a Map <Foo.b.id, Map<Foo.b.date, Foo> . I.e: group first by the Foo.b.id and then by Foo.b.date . I'm struggling with the following 2-step approach, but the second one doesn't even compile: Map<String, List<Foo>> groupById = myList .stream() .collect( Collectors.groupingBy( foo -> foo.getBar().getId()

Take every nth element from a Java 8 stream

拥有回忆 提交于 2019-12-29 03:32:11
问题 Suppose I have a list like this : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Is it possible to use a Java 8 stream to take every second element from this list to obtain the following? [1, 3, 5, 7, 9] Or maybe even every third element? [1, 4, 7, 10] Basically, I'm looking for a function to take every nth element of a stream: List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); List<Integer> list2 = list.stream().takenth(3).collect(Collectors.toList()); System.out.println(list2); // => [1, 4