Solved

Java Generics

Posted on 2007-03-20
6
1,286 Views
Last Modified: 2012-05-05
Hi guys. I have a program which I've successfully implemented that uses Hash maps. The problem is that I have done it the 'old way' and am struggling to update it to Generics.

Rather than try to get you to update my code, I wondered if you could take the example below and show me how that would be implemented in Generics. I know the changes needed are minor.

Thanks.

This example is found at http://www.exampledepot.com/egs/java.util/coll_HashTables.html?l=rel
// Create a hash table
    Map map = new HashMap();    // hash table
    map = new TreeMap();        // sorted map
   
    // Add key/value pairs to the map
    map.put("a", new Integer(1));
    map.put("b", new Integer(2));
    map.put("c", new Integer(3));
   
    // Get number of entries in map
    int size = map.size();        // 2
   
    // Adding an entry whose key exists in the map causes
    // the new value to replace the old value
    Object oldValue = map.put("a", new Integer(9));  // 1
   
    // Remove an entry from the map and return the value of the removed entry
    oldValue = map.remove("c");  // 3
   
    // Iterate over the keys in the map
    Iterator it = map.keySet().iterator();
    while (it.hasNext()) {
        // Get key
        Object key = it.next();
    }
   
    // Iterate over the values in the map
    it = map.values().iterator();
    while (it.hasNext()) {
        // Get value
        Object value = it.next();
    }
0
Comment
Question by:pendulumx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 35

Accepted Solution

by:
TimYates earned 100 total points
ID: 18756888
   Map<String,Integer> map = new HashMap<String,Integer>();    // hash table
    map = new TreeMap<String,Integer>();        // sorted map
   
    // Add key/value pairs to the map
    map.put("a", new Integer(1));
    map.put("b", new Integer(2));
    map.put("c", new Integer(3));
   
    // Get number of entries in map
    int size = map.size();        // 2
   
    // Adding an entry whose key exists in the map causes
    // the new value to replace the old value
    Integer oldValue = map.put("a", new Integer(9));  // 1
   
    // Remove an entry from the map and return the value of the removed entry
    oldValue = map.remove("c");  // 3
   
    // Iterate over the keys in the map
    for( String key : map.keySet() )
    {
    }
   
    // Iterate over the values in the map
    for( Integer value : map.values() )
    {
    }

Should do it...
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18756956
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 25 total points
ID: 18756981
You can also make use of autoboxing:

>> map.put("a", new Integer(1));

map.put ( "a", 1 ) ;
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 35

Expert Comment

by:TimYates
ID: 18757041
*shudder*

;-)
0
 
LVL 1

Author Comment

by:pendulumx
ID: 18757208
Excellent - thank you :)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18757419
:-)
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.

636 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