Capturing null values in java

Hi experts...
I have two java functions which take in two maps of type LinkedHashMap<String,Integer> sampleMap = new LinkedHashMap<String,Integer>();

I have a condition where one of the values of the maps turn out as null i want to be able to capture its value.

How do i achieve this.
Please help...
public static <K extends Comparable<? super K>, V> Map<K, Boolean> compareEntries(final Map<K, V> map1, final Map<K, V> map2)
	{ 	
		int cnt1=0,cnt2=0;
		String val1=null,val2=null;
		Map<K,Boolean> result=null;
		try
		{
		final Collection<K> allKeys = new HashSet<K>(); 

		allKeys.addAll(map1.keySet()); 

		allKeys.addAll(map2.keySet()); 

		result = new TreeMap<K,Boolean>(); 

		for(final K key : allKeys)
		
		{ 
			result.put(key, map1.containsKey(key) == map2.containsKey(key) && Boolean.valueOf(equal(map1.get(key), map2.get(key))));
		
		
		} 
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}

		return result; 
	}	 
	
	
	private static boolean equal(final Object obj1, final Object obj2)
	{ 
		double max=0;
		int i1=0,i2=0;
		try
		{
		int intermediate=(Integer) obj1;
		max=intermediate*devForStudents;
		i1 = (Integer)obj1;
	    i2 = (Integer) obj2;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		return obj1 == obj2 || (obj1 != null && Math.abs(i1-i2) <= max);
	
	}

Open in new window

gaugetaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ksivananthCommented:
something like this?

if( map1 != null ) allKeys.addAll(map1.keySet());

if( map2 != null ) allKeys.addAll(map2.keySet());

CEHJCommented:
>>I have a condition where one of the values of the maps turn out as null i want to be able to capture its value.

Wouldn't its value be ... null?
thomasbau65Commented:
This is the one line (in your equal method)  making trouble if the Map object contains keys with a null value

int intermediate = (Integer) obj1;

check if the object is null before casting

like:
if(obj1 != null)...
for_yanCommented:


Perhaps you want to change the second method like this:
	private static boolean equal(final Object obj1, final Object obj2)
	{ 
        if(obj1 == null && obj2 == null) return true;
      if(obj1 == null && obj2 != null) return false;
      if(obj1 != null && obj2 == null) return false;
		double max=0;
		int i1=0,i2=0;
		try
		{
		int intermediate=(Integer) obj1;
		max=intermediate*devForStudents;
		i1 = (Integer)obj1;
	    i2 = (Integer) obj2;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		return obj1 == obj2 || (obj1 != null && Math.abs(i1-i2) <= max);
	
	}

Open in new window


I'm also not sure why you want to have
return obj1 == obj2 || (obj1 != null && Math.abs(i1-i2) <= max);

You should proably still have

return obj1.equals(obj2) || (obj1 != null && Math.abs(i1-i2) <= max);




Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.