我需要在lambda表达式中使用Predicate作为参数.我尝试了一个示例代码,但看到编译器错误.我看到编译器对不同的参数采用不同的谓词.所以Predicate参数n -> true and n -> false有效,但n -> n%4 == 0不起作用.
编译器错误是:
The operator % is undefined for the argument type(s) Object, int
Run Code Online (Sandbox Code Playgroud)
我修好了(请参阅下面的替换代码),但我问我是否应该修复它,为什么?我不确定我是否遗漏了一些基本的东西.
这是完整的代码:
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
public class PredicateAsArgumentInLambdaExpression {
public static int add(List<Integer> numList, Predicate predicate) {
int sum = 0;
for (int number : numList) {
if (predicate.test(number)) {
sum += number;
}
}
return sum;
}
public static void main(String args[]){
List<Integer> numList = new ArrayList<Integer>(); …Run Code Online (Sandbox Code Playgroud) 请帮助我了解如何使用以下方法的方法参考替换lambda.
public List<Person> sortByStartDate_ASC(LinkedHashSet<Person> personList) {
List<Person> pList = new ArrayList<Person>(personList);
Collections.sort(pList, (Person person1, Person person2) -> person1
.getStartDate().compareTo(person2.getStartDate()));
return pList;
}
Run Code Online (Sandbox Code Playgroud) 我最近听说自Java 8以来可以定义一个this在实例方法中调用的显式参数,如下所示:
public class Test
{
public void test(Test this, int i) { System.out.println(i); }
}
Run Code Online (Sandbox Code Playgroud)
这种语法有什么用?
正如您在此截图(Eclipse,编译器兼容性Java 8)中可以清楚地看到的,这是有效的语法.

我试图遍历两个列表,过滤嵌套列表并使用java8功能将结果写回主对象.
locations.forEach(location -> location.getSubList().stream()
.filter(this::correctTestDataValue)
.collect(Collectors.toList()));
Run Code Online (Sandbox Code Playgroud)
所以到目前为止,位置内的子列表没有变化,这很明显,因为stream和collect会创建一个新的列表,而不会将其写回到位置对象中.所以我的问题是,如果有办法调用location对象的setSubList(...)方法并将新列表写入其中.
谢谢
我正在尝试过滤a Map<Long, Person> people并仅返回状态为SUBSCRIBEDa 的这些人的ID List<Long>.以下是老式的代码:
public List<Long> getSubscribedPeople() {
final List<Long> subscribedPeople = new ArrayList<>();
for (final Map.Entry<Long, Person> entry : subscribedPeople.entrySet()) {
if (entry.getValue().getStatus() == PersonStatus.SUBSCRIBED) {
subscribedPeople.add(entry.getKey());
}
}
return subscribedPeople;
}
Run Code Online (Sandbox Code Playgroud)
Person类看起来如下:
class Person {
private Long id;
private PersonStatus status;
// getters and setters
}
Run Code Online (Sandbox Code Playgroud)
我试过以下,但这只给了我一个List<Entry<Long, Person>>:
public List<Long> getSubscribedPeople() {
return people.entrySet()
.stream()
.filter(e -> e.getValue().getStatus() == PersonStatus.SUBSCRIBED)
.collect(Collectors.toList());
}
Run Code Online (Sandbox Code Playgroud)
我不得不以map某种方式到流?
它是如何工作的?怎么可以Consumer<? super Integer>演员IntConsumer?
default boolean tryAdvance(Consumer<? super Integer> action) {
if (action instanceof IntConsumer) {
return tryAdvance((IntConsumer) action);
}
else {
if (Tripwire.ENABLED)
Tripwire.trip(getClass(),
"{0} calling Spliterator.OfInt.tryAdvance((IntConsumer) action::accept)");
return tryAdvance((IntConsumer) action::accept);
}
}
Run Code Online (Sandbox Code Playgroud) 尝试设置sym链接,但都没有工作.它指向一个只读文件系统.
bash-4.1$ sudo ln -s /scratch/mbhamba/work/jdk1.8.0_40/bin/java /usr/bin/java
bash-4.1$ which java
/usr/dev_infra/platform/bin/java
bash-4.1$ sudo rm -f /usr/dev_infra/platform/bin/java
rm: cannot remove /usr/dev_infra/platform/bin/java': Read-only file system
bash-4.1$ sudo ln -sf /scratch/mbhamba/work/jdk1.8.0_40/bin/java /usr/dev_infra/platform/bin/java
ln: cannot remove /usr/dev_infra/platform/bin/java': Read-only file system
说我有以下简单的数据结构.
public class Player {
private Long id;
private String name;
private Integer scores;
//getter setter
}
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好.现在的问题是,我如何获得球员排名?我有另一种排名数据结构,即 -
public class Ranking {
private Integer score;
private Integer rank;
//getter/setter
}
Run Code Online (Sandbox Code Playgroud)
所以我有一个播放器列表,我想计算一个排名列表,我想使用java8流API.
我有一个名为PlayerService如下的服务
public class PlayerService {
@Autowired
private PlayerRepository playerRepository;
public List<Ranking> findAllRanking(Long limit) {
List<Player> players = playerRepository.findAll();
// calculation
return null;
}
Run Code Online (Sandbox Code Playgroud)
计算很简单,得分最高的人排名最高.
如果我有5,7,8,9,3分数,那么排名就是
rank score
1 9
2 8
3 7
4 5
5 3
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激.
假设我有一个方法,其中一些参数用javax @Nonnull注释注释.
public Something supply(@Nonnull SomeObject someObject) {
(...)
if (someObject == null) throw new ValidationException("Required parameter is null");
(...)
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用显式null参数调用此方法时,IDE正确地通知我,如下所示:
somethingSupplier.supply(null);
Run Code Online (Sandbox Code Playgroud)
现在我想要做的是创建单元测试,检查是否会抛出适当的异常,如果由于某种原因传递给此方法的参数恰好为null(假设它是某种验证异常).
如果我尝试像上面的例子中那样调用这个方法,IDE会警告我将null传递给@Nonnull ......但这正是我想在这里做的.我可以注释调用或方法甚至测试类@SuppressWarnings("ConstantConditions"),但它会使代码混乱,我可能不会收到有关我在代码中做出的其他无意错误的警告.
所以问题是我是否能以某种方式禁用测试类中对@Nonnull的检查?或许我不应该对这些案件进行单元测试?
我的目标是使用未知次数的分隔符加入已知字符串.
所以,如果我知道字符串是?和分隔符是,,我知道我想重复5多次,我会得到?,?,?,?,?
这段代码会这样做:
int n = 5;
ArrayList<String> al = new ArrayList<>(n);
for (int i = 0; i < n; ++i) al.add("?");
String s = String.join(",", al);
Run Code Online (Sandbox Code Playgroud)
但那太冗长了.我在Java 8库中遗漏了什么吗?请不要另外依赖.
java ×10
java-8 ×10
lambda ×4
installation ×1
intellij-13 ×1
java-stream ×1
linux ×1
parameters ×1
predicate ×1
spliterator ×1
this ×1
unit-testing ×1