Java 查找列表中最大列表的大小。

huangapple go评论67阅读模式
英文:

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&lt;TileFrame&gt; tileFrames that is inside of List&lt;TileAnimation&gt; class.

TileAnimation.java

public class TileAnimation {
private long localGID;
private List&lt;TileFrame&gt; tileFrames;

public long getLocalGID() {
    return localGID;
}

public void setLocalGID(long localGID) {
    this.localGID = localGID;
}

public List&lt;TileFrame&gt; getTileFrames() {
    return tileFrames;
}

public void setTileFrames(List&lt;TileFrame&gt; tileFrames) {
    this.tileFrames = tileFrames;
}

public TileAnimation(long localGID, List&lt;TileFrame&gt; tileFrames) {
    this.localGID = localGID;
    this.tileFrames = tileFrames;
}

@Override
public String toString() {
    return &quot;TileAnimation{&quot; +
            &quot;localGID=&quot; + localGID +
            &quot;, tileFrames=&quot; + tileFrames +
            &#39;}&#39;;
}
}

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 &quot;TileFrame{&quot; +
            &quot;tileId=&quot; + tileId +
            &quot;, duration=&quot; + duration +
            &#39;}&#39;;
    }
}

Let's say I have such list of objects and I filled it later on:

private List&lt;TileAnimation&gt; tileAnimations = new ArrayList&lt;&gt;();

then how to find such max size in inner List&lt;TileFrame&gt; of List&lt;TileAnimations&gt; ?
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 -&gt; tileAnimation.getTileFrames().size())
       .max()
       .orElse(0);

huangapple
  • 本文由 发表于 2023年4月17日 01:30:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76029323.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定