Conversion In Collections



Share on Google+Share on Google+

Conversion In Collections
1 Answer(s)      8 years ago
Posted in : Java Interview Questions
View Answers

September 11, 2009 at 2:24 PM

Hi Friend,

1)Convert ArrayList To HashMap:

import java.util.*;
import java.lang.reflect.*;

class Data{
private int key;
String value;
public Data(){}
public Data(int key, String value) {
this.key = key;
this.value = value;
public int getKey() {
return key;
public String getValue() {
return value;
public String toString(){
return key + " " + value;
public class ConvertArrayListToHashMap<K> {
public static String put(String str){
if(str != null && (str = str.trim()).length() > 0) {
str = str.substring(0, 1).toUpperCase() + str.substring(1);
return str;
public static String convertFieldToAccessor(String st) {
return "get" + put(st);
public <V> Map<K, V> convertToMap(List<V> list, String keyField) throws Exception {
String accessor = convertFieldToAccessor(keyField);
Map<K, V> map = new HashMap<K, V>();
for(V object : list) {
Method method = object.getClass().getDeclaredMethod(accessor);
K key = (K)method.invoke(object);
map.put(key, object);
return map;
public static void main(String[] args) throws Exception {
List<Data> list = new ArrayList<Data>();
list.add(new Data(1, "A"));
list.add(new Data(2, "B"));
list.add(new Data(3, "C"));
list.add(new Data(4, "D"));
Map<Long, Data> dimensionMap = new ConvertArrayListToHashMap<Long>().convertToMap(list, "key");

2)Convert HashMap To ArrayList:

import java.util.*;

public class ConvertHashMapToArrayList{
public static void main(String []args){
HashMap hashmap = new HashMap () ;
hashmap.put ( new Integer (1),"A" ) ;
hashmap.put ( new Integer (2) ,"B") ;
hashmap.put ( new Integer (3),"C" ) ;
hashmap.put ( new Integer (4),"D" ) ;
ArrayList list = new ArrayList ( hashmap.values () ) ;



Related Tutorials/Questions & Answers:
Conversion In Collections - Java Interview Questions
Conversion In Collections  Hi Friends, can u give code to convert ArrayList to HashMap and HashMap to ArrayList. Thanks Prakash  Hi Friend, 1)Convert ArrayList To HashMap: import*; import java.util.
Collections  Hi, Please send a study material on collections.../java/jdk6/introduction-collections-api.shtml
collections  in collections can we perform binarysearch on arraylist elements without sorting them
collections  why we need collections.when we will use collections in our applications
what is collections?
what is collections?  what is collections
Collections API
Collections API  hello, What is the Collections API?   hi The Collections API is a set of classes and interfaces that support operations on collections of objects
Collections in Java
Collections in Java  What is Concurrent Hash map
Collections in Java
Collections in Java  What is Concurrent Hash map
currency conversion
currency conversion   hi frds.. I wan jsp code to convert currency in different formats??... if u know plz plz plz post it   Please visit the following link:
conversion Applet
conversion Applet  I am a java beginner.................. strong texthow to create JApplet that prompts the user to enter a distance in miles in a JTextField and then converts mile to kilometers and display the result as XX.XX
java collections
java collections  what are all the methods and things added for collections in java5 and java6 please kindly reply me as soon as possible
Version of commons-collections>commons-collections dependency
List of Version of commons-collections>commons-collections dependency
Version of excalibur-collections>excalibur-collections dependency
List of Version of excalibur-collections>excalibur-collections dependency
image conversion tools
image conversion tools  how many tools are available in image conversion at particularlly in java
Introduction to Type conversion in Struts
object and collections. To use type conversion mechanism you need to do...Introduction to Type conversion in Struts Type conversion is a mechanism... this powerful and important facility. In struts the type conversion is an mechanism
Collections Framework
Collections Framework  Sir, We know that both HashMap & Hashtable is using for same purposes i.e Used for storing keys-values pair. But there is some difference between this two class that are 1)Hashtable is synchronized
collections in java
collections in java  please explain me the flow of this program..i m getting o/p as 2 1 1..y not 2 1 it that if we change the i2 value,it doesnt remove. import java.util.*; public class Mapit { public static void main


Advertisement null