Avatar of GAUTAM
GAUTAMFlag for United States of America

asked on 

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

JavaJava EE

Avatar of undefined
Last Comment
for_yan
Avatar of ksivananth
ksivananth
Flag of United States of America image

something like this?

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

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

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>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?
Avatar of thomasbau65
thomasbau65

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)...
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo