This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Java Generics HashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java Generics HashMap" Watch "Java Generics HashMap" New topic
Author

Java Generics HashMap

arunkumar subramanian
Ranch Hand

Joined: Jun 10, 2008
Posts: 32
Hi

We just upgraded Jdk1.4 to 1.5. I'm learning Generics and implementing it. Please let me know how can I change this piece of code to use Generics

The old code looks like

public HashMap get(int tempId)
{
HashMap result = new HashMap();
result = getResultMap(tempId);
if (result.isEmpty() || result == null)
{
result.put("Error", "Search returned no records");
}
return result;
}

private HashMap getResultMap(int tempId)
{
HashMap result = null;
ArrayList tempList = new ArrayList();
SomeObject object = null;
... Database Operations ...
while (resultSet.next())
{
... Flag check and Instantiate Hashmap ...
object = new SomeObject();
object.setFirstName(resultSet.getString("FIRSTNAME");
object.setLastName(resultSet.getString("LASTNAME");
tempList.add(object);
result.put(tempId, tempList);
}
return result;
}
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
You need to think about what object types are going in your HashMaps first of all. Suppose you discover the supermost class type is MyClass, then you need to write HashMap<MyClass> everywhere HashMap currently appears. That's the simplest but not necessarily best way to "generify" your code. Similarly for other collections. If you have specific problems, post back with how far you've got and what's then causing you problems.


Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
arunkumar subramanian
Ranch Hand

Joined: Jun 10, 2008
Posts: 32
Thanks. The second method I was able to change it using Generics



The first method, I'm not sure of how to change this. If I use , I get a compilation error when assigning the value. I want to store String/String when there is an Error or Int/ArrayList pair.

public HashMapHashMap<Object, Object> get(int tempId)
{
HashMapHashMap<Object, Object> result = new HashMapHashMap<Object, Object>();
// Assignment is not possible
result = getResultMap(tempId);
if (result.isEmpty() || result == null)
{
result.put("Error", "Search returned no records");
}
return result;
}
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

Originally posted by arunkumar subramanian:



assuming that, this is typo in a declaration of hasp Map object. "HashMapHashMap" ?


Saifuddin..
[Blog][Linkedin] How To Ask Questions On JavaRanch My OpenSource
arunkumar subramanian
Ranch Hand

Joined: Jun 10, 2008
Posts: 32
That was my fault. I edited the code after pasting, forgot to remove it.

public HashMapHashMap<Object, Object> get(int tempId)
{
HashMap<Object, Object> result = new HashMap<Object, Object>();
// Assignment is not possible
result = getResultMap(tempId);
if (result.isEmpty() || result == null)
{
result.put("Error", "Search returned no records");
}
return result;
}
arunkumar subramanian
Ranch Hand

Joined: Jun 10, 2008
Posts: 32


Assignment does not work

How to achieve something like this using Generics

[ June 30, 2008: Message edited by: arunkumar subramanian ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Generics HashMap
 
Similar Threads
Using Generics, HashMap with two different objects as Keys
Generics
generics
Generics