简体   繁体   English

将两个ArrayList添加到一个hashmap的ArrayList中

[英]Add two ArrayList to one ArrayList of hashmaps

I have two ArrayList and I want to make one ArrayList by adding them, both lists have same size 我有两个ArrayList ,我想通过添加它们来创建一个ArrayList ,两个列表具有相同的大小

I am going to do it this way. 我会这样做。

Is this optimized or can I make it better and efficient when the lists become large? 这是优化还是我可以在列表变大时使其更好,更有效?

ie

    private ArrayList<Bitmap> imageFile= new ArrayList<Bitmap>();

    imageFile.add(xy); 
    imageFile.add(ab);
    imageFile.add(cd);

    private ArrayList<MediaPlayer> musicFile= new ArrayList<MediaPlayer>();

    musicFile.add(mm);
    musicFile.add(nn);
    musicFile.add(ll);

    private HashMap<Bitmap, MediaPlayer> mappedFiles= new HashMap<Bitmap, MediaPlayer>();

    mappedFiles.put(imageFile.get(i),musicFile.get(i))


    private ArrayList<HashMap<Bitmap, MediaPlayer>> imageMusic= new ArrayList<HashMap<Bitmap, MediaPlayer>>();

   imageMusic.add(mappedFiles);

Write a wrapper for Bitmap , and Media Player yourself 为Bitmap和Media Player自己编写一个包装器

class Media {
   Bitmap bitmap;
   MediaPlayer mediaPlayer
}

When you have to map bitmap and mediaplayer, create an object of this class and push them to an ArrayList<Media> ? 当你必须映射位图和ArrayList<Media> ,创建这个类的对象并将它们推送到ArrayList<Media>

Why do you want to complicate by using HashMap of Bitmap of MediaPlayer? 为什么要使用MediaPlayer的Bitmap的HashMap来复杂化?

Based on your comment, you don't want a map at all, you want classes and Lists: 根据您的评论,您根本不需要地图,您需要类和列表:

public class Track {
    private final String name;
    private final MediaPlayer music;
    public Track (String name, MediaPlayer music) {
        this.name = name;
        this.music = music;
    }
    // getters omitted
}

public class CD {
    private final String name;
    private final BitMap image; 
    private final List<Track> tracks = new ArrayList<Track>();
    public CD (String name, BitMap image) {
        this.name = name;
        this.image = image;
    }
    public List<Track> getTracks() {
        return tracks;
    } 
    // other getters omitted
}

Then 然后

List<CD> cds = new List<CD>();
CD cd = new CD("Thriller", someBitMap);
cd.getTracks().add(new Track("I'm bad", someMusic));
cds.add(cd);

Try this way 试试这种方式

    public static void main(String[] args) {
        ArrayList<String> imageFile = new ArrayList<String>();

        imageFile.add("XY");
        imageFile.add("ZZ");
        imageFile.add("YY");

        ArrayList<Integer> musicFile = new ArrayList<Integer>();

        musicFile.add(1);
        musicFile.add(2);
        musicFile.add(4);

        Map<List<String>, List<Integer>> fullMap = new HashMap<List<String>, List<Integer>>();
        fullMap.put(imageFile, musicFile);

    }
}

If you really do need that structure (I don't understand what's the deal with the last ArrayList ), then I think it's fairly optimized. 如果你真的需要那个结构(我不明白最后一个ArrayList的处理是什么),那么我认为它已经相当优化了。

You don't have a way to easily create a Map out of two lists, so you'll have to cycle through them, since these are array lists, a very simple for will be quite explicit: 你没有办法轻松地创建一个映射出两个列表,所以你通过他们不得不周期,因为这些数组列表,一个很简单for将是相当明确的:

private HashMap<Bitmap, MediaPlayer> mappedFiles= new HashMap<Bitmap, MediaPlayer>();
for(int i=0; i<imageFile.size(); i++) {
    mappedFiles.put(imageFile.get(i), musicFile.get(i));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM