Map

java.util.Map

Map represents a dictionary in which a pair of objects are stored.

It is a kind of dynamically growing associative array that stores key value pairs: note the (K,V) generic type parameters.

Map is an interface. Some implementations are:
– HashMap
– Hashtable
– Properties
– TreeMap
– LinkedHashMap

HashMap orders elements according to their hashCode. If sorted order is required, TreeMap can be used.

An example

// Song.java
package album;
import java.util.*;

public class Song
{
	private String incipit;
	private String lyricist;
	private String movie;
	private int year;

	public Song(String incipit, String lyricist, String movie, int year)
	{
		this.incipit = incipit;
		this.lyricist = lyricist;
		this.movie = movie;
		this.year = year;
	}

	public void setIncipit(String incipit)
	{
		this.incipit = incipit;
	}

	public String getIncipit()
	{
		return incipit;
	}

	public void setLyricist(String lyricist)
	{
		this.lyricist = lyricist;
	}

	public String getLyricist()
	{
		return lyricist;
	}

	public void setMovie(String movie)
	{
		this.movie = movie;
	}

	public String getMovie()
	{
		return movie;
	}

	public void setYear(int year)
	{
		this.year = year;
	}

	public int getYear()
	{
		return year;
	}

	@Override
	public String toString()
	{
		return incipit+":"+lyricist+":"+movie+":"+year;
	}
	
}

// SongAlbum.java
package album;
import java.util.*;

public class SongAlbum
{
	private Map<String,Song> album = new HashMap<>();
	
	public void add(String director, Song song) {
		album.put(director,song);
	}
	
	public void showAllSongs() {
		Set<Map.Entry<String,Song>> entrySet = album.entrySet();
		Iterator<Map.Entry<String,Song>> entries = entrySet.iterator();
		while(entries.hasNext()) {
			Map.Entry<String,Song> entry = entries.next();
			String doc = entry.getKey();
			Song song = entry.getValue();
			System.out.println(doc+"->"+song);
		}
		
	}
	
	public void showSongsByYear() {
		Set<String> directors = album.keySet();
		ArrayList<Song> songList = new ArrayList<>();
		Iterator<String> keys = directors.iterator();
		while(keys.hasNext()) {
			Song song = album.get(keys.next());
			songList.add(song);
		}
		Collections.sort(songList,new YearSort());
		for(Song song : songList) {
			System.out.println(song);
		}
	}
	
	public static void main(String[] args) {
		Song hawa = new Song("Hawa Mein Udta Jaye / हवा में उड़ता जाए मोरा लाल दुपट्टा", "Jalal Malihabadi", "Barsaat", 1949);
		Song chori = new Song("Chori Chori Chupke Chupke / चोरी चोरी चुपके चुपके", "Anand Bakshi", "aap ki kasam", 1974);
		Song pyar = new Song("Pyaar Hume Kis Mod Pe Le Aaya / प्यार हमे किस मोडपे ले आया", "Gulshan Bawra", "Satte Pe Satta",1982);
		Song andaz = new Song("	Jhoom Jhoom Ke Nacho Aaj", "Majrooh Sultanpuri", "Andaz", 1949);
		SongAlbum alb = new SongAlbum();
		
		alb.add("Rahul Dev Burman", pyar);
		alb.add("Rahul Dev Burman", chori);
		alb.add("Naushad", andaz);
		alb.add("Shankar Jaikishan", hawa);
		
		alb.showAllSongs();
		System.out.println();
		alb.showSongsByYear();
	}
}

// YearSort.java
package album;

import java.util.Comparator;

public class YearSort implements Comparator<Song>
{
	@Override
	public int compare(Song p1, Song p2)
	{
		return p1.getYear() - p2.getYear();
	}

}

Note
– No duplicates
– Ordering of entries using the comparator
– Using the Map.Entry inner class of HashMap to iterate

Advertisements

Author: Anand Betanabhotla

Corporate Trainer

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s