Reputation: 157
Basically, I have an arraylist declared like :
private ArrayList<HashMap<String, String>> songsList =
new ArrayList<HashMap<String, String>>();
now what I want to do is that for each key I want to give two value fields. Can it be done??
Secondly I want to sort the key-value pair according to the value pair. So how can it be achieved?
Upvotes: 0
Views: 1070
Reputation: 14223
That's a lot of String
objects. Have you considered encapsulating your data structure using an Object
, rather than a complex collection of strings? For example, assuming your map key is a song:
public Song implements Comparable<Song> {
private String title;
private String artist;
private String album;
public Song(String title, String artist, String album) {
...
}
public int compareTo(Song s) {
// sorting logic
}
... // getters, setters, equals & hashCode
}
Then, create your list of songs:
List<Song> songs = new ArrayList<Song>();
songs.add(new Song("Dancing Queen", "Abba", "Arrival"); // I had to find this on Wikipedia
...
Then sort them:
Collections.sort(songs);
Upvotes: 1
Reputation: 3942
You can implement the comparable interface for the sorting problem of yours. And for having multiple values for a value in a map, create a class with the required values with getters and setters. And use the class as a value against the key.
Upvotes: 0