3

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);
7
  • "Optimized" depends on what you need. Are there duplicates? Same entries in both lists? Do you want to get rid of them? Does order matter? Commented May 9, 2013 at 12:18
  • @Makky I want to make arraylist of hashmaps from two different arraylists of same size. Commented May 9, 2013 at 12:22
  • Why an ArrayList of HashMaps? Are there other HashMaps you will be adding to the ArrayList later? Commented May 9, 2013 at 12:23
  • Yeah you are right I will add more hashmaps... to my arraylist of hashmaps. Commented May 9, 2013 at 12:25
  • 1
    Why multiple hashmaps? don't you want one hashmap with the images as keys to the music files? Do you need the lists alone, or are they just a mechanism to create the map? Commented May 9, 2013 at 12:27

4 Answers 4

1

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);
Sign up to request clarification or add additional context in comments.

2 Comments

but how much complexity would my approach incur.
Well it's going to be harder for you as the programmer to figure out what is going on. That will lead to bugs and/or frustration. Keep things beat and orderly and you'll stay sane and build you software. This approach also lets you easily add more attributes like track length, track style, CD genre, etc etc. you approach would quickly become a mess IMHO
1

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?

Comments

0

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

    }
}

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.