Total members 9951 | Gratitudes |It is currently Sat Feb 11, 2012 10:14 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 1 post ] 
Author Code Snippet
 Code subject: HashMap class with generics
PostPosted: Mon Dec 28, 2009 7:58 pm 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2103
Location: Earth
Has thanked: 39 time
Have thanks: 56 time

HashMap class with generics :
In this example . we see how we reverse the key and value .
Code:

import java
.util.HashMap;
import java.util.Map;

public class 
TestHashMap {

    public static 
void main(String args[])
    {
    
    
TestHashMap hashMap = new TestHashMap();
    
    
HashMap<String,IntegeroldhashMap = new HashMap<StringInteger>();
    
oldhashMap.put("a",1);
    
oldhashMap.put("2"2);
    
    
HashMap<Integer,String>  map hashMap.copyHashMap(oldhashMap);
    
System.out.println("Value of key =1 is : "+map.get(1));
    
    }
    
    public <
VKHashMap<VKcopyHashMap(HashMap<K,VoldhashMap)
    {
    
HashMap<VKnewhashMap = new HashMap<V,K>();
    
    for(
Map.Entry<KVentry :oldhashMap.entrySet())
    {
        
newhashMap.put(entry.getValue(),entry.getKey());
    }
    
    return  
newhashMap;
    }
}
 


The output is :
Value of key =1 is : a

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 1 post ] 
Quick reply


  


 Similar topics
 Topic title   Forum   Author   Comments 
 C C++ program help using class  C-C++  Anonymous  0
 Not to find nativeEncoder class jar  Java  Anonymous  0
 PHP class example  PHP examples  msi_333  0
 concrete class  Java  Anonymous  1
 Having a hard time with this class need help deseperately.  Java  Raven50  1

All times are UTC [ DST ]


Users browsing similar codes

Users browsing this forum: No registered users and 1 guest



Jump to:  
Previous Code Snippet | Next Code Snippet 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team