java-stream

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

只谈情不闲聊 提交于 2019-12-01 12:03:57
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? HashMap is not thread-safe, so don't use parallel streams with it. Besides, why do that there, when streams

How to process chuncks of a file with java.util.stream

心已入冬 提交于 2019-12-01 11:29:49
To get familliar with the stream api, I tried to code a quite simple pattern. Problem: Having a text file containing not nested blocks of text. All blocks are identified by start/endpatterns (e.g. <start> and <stop> . The content of a block isn't syntactically distinguishable from the noise between the blocks. Therefore it is impossible, to work with simple (stateless) lambdas. I was just able to implement something ugly like: Files.lines(path).collect(new MySequentialParseAndProsessEachLineCollector<>()); To be honest, this is not what I want. Im looking for a mapper something like: Files

Compact stream expression for transposing double[][] Matrix

允我心安 提交于 2019-12-01 11:24:18
I want to tranpose a double[][] matrix with the most compact and efficient expression possible. Right now I have this: public static Function<double[][], double[][]> transpose() { return (m) -> { final int rows = m.length; final int columns = m[0].length; double[][] transpose = new double[columns][rows]; range(0, rows).forEach(r -> { range(0, columns).forEach(c -> { transpose[c][r] = m[r][c]; }); }); return transpose; }; } Thoughts? Tunaki You could have: public static UnaryOperator<double[][]> transpose() { return m -> { return range(0, m[0].length).mapToObj(r -> range(0, m.length)

Getting the max of a property using Java 8

ε祈祈猫儿з 提交于 2019-12-01 11:05:40
I want to rewrite the below code using the Stream library ( allPeople is a List<Person> ). int maxYear = Integer.MIN_VALUE; Person oldest = null; for (Person p : allPeople) { if (p.getDateOfDeath() > maxYear) { oldest = p; maxYear = p.getDateOfDeath(); } } I am trying to find the oldest person in a list of people (assuming there is no Age property on the Person object, it's just an example). How can I rewrite this using Java 8? Person oldest = allPeople.stream().max(comparingInt(Person::getDateOfDeath)).orElse(null); This code creates a Stream of Person and selects the max element when

Best way performance wise to use limit on stream in case of multithreading

女生的网名这么多〃 提交于 2019-12-01 11:00:06
问题 I watched a talk by José Paumard on InfoQ : http://www.infoq.com/fr/presentations/jdk8-lambdas-streams-collectors (French) The thing is I got stuck on this one point. To collect 1M Long using stream AND multithreading we can do it this way : Stream<Long> stream = Stream.generate(() -> ThreadLocalRandom.current().nextLong()) ; List<Long> list1 = stream.parallel().limit(10_000_000).collect(Collectors.toList()) ; But given the fact that the threads are always checking the said limit in hinders

Refactoring nested for loop into Java 8 stream

人走茶凉 提交于 2019-12-01 10:01:59
问题 I have the following for loop: List<Map> mapList = new ArrayList<>(); for (Resource resource : getResources()) { for (Method method : resource.getMethods()) { mapList.add(getMap(resource,method)); } } return mapList; How could I refactor this nested loop into a Java 8 stream? 回答1: You can use flatMap to obtain all the Map s for all Method s of all Resource s : List<Map> mapList = getResources().stream() .flatMap(r->r.getMethods().stream().map(m->getMap(r,m))) .collect(Collectors.toList()); 来源

how to convert forEach to lambda

风流意气都作罢 提交于 2019-12-01 09:49:57
Iterator<Rate> rateIt = rates.iterator(); int lastRateOBP = 0; while (rateIt.hasNext()) { Rate rate = rateIt.next(); int currentOBP = rate.getPersonCount(); if (currentOBP == lastRateOBP) { rateIt.remove(); continue; } lastRateOBP = currentOBP; } how can i use above code convert to lambda by stream of java 8? such as list.stream().filter().....but i need to operation list. If you really want distinct and sorted as you say in your comments, than it is as simple as : TreeSet<Rate> sorted = rates.stream() .collect(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Rate:

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

懵懂的女人 提交于 2019-12-01 09:39:22
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()); System.out.println(peopleString); } However, this doesn't work, and only this: String peopleString =

how to convert forEach to lambda

南楼画角 提交于 2019-12-01 09:38:31
问题 Iterator<Rate> rateIt = rates.iterator(); int lastRateOBP = 0; while (rateIt.hasNext()) { Rate rate = rateIt.next(); int currentOBP = rate.getPersonCount(); if (currentOBP == lastRateOBP) { rateIt.remove(); continue; } lastRateOBP = currentOBP; } how can i use above code convert to lambda by stream of java 8? such as list.stream().filter().....but i need to operation list. 回答1: If you really want distinct and sorted as you say in your comments, than it is as simple as : TreeSet<Rate> sorted =

Replace for loop with java 8 foreach for updating values

蓝咒 提交于 2019-12-01 09:36:00
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 } Your loop evaluation has a dependency to the result of the previous evaluation. It is equivalent to for(int i = 1, value =