Conversion In Collections

Conversion In Collections

View Answers

September 11, 2009 at 2:24 PM

Hi Friend,

1)Convert ArrayList To HashMap:

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

class Data{
private int key;
String value;
public Data(){}
public Data(int key, String value) {
super();
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");
System.out.println(dimensionMap);
}
}

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 () ) ;
System.out.println(list);
}
}

Thanks









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 java.io.*; import java.util.
Collections
Collections  Hi, Please send a study material on collections.../java/jdk6/introduction-collections-api.shtml http://www.roseindia.net/javacodeexamples/index.shtml http://www.devmanuals.com/tutorials/java/collections/index.html
Advertisements
collections
collections  in collections can we perform binarysearch on arraylist elements without sorting them
collections
collections  why we need collections.when we will use collections in our applications
what is collections?
what is collections?  what is collections
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: http://www.roseindia.net/tutorials/I18N/currency
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
Collections API
Collections API  hello, What is the Collections API?   hiADS_TO_REPLACE_1 The Collections API is a set of classes and interfaces that support operations on collections of objects
conversion
conversion
Collections in Java
Collections in Java  What is Concurrent Hash map
Collections in Java
Collections in Java  What is Concurrent Hash map
image conversion tools
image conversion tools  how many tools are available in image conversion at particularlly in java
ModuleNotFoundError: No module named 'conversion'
ModuleNotFoundError: No module named 'conversion'  Hi, My Python... 'conversion' How to remove the ModuleNotFoundError: No module named 'conversion' error? Thanks   Hi, In your python environment you
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
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
Number conversion in JavaScript
Number conversion in JavaScript  How do you convert numbers between different bases in JavaScript
conversion of language in a project
conversion of language in a project  how can we convert the database code in the project dynamically
.wav file conversion
.wav file conversion   how to read .wav file and converd the data into text format
NSString to float conversion
NSString to float conversion  HI, I my iPhone and iPad application I have to convert NSString to float. Provide me example of NSString to float conversion. Thanks   Hi, Please see NSString to float conversion
object conversion - Java Beginners
object conversion   Hi, Can anybody tell me the object conversion in java.   Hi sandeep Can u please tell me in details about your.../java-conversion/ObjectToDouble.shtml Thanks... sandeep kumar suman
Version of commons-collections>commons-collections-testframework dependency
List of Version of commons-collections>commons-collections-testframework dependency
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
Conversion - Development process
is the theory of conversion from java to .NET? How it is done?   Hi... Conversion Assistant (JLCA). Code that calls Java APIs can convert to comparable C# code... to a topic with guidelines for modifications needed to finish the conversion
NSString to float conversion
NSString to float conversion  HI, I my iPhone and iPad application I have to convert NSString to float. Provide me example of NSString to float conversion. Thanks   HI, Use the following code: NSScanner *strXpos
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 0..is it that if we change the i2 value,it doesnt remove. import java.util.*; public class Mapit { public static void main
collections
collections
collections
collections
collections
collections
Collections
collections
collections
collections
Collections
collections
collections
collections
collections
Collections
collections
collections
data science conversion course
data science conversion course  Hi, I am beginner in Data Science... science conversion course Try to provide me good examples or tutorials links so that I can learn the topic "data science conversion course". Also tell
Version of cn.featherfly>featherfly-conversion dependency
List of Version of cn.featherfly>featherfly-conversion dependency
Version of com.groupdocs>groupdocs-conversion dependency
List of Version of com.groupdocs>groupdocs-conversion dependency
Version of com.ncona>conversion-graph dependency
List of Version of com.ncona>conversion-graph dependency

Ads