Using Collections in Java

package temp;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeSet;
public class CollectionTest {

/**
* @param args
*/

public void treeset()
{
TreeSet<String>ts=new TreeSet<String>();
ts.add(“Jack”);
ts.add(“Jim”);
ts.add(“Kurt”);
for(String str:ts)
{
System.out.println(“String is “+str );
}

}

public void hashset()
{
HashSet<String>ts=new HashSet<String>();
ts.add(“Jack”);
ts.add(“Jim”);
ts.add(“Kurt”);
for(String str:ts)
{
System.out.println(“String is “+str );
}

}

public void hashmaptest()
{
HashMap<String,Integer>hm=new HashMap<String,Integer>();
hm.put(“Jack”, 1);
hm.put(“Jim”, 2);
hm.put(“Kurt”, 3);
int value=hm.get(“Jack”);
Iterator itr=hm.entrySet().iterator();
while(itr.hasNext())
{
Map.Entry pairs=(Map.Entry)itr.next();
System.out.println(“Key and values are “+pairs.getKey() +pairs.getValue());
}

System.out.println(“Value of the hashmap test is “+value);

}
public static void main(String[] args) {
// TODO Auto-generated method stub
CollectionTest t1=new CollectionTest();
t1.treeset();
t1.hashset();
t1.hashmaptest();

}
}

Advertisements

About qainterviews

Software Geek,QA Expert,Blogger
This entry was posted in Java and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s