Hi All,

I have a first hashmap with some values and I have a second hashmap with diff values.

Question:

How can I find the diff between two hash maps and print only the difference.

~Vilas

Is there any example that can be shared...
I am doing the following

import java.util.*; 

public class HashTree 
{


	public static void main(String[] args) 
	{

		HashMap<String, Boolean> hm = new HashMap<String, Boolean>();
		HashMap<String, Boolean> hm1 = new HashMap<String, Boolean>();
		
		hm.put("POM", true);
		hm.put("PS", false);
		hm.put("ITEM", true);
		hm.put("FOLDER", false);
		
		for( String s : hm.keySet())
		{
			System.out.println(" The values of the String are" + hm.keySet());
			System.out.println(" The values of the String are" + hm.values());
			
		}
			
		hm1.put("POM", true);
		hm1.put("PS", false);
		hm1.put("ITEM", true);
		hm1.put("FOLDER", true);
		
		System.out.println("----Before---------------");
		for( String s1 : hm1.keySet())
		{
			System.out.println(" The values of the String are" + hm1.keySet());
			System.out.println(" The values of the String are" + hm1.values());
			
		}
		System.out.println("----Before-----------");
		
		for( String s : hm.keySet())
		{
			for(String s1 : hm1.keySet())
			{
				if(hm1.containsValue(hm.values())) hm1.remove(hm1.keySet());
				
			}
		}
		System.out.println("-----after----------");
		for( String s1 : hm1.keySet())
		{
			System.out.println(" The values of the String are" + hm1.keySet());
			System.out.println(" The values of the String are" + hm1.values());
			
		}
		System.out.println("-----after----------");
		
		}

	}

The output should be hm1 values are different?

~Vilas

Edited 5 Years Ago by peter_budo: Keep It Clear - Do wrap your programming code blocks within [code] ... [/code] tags

Ok have found a solutionn but it gives me the results twice...how to avoid that...here is the snippet

import java.util.*; 
import java.util.Map.Entry;

public class HashTree 
{


	public static void main(String[] args) 
	{

		HashMap<String, Boolean> hm = new HashMap<String, Boolean>();
		HashMap<String, Boolean> hm1 = new HashMap<String, Boolean>();
		
		hm.put("POM", true);
		hm.put("PS", false);
		hm.put("ITEM", true);
		hm.put("FOLDER", false);
		hm.put("FOLDER1", false);
		
		for( String s : hm.keySet())
		{
			System.out.println(" The values of the String are" + hm.keySet());
			System.out.println(" The values of the String are" + hm.values());
			
		}
			
		hm1.put("POM", true);
		hm1.put("PS", false);
		hm1.put("ITEM", true);
		hm1.put("FOLDER", true);
		hm1.put("FOLDER1", true);
		
		System.out.println("----Before---------------");
		for( String s1 : hm1.keySet())
		{
			System.out.println(" The values of the String are" + hm1.keySet());
			System.out.println(" The values of the String are" + hm1.values());
			
		}
		System.out.println("----Before-----------");
		

		System.out.println("-----after----------");
//		for( String s1 : hm1.keySet())
//		{
//			System.out.println(" The values of the String are" + hm1.keySet());
//			System.out.println(" The values of the String are" + hm1.values());
//			
//		}
		Map<String,Boolean> result = new HashMap<String,Boolean>(); 
		Set<Map.Entry<String, Boolean>> filter = hm.entrySet();
		for( Map.Entry<String,Boolean> entry : hm1.entrySet() )
		{
			 if( !filter.contains( entry ))
			 { 
			        result.put(entry.getKey(), entry.getValue()); 
			    }
		}
		
		for(String s2 : result.keySet())
		{
			
			System.out.println(" The values of the String are" + result.keySet());
			//System.out.println(" The values of the String are" + result.values());
		
		}
		
		
		System.out.println("-----after----------");
		
		}

	}

Here are the results

The values of the String are[POM, ITEM, PS, FOLDER, FOLDER1]
The values of the String are[true, true, false, false, false]
The values of the String are[POM, ITEM, PS, FOLDER, FOLDER1]
The values of the String are[true, true, false, false, false]
The values of the String are[POM, ITEM, PS, FOLDER, FOLDER1]
The values of the String are[true, true, false, false, false]
The values of the String are[POM, ITEM, PS, FOLDER, FOLDER1]
The values of the String are[true, true, false, false, false]
The values of the String are[POM, ITEM, PS, FOLDER, FOLDER1]
The values of the String are[true, true, false, false, false]
----Before---------------
The values of the String are[POM, ITEM, PS, FOLDER, FOLDER1]
The values of the String are[true, true, false, true, true]
The values of the String are[POM, ITEM, PS, FOLDER, FOLDER1]
The values of the String are[true, true, false, true, true]
The values of the String are[POM, ITEM, PS, FOLDER, FOLDER1]
The values of the String are[true, true, false, true, true]
The values of the String are[POM, ITEM, PS, FOLDER, FOLDER1]
The values of the String are[true, true, false, true, true]
The values of the String are[POM, ITEM, PS, FOLDER, FOLDER1]
The values of the String are[true, true, false, true, true]
----Before-----------
-----after----------
The values of the String are[FOLDER, FOLDER1]
The values of the String are[FOLDER, FOLDER1]
-----after----------

Comments
nicely done

vilas_tadoori - nicely done.

There are two ways to print

//for (String s : hm.keySet()) {
        System.out.println(" K are" + hm.keySet());  //used default toString() method hm.keySet().toString()
        System.out.println(" V are" + hm.values());  //used default toString() method
        //}
        System.out.println();

        for (String s : hm.keySet()) {
            System.out.println(" K = " + s + ",V = " + hm.get(s));
        }
This question has already been answered. Start a new discussion instead.