[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Capturing null values in java

Posted on 2011-10-05
4
Medium Priority
?
254 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:gaugeta
4 Comments
 
LVL 26

Expert Comment

by:ksivananth
ID: 36916149
something like this?

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

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

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36916248
>>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?
0
 
LVL 1

Expert Comment

by:thomasbau65
ID: 36916386
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)...
0
 
LVL 47

Accepted Solution

by:
for_yan earned 2000 total points
ID: 36918572


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);




0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month18 days, 21 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question