Solved

Java Generics

Posted on 2007-03-20
6
1,279 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
topping3 challenge 14 70
How can I do a Massively massive community reviewing-and-grading site? 7 70
Weekend adv creator 3 14
oracle 11g 23 51
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now