我想在java 8中为内部写:
for (String file : files) {
for (String line : lines) {
if (file.contains(line)) {
//do something
}
}
}
Run Code Online (Sandbox Code Playgroud)
我不想为每个内部写每个像:
files.stream().forEach(file -> {
lines.stream().forEach(line-> {
//do something
})
})
Run Code Online (Sandbox Code Playgroud)
有没有像
(file, line) -> { //do something}
Run Code Online (Sandbox Code Playgroud)
在这对中,我会得到所有可能的排列
我有这段代码,从具有某些条件的DeviceEvents列表中提取
List<DeviceEvent> deviceEvents = new ArrayList<>();
deviceEventService
.findAll(loggedInUser())
.filter(this::isAlarmMessage)
.iterator()
.forEachRemaining(deviceEvents::add);
private boolean isAlarmMessage (DeviceEvent deviceEvent) {
return AlarmLevelEnum.HIGH == deviceEvent.getDeviceMessage().getLevel();
}
Run Code Online (Sandbox Code Playgroud)
但我得到了这个编译错误:
The method filter(this::isAlarmMessage) is undefined for the type
Iterable<DeviceEvent>
Run Code Online (Sandbox Code Playgroud)
findAll 返回一个 Iterable<DeviceEvent> 所以我知道我可以用两.stream行来做到这一点,但不确定我是否只能用一行来做到这一点.这就是我所拥有的:
List<Long> abcIds= abcController.findByUserIds(userIds)
.stream()
.map(Abc::getAbcId)
.collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
但我希望abcIds成为一个整数列表,因为稍后我会使用它的其他函数.我知道我可以写这样的另一行来将List of Long转换为整数列表:
List<Integer> abcIntIds= abcIds.stream()
.map(Long::intValue)
.collec?t(Collectors.toList(??));
Run Code Online (Sandbox Code Playgroud)
但有没有办法把它写得更优雅?
我有以下结构:
class MyClass {
String name;
String descr;
public String getName() {
return name;
}
}
Run Code Online (Sandbox Code Playgroud)
现在我有一个这些对象的列表,如果列表包含任何这些元素,我想从上面的对象打印名称.
到目前为止这是我的代码:
List<MyClass> list = getList();
if (list != null && list.size() > 0) {
System.out.println(list.get(0).getName());
} else {
System.out.println("list is empty");
}
Run Code Online (Sandbox Code Playgroud)
当list只包含一个元素时,这将起作用.现在我需要改进它并考虑一个例子,当有多个元素时 - 在这种情况下我需要打印所有名称,逗号分隔.
例如,输出应该是:
当有3个元素时:
name1,name2,name3
Run Code Online (Sandbox Code Playgroud)
当有一个元素时:
name1
Run Code Online (Sandbox Code Playgroud)
什么时候没有:
list is empty
Run Code Online (Sandbox Code Playgroud)
什么是最有效的实施方式?
我想用优雅的java 8流或lambda解决方案替换以下for循环.有什么简洁有效的吗?
public static void main(String[] args) {
ArrayList<Integer> myList = new ArrayList<>( Arrays.asList( 10,-3,5));
// add 1/2 of previous element to each element
for(int i =1 ;i < myList.size(); ++i )
myList.set(i, myList.get(i)+myList.get(i-1)/2);
// myList.skip(1).forEach( e -> e + prevE/2 ); // looking for something in this spirit
}
Run Code Online (Sandbox Code Playgroud) 我想使用java 8流方法从列表中获取最大值.
结构如下:
Round.Round对象都存储在一个ArrayList被调用的中arrRoundRound对象都有一个字段:List<Hit> hitsHit由2个字段组成:int numberOfGames和int prizeAmountpublic class Round{
private List<Hits> hits;
}
public class Hits{
private int numberOfGames;
private int prizeAmount;
}
Run Code Online (Sandbox Code Playgroud)
我想要做的是迭代所有元素arrRound,获取他们的命中字段的getPrizeAmount()方法并获得最大值.我开始如下,但似乎无法做到:
public class Main(){
public void main(String[]args){
List<Round> arrRound = getRoundFromCSV();
int maxPrize = arrRound.stream()
.forEach(round -> {
round.getHits()
.forEach(hit -> hit.getPrizeAmount());
});
}
}
Run Code Online (Sandbox Code Playgroud)
而且我无法在声明的末尾调用max().
提前谢谢你的帮助!
我有ArrayList一个类对象,如下所示:
ArrayList<Score> scoreboard = new ArrayList<>();
Run Code Online (Sandbox Code Playgroud)
该Score班有一个字段points:
class Score {
private int points;
//constructor and methods
}
Run Code Online (Sandbox Code Playgroud)
我将如何使用Java流来比较points每个Score对象并返回具有最高/最低值的对象?
我试过这样的东西,但它不起作用:
scoreboard
.stream()
.max(Comparator.comparing(Score::getPoints)
.get()
.forEach(System::println);
Run Code Online (Sandbox Code Playgroud) 我正在阅读Vavr使用指南中关于使用Match和其他"语法糖"执行副作用的部分.这是给出的例子:
Match(arg).of(
Case($(isIn("-h", "--help")), o -> run(this::displayHelp)),
Case($(isIn("-v", "--version")), o -> run(this::displayVersion)),
Case($(), o -> run(() -> {
throw new IllegalArgumentException(arg);
}))
);
Run Code Online (Sandbox Code Playgroud)
然后讨论如何run不应该在lambda体外运行等等.
恕我直言,在解释中缺少一些东西让我完全清晰,即run在某些Vavr接口(我找不到)上的现有方法,或者它应该是我自己在周围代码库中的方法?
所以我努力并且稍微阐述了上面的例子,我可以运行并看到它的结果:
@Test public void match(){
String arg = "-h";
Object r = Match(arg).of(
Case($(isIn("-h", "--help")), o -> run(this::displayHelp)),
Case($(isIn("-v", "--version")), o -> run(this::displayVersion)),
Case($(), o -> run(() -> {
throw new IllegalArgumentException(arg);
}))
);
System.out.println(r);
}
private Void run(Supplier<String> supp) {
System.out.println(supp.get());
return null;}
private String displayHelp() {return "This …Run Code Online (Sandbox Code Playgroud) 我将JFoenix库用于我的Comboboxes.
' boxLeague.getSelectionModel().selectedItemProperty().addListener((observable,oldValue,newValue) - > boxTeams.setItems(listPremierLeague)); 当从boxLeague Combobox中选择任何内容时,'会将所有文本放到boxTeams Combobox中,但我想要做的是当在boxLeague中选择特定项时,然后填充另一个组合框.
public class Controller implements Initializable {
@FXML
private JFXComboBox<String> boxLeague;
@FXML
private JFXComboBox<String> boxTeams;
@FXML
private JFXComboBox<String> boxPlayers;
ObservableList<String> listLeagues = FXCollections.observableArrayList(
"Bundesliga", "La Liga", "Ligue 1", "Premier League", "Serie A", "Champions League", "Europa League");
ObservableList<String> listPremierLeague = FXCollections.observableArrayList(
"Arsenal", "Bournemouth", "Brighton", "Burnley", "Chelsea", "Crystal Palace", "Everton");
@Override
public void initialize(URL location, ResourceBundle resources) {
boxLeague.setItems(listLeagues);
boxLeague.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> boxTeams.setItems(listPremierLeague));
}
Run Code Online (Sandbox Code Playgroud)
}
从java 8开始迭代通过列表我可以使用两者:
List list = new ArrayList();
1. list.forEach(...)
2. list.stream().forEach(...)
Run Code Online (Sandbox Code Playgroud)
使用第二种情况有什么好处吗?要将列表转换为流?
java-8 ×10
java ×8
java-stream ×6
lambda ×2
collections ×1
comparator ×1
foreach ×1
javafx ×1
jfoenix ×1
long-integer ×1
max ×1
methods ×1
vavr ×1