是否有更智能的方法来通过获取 gameList 集合并将 Game 对象转换为字符串来填充此字符串列表?
ArrayList<Game> gameList = getAllGames();
ArrayList<String> stringList = new ArrayList<String>();
for (Game game : gameList) {
stringList.add(game.toString());
}
最佳答案
使用 Java 8:
ArrayList<String> stringList = gameList.stream().map(Object::toString).collect(Collectors.toCollection(ArrayList::new));
(注意:我尚未对此进行测试。)
关于java - 如何从 ArrayList<Object> 填充 ArrayList<String>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25611965/