java-stream

Verify JMH measurements of simple for/lambda comparisons

早过忘川 提交于 2019-12-12 09:41:15
问题 I wanted to do some performance measurements and comparisons of simple for loops and equivalent streams implementations. I believe it's the case that streams will be somewhat slower than equivalent non-streams code, but I wanted to be sure I'm measuring the right things. I'm including my entire jmh class here. import java.util.ArrayList; import java.util.List; import org.openjdk.jmh.annotations.Benchmark; import org.openjdk.jmh.annotations.Scope; import org.openjdk.jmh.annotations.Setup;

Determine if a list composed of anagram elements in Java 8

偶尔善良 提交于 2019-12-12 09:34:25
问题 I want to determine if a list is anagram or not using Java 8. Example input: "cat", "cta", "act", "atc", "tac", "tca" I have written the following function that does the job but I am wondering if there is a better and elegant way to do this. boolean isAnagram(String[] list) { long count = Stream.of(list) .map(String::toCharArray) .map(arr -> { Arrays.sort(arr); return arr; }) .map(String::valueOf) .distinct() .count(); return count == 1; } It seems I can't sort char array with Stream.sorted()

Java stream operation order of execution at terminal point [duplicate]

浪子不回头ぞ 提交于 2019-12-12 08:51:16
问题 This question already has answers here : Stream intermediate operations ordering (2 answers) Closed last year . I have been trying to find clear contract from official Java documentation as to in which order Java streams, once a terminal operation is called, process the elements and call intermediate operations. For example lets look at these examples that use both Java stream version and plain iteration version (both producing same outcome) . Example1: List<Integer> ints = Arrays.asList(1, 2

Builder pattern with a Java 8 Stream

≯℡__Kan透↙ 提交于 2019-12-12 07:55:31
问题 I am building an object with a simple loop: WebTarget target = getClient().target(u); for (Entry<String, String> queryParam : queryParams.entrySet()) { target = target.queryParam(queryParam.getKey(), queryParam.getValue()); } I want to do the same thing using the Java8 Stream API but I cannot figure out how to do it. What makes me struggle is that target is reassigned every time, so a simple .forEach() will not work. I guess I need to use a .collect() or reduce() since I am looking for a

Why doesn't Stream.limit work as expected in this snippet?

时间秒杀一切 提交于 2019-12-12 07:25:25
问题 List<Integer> integer = Stream.generate(new Supplier<Integer>() { int i = 0 ; @Override public Integer get() { return ++i; } }).filter(j -> j < 5) .limit(10) // Note the call to limit here .collect(Collectors.toList()); Counter to my expectation, the collect call never returns. Setting limit before filter produces the expected result. Why? 回答1: Since there are only 4 elements that pass the filter, limit(10) never reaches 10 elements, so the Stream pipeline keeps generating new elements and

How to pass argument to class constructor when initialzed thru ::new in Java8

帅比萌擦擦* 提交于 2019-12-12 07:19:20
问题 I am using java 8 stream API to perform action on a list of Store objects. Store takes a String argument and a Mapper object. Mapper will be same for all Store object. Question: How can I pass Mapper object when I initialize Store here .map(Store::new) ? public class Store { public Store(String name, Mapper mapper) { } } public class Mapper { } public class Test { public static void main(String[] args) { List<String> names = new ArrayList<String>(); Mapper mapper = new Mapper(); // compile

How to get multiple values from an object using a single stream operation?

时光总嘲笑我的痴心妄想 提交于 2019-12-12 07:14:56
问题 I want to determine the minimum area required to display a collection of points. The easy way is to loop through the collection like this: int minX = Integer.MAX_VALUE; int maxX = Integer.MIN_VALUE; int minY = Integer.MAX_VALUE; int maxY = Integer.MIN_VALUE; for (Point point: points) { if (point.x < minX) { minX = point.x; } if (point.x > maxX) { maxX = point.x; } if (point.y < minY) { minY = point.y; } if (point.y > maxY) { maxY = point.y; } } I am getting to know streams. To do the same,

Using JPA objects in parallel streams with Spring

梦想的初衷 提交于 2019-12-12 05:16:34
问题 I'm working on spring-boot application with JPA. In the code I found a really suspicious part which does something like (simplified example, the code otherwise contains a lot of clutter): entityRepository.findAll().parallel() // The findAll returns already a stream, it's a CrudRepository from Spring .filter (...) .map(e -> { OtherDbObject other = service.getOtherDbObjectBasedOn(e); boolean hasSomething = other.getProperties().stream() //properties is fetch type EAGER @OneToMany collection in

Why can I call the stream() method on objects of a class that don't have the stream()-method?

扶醉桌前 提交于 2019-12-12 01:26:47
问题 I'm a novice Java programmer at university. I discovered something today that broke one of my conceptions about how Java syntax works. public class testClass { ArrayList <String> persons = new ArrayList <String> (); public void run(){ Stream <String> personstream = persons.stream(); }} The method stream() is not found in the ArrayList class, yet it might appear as if it's there. When I move my mouse over the stream() -method in Eclipse, it says it's part of Collections, but I don't find the

How to print all the button texts within the url using Selenium through Java

安稳与你 提交于 2019-12-11 23:09:15
问题 Steps: Go to the site > https://www.toolsqa.com/automation-practice-switch-windows/ Get a list of buttons from that page Print the name of the buttons that are displayed on the page. Code trial: package com.practice; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class Buttons { public static void main(String[] args) throws InterruptedException { System