Conversion In Collections



Share on Google+Share on Google+

Conversion In Collections
1 Answer(s)      7 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  why we need collections.when we will use collections in our applications
collections  in collections can we perform binarysearch on arraylist elements without sorting them
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
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
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
java collections
achieve this plz without using stringtokenizer with any collections!!!  
Hibernate Criteria With Collections Example
Hibernate Criteria With Collections Example  Good code of Hibernate Criteria With Collections Example. Thanks   Example program of using Hibernate Criteria With Collections in your Java project. Check the tutorial


Advertisement null