java-stream

Java Stream - group by when key appears in a list

匆匆过客 提交于 2019-12-01 08:12:21
问题 I am trying to group by a collection by a value that appears in my object as a list. This is the model that I have public class Student { String stud_id; String stud_name; List<String> stud_location = new ArrayList<>(); public Student(String stud_id, String stud_name, String... stud_location) { this.stud_id = stud_id; this.stud_name = stud_name; this.stud_location.addAll(Arrays.asList(stud_location)); } } When I initialize it with the following : List<Student> studlist = new ArrayList<Student

Use parallelStream foreach to create HashMap, But sometimes value is empty

房东的猫 提交于 2019-12-01 08:07:45
问题 Java Code Like : List<Detail> DbDetails = ... Like 50000 rows records Map<Long, List<Detail>> details = new HashMap(); DbDetails .parallelStream().forEach(detail -> { Long id = detail.getId(); details.computeIfAbsent(id, v -> new ArrayList<>()).add(detail); }); Then ... details.entrySet().stream().forEach(e -> { e.getValue(); // Some value is empty }); I guess it because HashMap is thread-unsafe, so I use Hashtable instead of it. Then it run ok, all value has value, but I don't know why? 回答1:

convert map to list<string> - as “key-value” to each list entry

谁都会走 提交于 2019-12-01 07:54:31
问题 I want to convert Map<Integer, String> to List<String> with each map entry - to 1 entry in the list as "key - value" I searched and I only found to map values only to List. Map<Integer, String> map = new HashMap<>(); map.put(10, "apple"); map.put(20, "orange"); map.put(30, "banana"); map.put(40, "watermelon"); map.put(50, "dragonfruit"); I want this to be mapped to list as "10-apple" "20-orange" and so on. this can be done easily if I used foreach .. but I want to know if it is feasible to

Is collectingAndThen method enough efficient?

余生长醉 提交于 2019-12-01 07:51:35
问题 I have recently started using collectingAndThen and found that it is taking a bit long time comparatively to the other coding procedures, which i used for performing the similar tasks. Here is my code: System.out.println("CollectingAndThen"); Long t = System.currentTimeMillis(); String personWithMaxAge = persons.stream() .collect(Collectors.collectingAndThen( Collectors.maxBy(Comparator.comparing(Person::getAge)), (Optional<Person> p) -> p.isPresent() ? p.get().getName() : "none" )); System

How to set value of HashMap into a custom java object using java 8 stream?

两盒软妹~` 提交于 2019-12-01 07:37:56
问题 I have a HashMap which I want to convert to custom object Response . I am not sure how to set the values (80, 190, 900, 95) from HashMap to the custom object? How to write a separate function in Response object which sets price fields or set two parameters (key and value) to fromString function. class Converter{ public static void main(String[] args) { Map<String, Long> map = new HashMap<String, Long>(); map.put("111", 80) // first map.put("1A9-ppp", 190) // second map.put("98U-6765", 900) //

Can you collect(joining()) without mapping to string?

守給你的承諾、 提交于 2019-12-01 07:35:08
问题 I am reading java 8 in action, and the author says that if you have a class that overrides the toString method, you don't need to map the stream to Strings when doing collect(joining()). An example: public static void main(String... args) { List<Person> people = Arrays.asList( new Person(23, "Paul"), new Person(23, "John"), new Person(23, "Greg"), new Person(24, "Greg"), new Person(25, "Paul") ); // Person overrides toString String peopleString = people .stream() .collect(Collectors.joining()

Replace for loop with java 8 foreach for updating values

偶尔善良 提交于 2019-12-01 07:26:40
问题 I'm looking to replace the following for loop with an elegant java 8 stream or lambda solution. Is there anything concise and efficient? public static void main(String[] args) { ArrayList<Integer> myList = new ArrayList<>( Arrays.asList( 10,-3,5)); // add 1/2 of previous element to each element for(int i =1 ;i < myList.size(); ++i ) myList.set(i, myList.get(i)+myList.get(i-1)/2); // myList.skip(1).forEach( e -> e + prevE/2 ); // looking for something in this spirit } 回答1: Your loop evaluation

Chaining Consumers Java 8

本小妞迷上赌 提交于 2019-12-01 07:15:44
Hello I am having the following problem. Lets say that We have object "Account" This object Account is immutable, so overtime we execute an action upon it we are actually transforming it to another state. For example Account can become ClosedAccount or NewAccount and so on. Now in some occasions my transitions are reflective Account is transformed to Account but something very small is changed like for example "modifiedDate". Because my Account is its essence immutable even the reflective operations are still creating new Accounts with updated values. If my object is not immutable I would have

Collect complex objects in one lambda expression

烈酒焚心 提交于 2019-12-01 07:14:18
问题 I have a list of objects. At first, I need to sort it by type. Than by faceValue. In the end, summarize all quantities: class Coin{ String type; BigInteger faceValue; BigInteger quantity; ... } List<Coin> coins = new ArrayList<>(); coins.add(new Coin("USD", 1, 150)); coins.add(new Coin("USD", 1, 6)); coins.add(new Coin("USD", 1, 60)); coins.add(new Coin("USD", 2, 100)); coins.add(new Coin("USD", 2, 100)); coins.add(new Coin("CAD", 1, 111)); coins.add(new Coin("CAD", 1, 222)); Result list must

Why Stream operations is duplicated with Collectors?

£可爱£侵袭症+ 提交于 2019-12-01 06:47:36
问题 Please allow me to make some complaints, maybe it is boringly but I want to describe:" Why did this question will be raised? ". I have answered questions is different from others here, here and here last night. After I get dig into it, I found there are many duplicated logic between Stream and Collector that violates Don't repeat yourself principle, e.g: Stream#map & Collectors#mapping, Stream#filter & Collectors#filtering in jdk-9 and .etc. But it seems to reasonable since Stream abide by