使用 lambdaj 选择列表的前 3 个项目
我想选择 java 列表的前 3 项。
我知道如何对列表进行排序(请参阅代码),但我不知道如何选择该列表中具有最高值的前 3 个条目([3.0, 5.5, 7.2]) 。
我想我可以使用 subList(),但我想知道是否有办法在 lambdaj 中做到这一点。
import java.util.Arrays;
import java.util.List;
import static ch.lambdaj.Lambda.*;
public class Example {
public static void main(String[] args)
{
List<Double> list = Arrays.asList(5.5,1.5,7.2,3.0,0.5);
System.out.println(list);
List<Double> sortedList = sort(list,on(Double.class).doubleValue());
System.out.println(sortedList);
}
}
I want to select the top 3 items of a java List.
I know how to sort the List (please see the code), but I don't know how to select the top 3 entries of this list having the highest values ([3.0, 5.5, 7.2]).
I guess I could use subList(), but I want to know if there is a way to do this in lambdaj.
import java.util.Arrays;
import java.util.List;
import static ch.lambdaj.Lambda.*;
public class Example {
public static void main(String[] args)
{
List<Double> list = Arrays.asList(5.5,1.5,7.2,3.0,0.5);
System.out.println(list);
List<Double> sortedList = sort(list,on(Double.class).doubleValue());
System.out.println(sortedList);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么使用 lambdaj ?这是一个足够简单的任务,可以这样完成:
lambdaj
在这里没有实际用途。由于您只要求获取列表的一部分,而不以任何方式修改它,因此使用List
内置方法是最好的解决方案。如果您确实想要使用
lambdaj
,LambdaList
有一个subList()
方法,但那是与 Java SE 的List
接口中的完全相同。Why use
lambdaj
? This is a simple enough task that can be done like this:lambdaj
has no real use here. Since you are only asking to get parts of the list, and not to modify it in any way, usingList
s built in method is the best solution.If you really want to use
lambdaj
,LambdaList
has asubList()
method, but that is exactly the same as the one in Java SE'sList
interface.