我有一个函数 ( findByNames) 接受传播参数,如下例所示:
List<Users> findByNames(String... names)
{
...
}
Run Code Online (Sandbox Code Playgroud)
作为参数,我有一个列表:
List<String> names = asList("john","abraham");
Run Code Online (Sandbox Code Playgroud)
所以我想将names列表转换为传播对象以使用findByNames函数,这可以使用 Java 8 吗?我试过这个解决方案:
MapUtils.getMap(names.toArray(new String[names.size()]))
Run Code Online (Sandbox Code Playgroud)
但它不起作用!
谢谢你的时间。
我有一些 DTO 的链接哈希列表,其中包含 name (字符串)和 isActive (布尔)属性。我需要从头到尾迭代所有成员,并检查 isActive 中是否有任何成员具有错误值。当我找到这个元素时,我需要切断这个元素和列表的其余部分,并仅返回该元素之前的元素。
我想使用java流来解决这个问题。我尝试通过谓词使用流和过滤元素来检查元素是否处于活动状态,但这与我想要的逻辑不匹配。正如我所说,我需要找到第一个处于活动状态的元素,并将其及其后的所有内容从列表中删除。
如果我们在 LinkedHashSet<> 中有这个 ElementDto 列表
0 - name:test, isActive:true
1 - name:test2, isActive:true
2 - name:inActive, isActive:false
3 - name:activeAfterInactive, isActive:true
Run Code Online (Sandbox Code Playgroud)
预期输出将是包含元素 0,1 的列表
我只是想知道,内部迭代与外部迭代的真正好处是什么,以及为什么最好使用内部操作(至少我听到的是这样)。是否也可以在内部迭代集合时删除集合的元素?就像在代码示例中一样:
我知道内部迭代的代码可读性更好,但是还有其他一些好处,比如性能改进吗?
//List with Strings of Fruit-Names
Iterator i = aList.iterator();
String str = "";
while (i.hasNext()) {
str = (String) i.next();
if (str.equals("Orange")) {
i.remove();
System.out.println("\nThe element Orange is removed");
break;
}
}
Run Code Online (Sandbox Code Playgroud) 我试图以ArrayList<Person>相反的顺序对此进行排序,但这无法编译
List<Person> newList = arrayList.stream()
.sorted(Comparator.reverseOrder(Person::getAge)) //Error
.limit(3)
.collect(Collectors.toList());
newList.forEach(System.out::println);
Run Code Online (Sandbox Code Playgroud)
还有其他方法可以对流进行反向排序吗?
我已经列出了成对的。我想先根据键对它们进行排序,如果键相同,则根据值进行排序。
我尝试了以下代码,但引发了不兼容类型的异常:无法推断类型变量T
List<Pair<Integer,Integer>> list = new ArrayList<>();
list.sort(Comparator.comparingInt(Pair::getKey).thenComparingInt(Pair::getValue);
Run Code Online (Sandbox Code Playgroud)
错误:
不兼容的类型:无法推断类型变量T (参数不匹配;类对中的无效方法引用方法getKey无法应用于所需的给定类型:未找到参数:对象原因:实际和正式参数列表的长度不同)
其中T,K,V是类型变量:T扩展在方法compareInt(ToIntFunction)中声明的对象K扩展在类Pair中声明的对象V扩展在类Pair中声明的对象
How can I use Java 8 streams to calculate an average of the stream elements? Because I cannot use a stream twice I have to do it in one iteration. The stream contains custom beans holding a float value. The number of elements in the stream may vary. Hence, I want to sum up the float value of all elements in the stream and divide it by the number of elements in the stream.
Update: Finally I need the sum …
是否可以使用 Streams 过滤器访问列表的索引?我的意思是...我知道可以使用循环(while、for 等)来做到这一点,但就好像列表在变成流时会丢失有关索引的信息。
我想是这样的:
list.stream().filter(x -> x > 100)
.fiter(--getIndexOfEachValue--)
.collect(Collectors.toList())
Run Code Online (Sandbox Code Playgroud)
如果我有 List=[10,200,30,150,400,30]...
我期望结果=[1,3,4]
这UserMst是对象,users也是我从中获取 LoginId && TenantId 的列表。
LinkedHashMap<String, String> Tmap = users.stream().collect(Collectors.toMap(
UserMst::getLoginId, UserMst::getTenantId,
(x, y)-> ((x=="1") ? "Rocks" : (x=="2")
? "Mocks" : (x=="3")
? "Docs" : (x=="4")
? "Pocks" : "")
+ " , " + ( (y=="1") ? "Rocks": (y=="2")
? "Mocks": (y=="3")
? "Docs" : (y=="4")
? "Pocks": ""),
LinkedHashMap::new));
///////////////////
LinkedHashMap<String, String> Tmap =
users.stream().collect(
Collectors.toMap( UserMst::getLoginId, UserMst::getTenantId,
(x, y)-> x + ", " + y, LinkedHashMap::new));
```
It gives output as answer:: …Run Code Online (Sandbox Code Playgroud) 我有这个数组:
Integer[] originalItems = itemsArray.stream()
.distinct()
.sorted()
.toArray(Integer[]::new);
Run Code Online (Sandbox Code Playgroud)
我想返回它 asint[]而不是 as Integer[]。
我试图打电话,.toArray(int[]::new)但我收到此错误:
不存在类型变量 A 的实例,因此 int[] 符合 A[]
有人告诉我 Java Stream 是处理大量数据的不错选择,我最近做了一个比较测试。然而测试结果却出人意料:
问题来自CodeWar:
假设有一个最初有100人的地铁,每一站都有几个人上车,也有几个人下车。目标是统计大量停靠站后留在地铁中的人数(100000)。
这是我的代码:
import java.util.ArrayList;
public class Metro1 {
private final static int STOPS = 100000;
private static ArrayList<int[]> metro = new ArrayList<int[]>();
public static int sum1() {
int sum = 0;
for(int[] x: metro) {
sum +=x[0] - x[1];
}
return sum;
}
public static int sum2() {
return metro.stream()
.mapToInt(x -> x[0]-x[1])
.sum();
}
public static void main(String[] args) {
long start=0;
long end = 0;
metro.add(new int[] {100,0});
for(int i=1;i<STOPS;i++) {
int in …Run Code Online (Sandbox Code Playgroud)