Java中列表列表的最大值
我试图通过应用过滤器获得最高值 我有一个包含团队对象和卡片列表的对象。 因此,我需要从卡片权重属性中的卡片列表中获取最高值,其中团队为A,然后返回玩家Java中列表列表的最大值,java,lambda,java-stream,comparator,Java,Lambda,Java Stream,Comparator,我试图通过应用过滤器获得最高值 我有一个包含团队对象和卡片列表的对象。 因此,我需要从卡片权重属性中的卡片列表中获取最高值,其中团队为A,然后返回玩家 public class Player { private String name; private List<Card> cards; private Team team; // getters and setters } public class Team { private int id; privat
public class Player {
private String name;
private List<Card> cards;
private Team team;
// getters and setters
}
public class Team {
private int id;
private int points;
// getters and setters
}
public class Card {
private int weightCard;
// getters and setters
}
玩家是由4名玩家组成的列表
错误:
Type mismatch: cannot convert from Stream<Object> to int
请帮帮我,这是用于学术工作的首先,请替换对mapToInt的两个map调用。然后您就不需要Comparator.naturalOrder了
其次,您的第一个map调用包含以下lambda:
n->n.getCards.stream.mapc->c.getWeightCard
这就把一个“n”,不管它是什么,变成了一个weightcard返回的不是您粘贴的代码的流。地图的意义在于把一件事变成另一件事,而不是一连串的事情。您可以使用flatmap来实现这一点,因此可以假定,首先将flatmap映射到一个卡片流,然后通过weightcard函数将其映射到一个int,然后您可以最大化
总而言之:
int maxPointsTeamA=玩家
流动
.filterx->x.getTeam.equalsteamA
.flatMapn->n.getCards.stream
.mapToIntc->c.getWeightCard
麦克斯先生
.orElse0;
编辑:啊,是的,我忘了max返回一个Optionant;已修复。首先替换对mapToInt的两个映射调用。然后您就不需要Comparator.naturalOrder了
其次,您的第一个map调用包含以下lambda:
n->n.getCards.stream.mapc->c.getWeightCard
这就把一个“n”,不管它是什么,变成了一个weightcard返回的不是您粘贴的代码的流。地图的意义在于把一件事变成另一件事,而不是一连串的事情。您可以使用flatmap来实现这一点,因此可以假定,首先将flatmap映射到一个卡片流,然后通过weightcard函数将其映射到一个int,然后您可以最大化
总而言之:
int maxPointsTeamA=玩家
流动
.filterx->x.getTeam.equalsteamA
.flatMapn->n.getCards.stream
.mapToIntc->c.getWeightCard
麦克斯先生
.orElse0;
编辑:啊,是的,我忘了max返回一个Optionant;已修复。这应该可以
int maxPointsTeamA=玩家
流动
.filter x->x.getTeam.equalsteamA
.flatmaplayer->player.getCards
流动
.mapcard->card.getWeightCard
.maxComparator.naturalOrder
.orElse0;
这应该行得通
int maxPointsTeamA=玩家
流动
.filter x->x.getTeam.equalsteamA
.flatmaplayer->player.getCards
流动
.mapcard->card.getWeightCard
.maxComparator.naturalOrder
.orElse0;
试试这个
将列表转换为流。
筛选正确的团队
然后创建一个权重流并选择最大值
这已经用类似的类进行了测试,并且运行正常。试试这个
将列表转换为流。
筛选正确的团队
然后创建一个权重流并选择最大值
这已经用类似的类进行了测试,并且操作正确。根据预期回答 所以我需要从卡片列表中获取最高值 权重属性,其中团队为A,并返回球员
public class Player {
private String name;
private List<Card> cards;
private Team team;
// getters and setters
}
public class Team {
private int id;
private int points;
// getters and setters
}
public class Card {
private int weightCard;
// getters and setters
}
在寻找要返回的播放器时,您不应该映射流,而应该使用自定义比较器查找最大值
基于期望的回答 所以我需要从卡片列表中获取最高值 权重属性,其中团队为A,并返回球员
public class Player {
private String name;
private List<Card> cards;
private Team team;
// getters and setters
}
public class Team {
private int id;
private int points;
// getters and setters
}
public class Card {
private int weightCard;
// getters and setters
}
在寻找要返回的播放器时,您不应该映射流,而应该使用自定义比较器查找最大值
其他人已经给出了很好的答案。但是,这里有一个答案,它还通过使用流为您生成测试数据,并让您获得最大值 覆盖/定义团队的toString方法: 解决方案: 我在代码中添加了注释来解释解决方案的工作原理
class Test{
//MAIN METHOD - START HERE !!!
public static void main(String [] args){
//Prepare test data.
List<Player> players = getTestData();
Player onePlayer = players.get(players.size()/2);//3rd player when list has 5 players.
List<Card> onePlayerCards = onePlayer.getCards();
Team teamA = new Team(onePlayer.getTeam().getId(), onePlayer.getTeam().getPoints());
//Test the max finder.
Integer max = getMax(players, teamA);
System.out.println("Expected max: " + onePlayerCards.get(onePlayerCards.size()-1).getWeightCard());
System.out.println("Actual max: " + max);
}
public static int getMax(List<Player> players, Team team){
//Replace this with method with any other method you'd like to test.
return getMax_byMasterJoe2(players, team);
}
public static int getMax_byMasterJoe2(List<Player> players, Team team){
Integer max = players
.stream()
//Extract player whose team is teamA.
.filter(player -> player.getTeam().equals(team))
//Get the cards of a player.
//.map() gives us Stream<List<Card>>. flatMap() gives us Stream<Card> which is what we need.
.flatMap(player -> player.getCards().stream())
//Get only the weights of each card of a player.
.mapToInt(Card::getWeightCard)
//Get maximum weight of a player's cards.
.max()
//If player has zero cards, then return 0.
.orElse(0);
return max;
}
public static List<Player> getTestData(){
List<Player> players = Stream
//Players numbers.
.of(1,2,3,4,5)
//Use player number to make player object.
.map(n -> new Player(
//Player name.
"player" + n,
//Generate n cards for n-th player. Ex. n=3 gives cards with weights 10, 20, 30.
IntStream.range(1, n+1).mapToObj(num -> new Card(num * 10)).collect(Collectors.toList()),
//Team.
new Team(n, n * 10)))
.collect(Collectors.toList());
//Remove all cards of last player for testing.
players.get(players.size()-1).getCards().clear();
return players;
}
}
其他人已经给出了很好的答案。但是,这里有一个答案,它还通过使用流为您生成测试数据,并让您获得最大值 覆盖/定义团队的toString方法: 解决方案: 我在代码中添加了注释来解释解决方案的工作原理
class Test{
//MAIN METHOD - START HERE !!!
public static void main(String [] args){
//Prepare test data.
List<Player> players = getTestData();
Player onePlayer = players.get(players.size()/2);//3rd player when list has 5 players.
List<Card> onePlayerCards = onePlayer.getCards();
Team teamA = new Team(onePlayer.getTeam().getId(), onePlayer.getTeam().getPoints());
//Test the max finder.
Integer max = getMax(players, teamA);
System.out.println("Expected max: " + onePlayerCards.get(onePlayerCards.size()-1).getWeightCard());
System.out.println("Actual max: " + max);
}
public static int getMax(List<Player> players, Team team){
//Replace this with method with any other method you'd like to test.
return getMax_byMasterJoe2(players, team);
}
public static int getMax_byMasterJoe2(List<Player> players, Team team){
Integer max = players
.stream()
//Extract player whose team is teamA.
.filter(player -> player.getTeam().equals(team))
//Get the cards of a player.
//.map() gives us Stream<List<Card>>. flatMap() gives us Stream<Card> which is what we need.
.flatMap(player -> player.getCards().stream())
//Get only the weights of each card of a player.
.mapToInt(Card::getWeightCard)
//Get maximum weight of a player's cards.
.max()
//If player has zero cards, then return 0.
.orElse(0);
return max;
}
public static List<Player> getTestData(){
List<Player> players = Stream
//Players numbers.
.of(1,2,3,4,5)
//Use player number to make player object.
.map(n -> new Player(
//Player name.
"player" + n,
//Generate n cards for n-th player. Ex. n=3 gives cards with weights 10, 20, 30.
IntStream.range(1, n+1).mapToObj(num -> new Card(num * 10)).collect(Collectors.toList()),
//Team.
new Team(n, n * 10)))
.collect(Collectors.toList());
//Remove all cards of last player for testing.
players.get(players.size()-1).getCards().clear();
return players;
}
}
当你问这样的问题时,你应该提供可以编译的样本数据。比较团队的规则是什么,即检查一个团队是否与另一个团队相等?对于相等,id应该相同,还是id和点数应该相同?每当你问这样的问题时,你应该提供可以编译的样本数据。比较团队的规则是什么,即检查一个团队是否与另一个团队相等?为了公平起见,id应该相同,还是id和点数应该相同?是否可以返回具有最高值的玩家的字母?问题没有解决
抄写玩家的一封信。虽然您可以很容易地将此答案中的玩家映射到其任何属性。是否可以返回具有最高值的玩家的字母?问题没有描述玩家的字母。虽然你可以很容易地将这个答案中的玩家映射到它的任何属性。
class Test{
//MAIN METHOD - START HERE !!!
public static void main(String [] args){
//Prepare test data.
List<Player> players = getTestData();
Player onePlayer = players.get(players.size()/2);//3rd player when list has 5 players.
List<Card> onePlayerCards = onePlayer.getCards();
Team teamA = new Team(onePlayer.getTeam().getId(), onePlayer.getTeam().getPoints());
//Test the max finder.
Integer max = getMax(players, teamA);
System.out.println("Expected max: " + onePlayerCards.get(onePlayerCards.size()-1).getWeightCard());
System.out.println("Actual max: " + max);
}
public static int getMax(List<Player> players, Team team){
//Replace this with method with any other method you'd like to test.
return getMax_byMasterJoe2(players, team);
}
public static int getMax_byMasterJoe2(List<Player> players, Team team){
Integer max = players
.stream()
//Extract player whose team is teamA.
.filter(player -> player.getTeam().equals(team))
//Get the cards of a player.
//.map() gives us Stream<List<Card>>. flatMap() gives us Stream<Card> which is what we need.
.flatMap(player -> player.getCards().stream())
//Get only the weights of each card of a player.
.mapToInt(Card::getWeightCard)
//Get maximum weight of a player's cards.
.max()
//If player has zero cards, then return 0.
.orElse(0);
return max;
}
public static List<Player> getTestData(){
List<Player> players = Stream
//Players numbers.
.of(1,2,3,4,5)
//Use player number to make player object.
.map(n -> new Player(
//Player name.
"player" + n,
//Generate n cards for n-th player. Ex. n=3 gives cards with weights 10, 20, 30.
IntStream.range(1, n+1).mapToObj(num -> new Card(num * 10)).collect(Collectors.toList()),
//Team.
new Team(n, n * 10)))
.collect(Collectors.toList());
//Remove all cards of last player for testing.
players.get(players.size()-1).getCards().clear();
return players;
}
}