Solved

Java Generics

Posted on 2007-03-20
6
1,282 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
  • 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.

808 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