Solved

Java Generics

Posted on 2007-03-20
6
1,278 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:mayankeagle
ID: 18756956
0
 
LVL 30

Assisted Solution

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

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

map.put ( "a", 1 ) ;
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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:mayankeagle
ID: 18757419
:-)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

708 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

12 Experts available now in Live!

Get 1:1 Help Now