是map()和filter()的Optional懒样Stream?
我如何确认它们的类型?
我正在对非常大的每个元素应用操作LinkedList<LinkedList<Double>>:
list.stream().map(l -> l.stream().filter(d ->
(Collections.max(l) - d) < 5)
.collect(Collectors.toCollection(LinkedList::new))).collect(Collectors.toCollection(LinkedList::new));
Run Code Online (Sandbox Code Playgroud)
在我的计算机(四核)上,并行流似乎比使用顺序流更快:
list.parallelStream().map(l -> l.parallelStream().filter(d ->
(Collections.max(l) - d) < 5)
.collect(Collectors.toCollection(LinkedList::new))).collect(Collectors.toCollection(LinkedList::new));
Run Code Online (Sandbox Code Playgroud)
然而,并不是每台计算机都是多核的。我的问题是,在单处理器计算机上使用并行流会比使用顺序流明显慢吗?
我有Map以下结构,我想翻转键和值。
Map<String, List<String>> dataMap
Run Code Online (Sandbox Code Playgroud)
样本数据 :
acct01: [aa, ab, ad],
acct02: [ac, ad]
acct03: [ax, ab]
Run Code Online (Sandbox Code Playgroud)
想要将这些数据转换为,
aa: [acct01],
ab: [acct01, acct03],
ac: [acct02],
ad: [acct01, acct02],
ax: [acct03]
Run Code Online (Sandbox Code Playgroud)
想知道有没有java 8-stream的方式来转换Map。
我当前的实现(没有流)
Map<String, List<String>> originalData = new HashMap<String, List<String>>();
originalData.put("Acct01", Arrays.asList("aa", "ab", "ad"));
originalData.put("Acct02", Arrays.asList("ac", "ad"));
originalData.put("Acct03", Arrays.asList("ax", "ab"));
System.out.println(originalData);
Map<String, List<String>> newData = new HashMap<String, List<String>>();
originalData.entrySet().forEach(entry -> {
entry.getValue().forEach(v -> {
if(newData.get(v) == null) {
List<String> t = new ArrayList<String>();
t.add(entry.getKey());
newData.put(v, t);
} else { …Run Code Online (Sandbox Code Playgroud) 我有一个流,我需要在这个流的索引处获取元素
list.get(index)
我有一个包含多个条目的 LinkedHashMap。我想在第一步中将多个条目减少为单个条目,然后将其映射到单个字符串。例如:我从这样的地图开始:
{"<a>"="</a>", "<b>"="</b>", "<c>"="</c>", "<d>"="</d>"}
Run Code Online (Sandbox Code Playgroud)
最后我想得到一个这样的字符串:
<a><b><c><d></d></c></b></a>
Run Code Online (Sandbox Code Playgroud)
(在这种情况下,字符串按顺序包含键,而不是按相反顺序包含值。但这并不重要,我想要一个通用的解决方案)
我想我需要map.entrySet().stream().reduce(),但是我不知道在reduce方法中写什么,以及如何继续。
不久前,我发现了以下有关使用 Java 8 初始化地图的更简洁方法的信息:http : //minborgsjavapot.blogspot.com/2014/12/java-8-initializing-maps-in-smartest-way.html。
使用这些指南,我在一个应用程序中实现了以下类:
public class MapUtils {
public static <K, V> Map.Entry<K, V> entry(K key, V value) {
return new AbstractMap.SimpleEntry<>(key, value);
}
public static <K, U> Collector<Map.Entry<K, U>, ?, Map<K, U>> entriesToMap() {
return Collectors.toMap((e) -> e.getKey(), (e) -> e.getValue());
}
public static <K, U> Collector<Map.Entry<K, U>, ?, ConcurrentMap<K, U>> entriesToConcurrentMap() {
return Collectors.toConcurrentMap((e) -> e.getKey(), (e) -> e.getValue());
}
}
Run Code Online (Sandbox Code Playgroud)
在那个应用程序中,我实现了这样的代码:
public Map<String, ServiceConfig> serviceConfigs() {
return Collections.unmodifiableMap(Stream.of(
entry("ActivateSubscriber", new ServiceConfig().yellowThreshold(90).redThreshold(80)),
entry("AddAccount", …Run Code Online (Sandbox Code Playgroud) 我正在Stream对一组Integers运行操作。
然后我创建了一个Cell对象来进行一些操作并应用一个过滤器,
我想创建一个只有有效s的Integerto的映射。CellCell
沿着这条线的东西:
List<Integer> type = new ArrayList<>();
Map<Integer, Cell> map =
list.stream()
.map(x -> new Cell(x+1, x+2)) // Some kind of Manipulations
.filter(cell->isNewCellValid(cell)) // filter some
.collect(Collectors.toMap(....));
Run Code Online (Sandbox Code Playgroud)
是否可以沿流操作使用原始流对象?
我正在尝试将下面的嵌套循环转换为流 Java 8。
newself2中的每个元素都是一个字符串列表——["1 2","3 4"]需要改为["1","2","3","4"]。
for (List<String> list : newself2) {
// cartesian = [["1 2","3 4"],["4 5","6 8"]...] list = ["1 2","3 4"]...
List<String> clearner = new ArrayList<String>();
for (String string : list) { //string = "1 3 4 5"
for (String stringElement : string.split(" ")) {
clearner.add(stringElement);
}
}
newself.add(clearner);
//[["1","2","3","4"],["4","5","6","8"]...]
}
Run Code Online (Sandbox Code Playgroud)
到目前为止我所尝试的 -
newself2.streams().forEach(list -> list.foreach(y -> y.split(" ")))
Run Code Online (Sandbox Code Playgroud)
现在我确定如何将内部 for 循环中的拆分数组添加到新列表 for x?
任何帮助是极大的赞赏。
我有一个Stream<Set<Integer>> intSetStream.
我可以这样做...
Set<Integer> theSetWithTheMax = intSetStream.max( (x,y)->{ return Integer.compare( x.size(), y.size() ); } ).get( );
Run Code Online (Sandbox Code Playgroud)
...我掌握了其中元素Set<Integer>数量最多的Integer那个。
那太棒了。但我真正需要知道的是,这是第一次Set在Stream这最大?或者是10日Set在Stream?还是i第Set?其中哪一个元素最多?
所以我的问题是:有没有一些方法-使用Stream API -我可以确定“这是i个Set在Stream中Set传回他们所有的最大价值,为氏Set.size( )呼叫”?
我能想到的最佳解决方案是迭代Stream<Set<Integer>>(使用intSetStream.iterator())并进行手动max( )计算。但我希望学习更多Stream-y 的方法;如果有这样的事情。
我有以下流代码:
List<Data> results = items.stream()
.map(item -> requestDataForItem(item))
.filter(data -> data.isValid())
.collect(Collectors.toList());
Data requestDataForItem(Item item) {
// call another service here
}
Run Code Online (Sandbox Code Playgroud)
问题是我只想requestDataForItem在流中的所有元素都有效时调用
。例如,如果第一项无效,我不会调用流中的任何元素。
有.allMatch流API中,但它返回一个boolean。我想要做的一样.allMatch不是
.collect当一切都匹配的结果。另外,我只想处理一次流,有两个循环很容易。
Java Streams API 可以做到这一点吗?