Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 662
Prateek Avatar asked Feb 26 '26 23:02

Prateek


2 Answers

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);
like image 90
Bohemian Avatar answered Feb 28 '26 11:02

Bohemian


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?

like image 42
divyanshm Avatar answered Feb 28 '26 13:02

divyanshm