英文:
Java find biggest size of list in list
问题
You can find the maximum size of the inner List<TileFrame>
within the List<TileAnimation>
using Java. Here's the code to do that:
int maxTileFramesSize = 0;
for (TileAnimation animation : tileAnimations) {
int tileFramesSize = animation.getTileFrames().size();
if (tileFramesSize > maxTileFramesSize) {
maxTileFramesSize = tileFramesSize;
}
}
System.out.println("Maximum size of inner List<TileFrame>: " + maxTileFramesSize);
This code iterates through the tileAnimations
list and checks the size of the tileFrames
list within each TileAnimation
object. It keeps track of the maximum size encountered and prints it out at the end.
英文:
I want to find the biggest size() of List<TileFrame>
tileFrames that is inside of List<TileAnimation>
class.
TileAnimation.java
public class TileAnimation {
private long localGID;
private List<TileFrame> tileFrames;
public long getLocalGID() {
return localGID;
}
public void setLocalGID(long localGID) {
this.localGID = localGID;
}
public List<TileFrame> getTileFrames() {
return tileFrames;
}
public void setTileFrames(List<TileFrame> tileFrames) {
this.tileFrames = tileFrames;
}
public TileAnimation(long localGID, List<TileFrame> tileFrames) {
this.localGID = localGID;
this.tileFrames = tileFrames;
}
@Override
public String toString() {
return "TileAnimation{" +
"localGID=" + localGID +
", tileFrames=" + tileFrames +
'}';
}
}
TileFrame.java
public class TileFrame {
private long tileId;
private long duration;
public long getTileId() {
return tileId;
}
public void setTileId(long tileId) {
this.tileId = tileId;
}
public long getDuration() {
return duration;
}
public void setDuration(long duration) {
this.duration = duration;
}
public TileFrame(long tileId, long duration) {
this.tileId = tileId;
this.duration = duration;
}
@Override
public String toString() {
return "TileFrame{" +
"tileId=" + tileId +
", duration=" + duration +
'}';
}
}
Let's say I have such list of objects and I filled it later on:
private List<TileAnimation> tileAnimations = new ArrayList<>();
then how to find such max size in inner List<TileFrame>
of List<TileAnimations>
?
All the solutions I know when there are just one list with sigular fields without inner lists, but here it is something new for me and I could not find any solution in the internet of such issue.
答案1
得分: 3
你可以简单地使用流来实现它。
英文:
you can achieve it simply by using streams
int maxTileFramesSize = tileAnimations.stream()
.mapToInt(tileAnimation -> tileAnimation.getTileFrames().size())
.max()
.orElse(0);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论