我有一个单声道A.对象A包含两个列表.我想直接创建两个Flux.这可能没有block()吗?
Mono<A> a = ... ;
Flux<AList1> a1 = Flux.fromIterable(a.block().getList1());
Run Code Online (Sandbox Code Playgroud) 我想在某个操作上提高后端 REST API 的性能,该操作按顺序轮询多个不同的外部 API 并收集它们的响应并将它们全部扁平化为一个响应列表。
最近刚刚了解了CompletableFutures,我决定试一试,并将该解决方案与仅将 my 更改stream为 a 的解决方案进行比较parallelStream。
这是用于基准测试的代码:
package com.alithya.platon;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class ConcurrentTest {
static final List<String> REST_APIS =
Arrays.asList("api1", "api2", "api3", "api4", "api5", "api6", "api7", "api8");
MyTestUtil myTest = new MyTestUtil();
long millisBefore; // used to benchmark
@BeforeEach
void setUp() {
millisBefore = System.currentTimeMillis();
}
@AfterEach
void tearDown() {
System.out.printf("time taken …Run Code Online (Sandbox Code Playgroud) 我有一个像这样的颜色列表:
粉色、蓝色、红色、蓝色、灰色、绿色、紫色、黑色...等
List<String> listOfColors = Arrays.asList("Pink", "Blue", "Red", "blue", "Grey", "green", "purple", "black");
Run Code Online (Sandbox Code Playgroud)
有一些中间操作,比如过滤一些水果颜色,现在我留下了过滤结果,我希望它们按顺序排序:
蓝、黑、蓝、灰、绿、粉、紫、红
我试过了 :
List<String> listOfColors = Arrays.asList("Pink", "Blue", "Red", "blue", "Grey", "green", "purple", "black");
Run Code Online (Sandbox Code Playgroud)
它没有按预期工作。
输出如下:
黑、蓝、蓝、绿、灰、粉、紫、红
我想要以下内容:
蓝、黑、蓝、灰、绿、粉、紫、红
对于我的具体情况,我想减少使用功能组合;例如:
BiFunction<ImmutableSet<Integer>, ImmutableSet<Integer>, Sets.SetView<Integer>> f = Sets::intersection;
Function<Sets.SetView<Integer>, ImmutableSet<Integer>> g = Sets.SetView::immutableCopy;
BiFunction<ImmutableSet<Integer>, ImmutableSet<Integer>, ImmutableSet<Integer>> biFunction = f.andThen(g);
ImmutableSet<Integer> intersection = Stream.of(ImmutableSet.of(1, 2, 3), ImmutableSet.of(1, 2), ImmutableSet.of(4))
.reduce(biFunction)
.orElse(ImmutableSet.of());
Run Code Online (Sandbox Code Playgroud)
这有一个编译错误:
BiFunction<ImmutableSet<Integer>, ImmutableSet<Integer>, Sets.SetView<Integer>> f = Sets::intersection;
Function<Sets.SetView<Integer>, ImmutableSet<Integer>> g = Sets.SetView::immutableCopy;
BiFunction<ImmutableSet<Integer>, ImmutableSet<Integer>, ImmutableSet<Integer>> biFunction = f.andThen(g);
ImmutableSet<Integer> intersection = Stream.of(ImmutableSet.of(1, 2, 3), ImmutableSet.of(1, 2), ImmutableSet.of(4))
.reduce(biFunction)
.orElse(ImmutableSet.of());
Run Code Online (Sandbox Code Playgroud)
相反,我需要这样做:
ImmutableSet<Integer> intersection = Stream.of(ImmutableSet.of(1, 2, 3), ImmutableSet.of(1, 2), ImmutableSet.of(4))
.reduce((a, b) -> Sets.intersection(a, b).immutableCopy())
.orElse(ImmutableSet.of());
Run Code Online (Sandbox Code Playgroud)
然而,这失去了组合提供的无点风格。
为什么 Stream API …
我发现了一个关于 peek 方法的 Java 8 Stream API 的测验,如下所示
Arrays.asList("Fred", "Jim", "Sheila")
.stream()
.peek(System.out::println)
.allMatch(s -> s.startsWith("F"));
Run Code Online (Sandbox Code Playgroud)
输出是
Fred
Jim
Run Code Online (Sandbox Code Playgroud)
我很困惑这个流是如何工作的?我的预期结果应该是
Fred
Jim
Sheila
Run Code Online (Sandbox Code Playgroud)
peek() 方法是一个中间操作,它处理 Stream 中的每个元素。谁能给我解释一下。
考虑以下用于hibernate 的JAVA 模型:
@Entity
@Table
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long id;
@Column
public String firstName;
@Column
public String lastName;
@Column
public Boolean active;
}
Run Code Online (Sandbox Code Playgroud)
以及以下 API 序列化模型(使用spring boot rest 控制器):
public class PersonVO {
public Long id;
public String fullName;
}
Run Code Online (Sandbox Code Playgroud)
我想要的是:
在C# .NET 中,我可以像这样:
IQueryable<Person> personsQuery = entityFrameworkDbContext.Persons;
// FIRST POINT - Here i could make some predefined filtering like 'only active', …Run Code Online (Sandbox Code Playgroud) 我想转换:
Map<String, Map<String, List<Map<String, String>>>> inputMap
Run Code Online (Sandbox Code Playgroud)
到:
Map<String, Map<String, CustomObject>> customMap
Run Code Online (Sandbox Code Playgroud)
inputMap在配置中提供并准备就绪,但我需要customMap格式化。CustomObject 将通过List<Map<String, String>>在函数中使用几行代码来派生。
我已经尝试了迭代输入映射和复制 customMap 中的键值的正常方法。有没有使用 Java 8 或其他一些快捷方式来做到这一点的有效方法?
Map<String, Map<String, List<Map<String, String>>>> configuredMap = new HashMap<>();
Map<String, Map<String, CustomObj>> finalMap = new HashMap<>();
for (Map.Entry<String, Map<String, List<Map<String, String>>>> attributeEntry : configuredMap.entrySet()) {
Map<String, CustomObj> innerMap = new HashMap<>();
for (Map.Entry<String, List<Map<String, String>>> valueEntry : attributeEntry.getValue().entrySet()) {
innerMap.put(valueEntry.getKey(), getCustomeObj(valueEntry.getValue()));
}
finalMap.put(attributeEntry.getKey(), innerMap);
}
private CustomObj getCustomeObj(List<Map<String, String>> list) {
return new CustomObj();
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Java StreamsString从我的列表中收集最大长度的所有s:
List<String> strings = Arrays.asList("long word", "short", "long wwww", "llll wwww", "shr");
List<String> longest = strings.stream()
.sorted(Comparator.comparingInt(String::length).reversed())
.takeWhile(???)
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
我希望 mylongest包含{"long word", "long wwww", "llll wwww"},因为那些是String具有最大长度的s。如果只有String最大长度的s之一,我显然希望结果List只包含该元素。
我试图首先对它们进行排序,以便在第一个元素中出现最大长度,但我无法检索流中第一个元素的长度。我可以尝试类似的东西peek():
static class IntWrapper {
int value;
}
public static void main(String[] args) throws IOException {
List<String> strings = Arrays.asList("long word", "short", "long wwww", "llll wwww", "shr");
IntWrapper wrapper = new IntWrapper();
List<String> longest = strings.stream()
.sorted(Comparator.comparingInt(String::length).reversed())
.peek(s …Run Code Online (Sandbox Code Playgroud) parallel()然而,今天我使用的是在地图之后执行操作的流;底层源是一个非线程安全的迭代器,类似于BufferedReader.lines实现。
我原本以为 trySplit 会在创建的线程上调用,但是;我观察到对迭代器的访问来自多个线程。
例如,以下愚蠢的迭代器实现只是设置了足够的元素来导致分裂,并且还跟踪访问该hasNext方法的唯一线程。
class SillyIterator implements Iterator<String> {
private final ArrayDeque<String> src =
IntStream.range(1, 10000)
.mapToObj(Integer::toString)
.collect(toCollection(ArrayDeque::new));
private Map<String, String> ts = new ConcurrentHashMap<>();
public Set<String> threads() { return ts.keySet(); }
private String nextRecord = null;
@Override
public boolean hasNext() {
var n = Thread.currentThread().getName();
ts.put(n, n);
if (nextRecord != null) {
return true;
} else {
nextRecord = src.poll();
return nextRecord != null;
}
}
@Override
public String next() {
if (nextRecord …Run Code Online (Sandbox Code Playgroud) 单元测试的问题是同一集合在流和 for 循环中的处理方式不同。两种情况下的集合都是空的(data.size() = 0),但在情况 1 中,该集合以某种方式进行了处理,换句话说,它将进入 for 循环。在情况 2 中,该集合只是被跳过(这是预期的,因为它是空的)。
测试使用 Mockito,并将Result<Record>用于 JOOQ。
测试是旧的并且没有变化,唯一的变化是从 for 循环到流。
情况1
private SearchResult iterateData(
Result<Record> data, ...) {
for (Record record : data) {
doSomething(record);
}
Run Code Online (Sandbox Code Playgroud)
案例2
private SearchResult iterateData(
Result<Record> data, ...) {
data.stream().forEach(record -> doSomething(record));
Run Code Online (Sandbox Code Playgroud)
案例 1 for 循环示例的屏幕截图
模拟结果对象
private DefaultSearchRequestModel rowSpecificValuesTestSetup(
parameters...) {
DefaultSearchRequestModel searchRequest = new DefaultSearchRequestModel(
Arrays.asList(....),
Collections.singletonList(
new SearchFilter(
"test",
Collections.singletonList(...)));
List<Column> columns =
this.searchService.filterUserAllowedColumns(...);
Condition searchCondition =
this.searchRepositoryMock.getSearchConditions(...);
List<TableJoinMapping> joinMappings = …Run Code Online (Sandbox Code Playgroud) java ×10
java-stream ×10
java-8 ×4
benchmarking ×1
collections ×1
hibernate ×1
jooq ×1
mockito ×1
projection ×1
querydsl ×1
reducing ×1
sorting ×1