如何使用三个列表将数据添加到自定义 ArrayList?

How to add data to a custom ArrayList using three lists?

我有三个 String Lists ,我想用它们来填充 ActorData (POJO class) 类型的 ArrayList,如何正确完成这项工作?

数据示例

演员姓名

actornames = {
0 = "name1",
1 = "name2"
}

演员角色

actorcharacters = {
0 = "character1",
1 = "character2"
}

演员照片文件路径

actorphotoFILEpaths = {
0 = "filepath1",
1 = "filepath2"
}

我的问题是在 activity classonCreate 中使用以下方法收集所有三个列表,即生成的 actors 列表是这样的:

actors = [
0 = {
     name = "name1",
     character = null,
     photofilepath = null
    },
1 = {
     name = "name2",
     character = null,
     photofilepath = null
    },
2 = {
     name = null,
     character = "character1",
     photofilepath = null
    },
3 = {
     name = null,
     character = "character2",
     photofilepath = null
    },
4 = {
     name = null,
     character = null,
     photofilepath = "photofilepath1"
    },
5 = {
     name = null,
     character = null,
     photofilepath = "photofilepath2"
    }
]

但是我希望输出是这样的

actors = [
0 = {name = "name1",
     character = "character1",
     photofilepath = "photofilepath1"
    },
1 = {name = "name2",
     character = "character2",
     photofilepath = "photofilepath2"
    }
]

Activity

public void onCreate(Bundle savedInstanceState) {

    rest of the code ......

    List<String> actornames = new ArrayList<>();
    List<String> actorcharacters = new ArrayList<>();
    List<String> actorphotoFILEpaths = new ArrayList<>();
    List<ActorData> actors = new ArrayList<ActorData>();        

    // Initializing actornames arraylist
    String actorname = mMovie.getActorname();
    actornames = Arrays.asList(actorname.split("\s*,\s*"));

    // Initializing actorcharacters arraylist
    String actorcharacter = mMovie.getActorcharacter();
    actorcharacters = Arrays.asList(actorcharacter.split("\s*,\s*"));

    // Initializing actorphotoFILEpaths arraylist
    String baseFILEpath = "/storage/emulated/0/Android/data/com.miz.mizuu/files/movie-actors/";
    String actorphotourl = mMovie.getActorphotourl();
    actorphotoFILEpaths = Arrays.asList(actorphotourl.split("\s*,\s*"));        

    List<ActorData> actors = new ArrayList<ActorData>();
    for (int i = 0; i < actornames.size(); i++) {
    ActorData actorData = new ActorData();
    String name = actornames.get(i);
    actorData.setName(name);
    actors.add(actorData);
    }
    for (int i = 0; i < actorcharacters.size(); i++) {
    ActorData actorData = new ActorData();
    String character = actorcharacters.get(i);
    actorData.setCharacter(character);
    actors.add(actorData);
    }
    for (int i = 0; i < actorphotoFILEpaths.size(); i++) {
    ActorData actorData = new ActorData();
    String filepath = baseFILEpath + actorphotoFILEpaths.get(i).replaceAll("/", "");
    actorData.setPhotoFILEpath(filepath);
    actors.add(actorData);
    } 
}        

POJO class

public class ActorData {
    String name, character, photofilepath;
    public ActorData() {}

    public String getName() {return name;}

    public void setName(String name) {this.name = name;}

    public String getCharacter() {return character;}

    public void setCharacter(String character) {this.character = character;}

    public String getPhotoFILEpath() {return photofilepath;}

    public void setPhotoFILEpath(String photofilepath) {this.photofilepath = photofilepath;}
}

我通过组合 for loops

完成了它
     List<ActorData> actors = new ArrayList<>();
                for (int i = 0; (i < actornames.size()) && (i < actorcharacters.size()) && (i < actorphotoFILEpaths.size()); i++) {
                    ActorData actorData = new ActorData();
                    String name = actornames.get(i);
                    String character = actorcharacters.get(i);
                    String filepath = baseFILEpath + actorphotoFILEpaths.get(i).replaceAll("/", "");
                    actorData.setName(name);
                    actorData.setCharacter(character);
                    actorData.setPhotoFILEpath(filepath);
                    actors.add(actorData);
                }

输出

actors = [
0 = {
     name = "name1",
     character = "character1",
     photofilepath = "photofilepath1"
    },
1 = {
     name = "name2",
     character = "character2",
     photofilepath = "photofilepath2"
    }
]

tl;博士

record Actor( String name , String character , Path filePath ) { }

List < String > names = List.of( "Alice" , "Bob" );
List < String > characters = List.of( "Juliet" , "Romeo" );
List < Path > paths = List.of( Paths.get( "filepath1" ) , Paths.get( "filepath2" ) );

if ( ( names.size() + characters.size() + paths.size() ) != ( names.size() * 3 ) )
{
    throw new IllegalStateException( "Input list sizes differ. Message # 075e7165-09d5-4256-8e5e-b5431520b1b4." );
}

List < Actor > actors = new ArrayList <>( names.size() );
for ( int index = 0 ; index < names.size() ; index++ )
{
    actors.add(
            new Actor( names.get( index ) , characters.get( index ) , paths.get( index ) )
    );
}

actors = List.copyOf( actors );

记录

Java 16 及更高版本提供了一种编写 class 的更简洁的方法,其主要目的是透明且不可变地通信数据。您只需声明每个成员字段的名称和类型。编译器隐式创建默认构造函数、getter、equals & hashCodetoString。参见 JEP 395: Records

整个记录定义:

record Actor( String name , String character , Path filePath ) { }

对于操作路径和文件,了解使用 Java NIO.2 classes 而不是遗留的 class。可能 Path 可能是适合您需要的 class。参见 tutorial by Oracle

List.of

做一些样本数据,写成unmodifiable lists using List.of.

List < String > names = List.of( "Alice" , "Bob" );
List < String > characters = List.of( "Juliet" , "Romeo" );
List < Path > paths = List.of( Paths.get( "filepath1" ) , Paths.get( "filepath2" ) );

验证输入

验证所有输入列表的大小是否相同。

if ( ( names.size() + characters.size() + paths.size() ) != ( names.size() * 3 ) )
{
    throw new IllegalStateException( "Input list sizes differ. Message # 075e7165-09d5-4256-8e5e-b5431520b1b4." );
}

实例化对象,并收集

以及核心逻辑,将三个输入列表中的每一个的元素组装成一个新的 Actor 对象。

List < Actor > actors = new ArrayList <>( names.size() );
for ( int index = 0 ; index < names.size() ; index++ )
{
    actors.add(
            new Actor( names.get( index ) , characters.get( index ) , paths.get( index ) )
    );
}

转储到控制台。

System.out.println( "actors = " + actors );

当运行.

actors = [Actor[name=Alice, character=Juliet, filePath=filepath1], Actor[name=Bob, character=Romeo, filePath=filepath2]]

通常最好 return 一个不可修改的列表。使用 List.copyOf.

return List.copyOf( actors ) ;