java-8

Remove all elements except one from JSONArray of JSONObjects

强颜欢笑 提交于 2020-05-29 10:15:02
问题 I have a JSONArray (org.json) like this: [ { "a": "a" }, { "b": "a" }, { "c": "a" }, { "d": "a" }, { "e": "a" }, { "f": "a" }, { "g": "a" } ] I would like to remove all the JSONObject s that do not have the key a . Is there a better way to do other than my naive approach? Iterator objects = jsonArray.iterator(); while (objects.hasNext()) { Object o = objects.next(); if (o instanceof JSONObject && !((JSONObject) o).has("a")) { objects.remove(); } } Expected output: [{"a": "a"}] 回答1: If you're

Remove all elements except one from JSONArray of JSONObjects

不羁岁月 提交于 2020-05-29 10:13:53
问题 I have a JSONArray (org.json) like this: [ { "a": "a" }, { "b": "a" }, { "c": "a" }, { "d": "a" }, { "e": "a" }, { "f": "a" }, { "g": "a" } ] I would like to remove all the JSONObject s that do not have the key a . Is there a better way to do other than my naive approach? Iterator objects = jsonArray.iterator(); while (objects.hasNext()) { Object o = objects.next(); if (o instanceof JSONObject && !((JSONObject) o).has("a")) { objects.remove(); } } Expected output: [{"a": "a"}] 回答1: If you're

Remove all elements except one from JSONArray of JSONObjects

两盒软妹~` 提交于 2020-05-29 10:13:50
问题 I have a JSONArray (org.json) like this: [ { "a": "a" }, { "b": "a" }, { "c": "a" }, { "d": "a" }, { "e": "a" }, { "f": "a" }, { "g": "a" } ] I would like to remove all the JSONObject s that do not have the key a . Is there a better way to do other than my naive approach? Iterator objects = jsonArray.iterator(); while (objects.hasNext()) { Object o = objects.next(); if (o instanceof JSONObject && !((JSONObject) o).has("a")) { objects.remove(); } } Expected output: [{"a": "a"}] 回答1: If you're

Grouping the custom object list by properties using Java 8

假装没事ソ 提交于 2020-05-29 05:59:17
问题 I am learning many new features of lambda and wondering how can I group by my custom object list based on certain properties as key? For example, I have list of object like this in json. [{ "account" : "checking", "source" : "BOA" }, { "account" : "checking", "source" : "TD" }, { "account" : "saving", "source" : "WS" } ] I am looking for way to group using java 8 feature to get output like this (grouping source as comma separated for same account. [{ "account" : "checking", "source" : "BOA,

RxJava-file and operator chaining

好久不见. 提交于 2020-05-27 06:21:09
问题 I'm trying to reactively tail a log file using RxJava-File: File file = new File(".\\server.log"); Observable<String> newLines = FileObservable.tailer() .file(file) .startPosition(file.length()) .sampleTimeMs(1000) .chunkSize(8192) .utf8() .tailText(); newLines.subscribe(System.out::println); and it works as expected. But as soon as I try to chain some more operators, I get problems. For instance, changing to newLines.filter(LogfileWatcher::error).subscribe(System.out::println); (where error(

RxJava-file and operator chaining

老子叫甜甜 提交于 2020-05-27 06:19:09
问题 I'm trying to reactively tail a log file using RxJava-File: File file = new File(".\\server.log"); Observable<String> newLines = FileObservable.tailer() .file(file) .startPosition(file.length()) .sampleTimeMs(1000) .chunkSize(8192) .utf8() .tailText(); newLines.subscribe(System.out::println); and it works as expected. But as soon as I try to chain some more operators, I get problems. For instance, changing to newLines.filter(LogfileWatcher::error).subscribe(System.out::println); (where error(

How to compare two java.time.Period in java 8?

半城伤御伤魂 提交于 2020-05-27 05:58:25
问题 How do I compare two Periods in java 8? E.g. Period one = Period.of(10,0,0); Period two = Period.of(8,0,0); here in this case one is greater than two. 回答1: It is true that the comparison of two Period objects does not make sense in a general case, due to the undefined standard length of a month. However, in many situations you can quite well live with an implementation similar to that which follows. The contract will be similar to the contract of compareTo() : public int

Java 8: merging two Lists containing objects by Id

帅比萌擦擦* 提交于 2020-05-27 04:15:11
问题 I have 2 Lists: // old list List<Employee> oldList = new ArrayList<>(); Employee emp1 = new Employee(); emp1.setPersonalNumber("123"); emp1.setName("old_name1"); emp1.setStatus(Status.OLD); Employee emp2 = new Employee(); emp2.setPersonalNumber("456"); emp2.setName("old_name2"); emp2.setStatus(Status.OLD); oldList.add(emp1); oldList.add(emp2); // new list List<Employee> newList = new ArrayList<>(); Employee newEmp1 = new Employee(); newEmp1.setPersonalNumber("123"); newEmp1.setName("new_name1

Java 8: merging two Lists containing objects by Id

守給你的承諾、 提交于 2020-05-27 04:13:26
问题 I have 2 Lists: // old list List<Employee> oldList = new ArrayList<>(); Employee emp1 = new Employee(); emp1.setPersonalNumber("123"); emp1.setName("old_name1"); emp1.setStatus(Status.OLD); Employee emp2 = new Employee(); emp2.setPersonalNumber("456"); emp2.setName("old_name2"); emp2.setStatus(Status.OLD); oldList.add(emp1); oldList.add(emp2); // new list List<Employee> newList = new ArrayList<>(); Employee newEmp1 = new Employee(); newEmp1.setPersonalNumber("123"); newEmp1.setName("new_name1

Load a package and inside classes

有些话、适合烂在心里 提交于 2020-05-26 09:32:10
问题 I'm creating a package with some classes that I generated with wsimport on the fly and now I'm trying to load it to use, how can I do this? natively? or with some lib like byte-buddy, I tried the bellow code to load each class in a package: File [] files = new File("<Path to package in filesystem with classes (*.class)>").listFiles(); List<URL> classUrls = new ArrayList<>(); for(File file : files) { classUrls.add(new URL("file://" + file.getAbsolutePath())); } URL[] classz = new URL[classUrls