Prateek
Prateek

Reputation: 4013

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

I am going to do it this way.

Is this optimized or can I make it better and efficient when the lists become large?

i.e.

    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);

Upvotes: 3

Views: 4983

Answers (4)

Bohemian
Bohemian

Reputation: 425003

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);

Upvotes: 1

divyanshm
divyanshm

Reputation: 6800

Write a wrapper for Bitmap , and Media Player yourself

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> ?

Why do you want to complicate by using HashMap of Bitmap of MediaPlayer?

Upvotes: 1

pcalcao
pcalcao

Reputation: 15990

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.

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:

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));
}

Upvotes: 0

Makky
Makky

Reputation: 17463

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);

    }
}

Upvotes: 0

Related Questions