File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Generics HashMap

 
arunkumar subramanian
Ranch Hand
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 836
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
arunkumar subramanian
Ranch Hand
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1324
Android Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by arunkumar subramanian:



assuming that, this is typo in a declaration of hasp Map object. "HashMapHashMap" ?
 
arunkumar subramanian
Ranch Hand
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Assignment does not work

How to achieve something like this using Generics

[ June 30, 2008: Message edited by: arunkumar subramanian ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic