Home Answers Viewqa Java-Interview-Questions Conversion In Collections

 
 


Rajendra
Conversion In Collections
1 Answer(s)      3 years and 8 months ago
Posted in : Java Interview Questions

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 Pages:
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
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
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
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
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 0..is 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!!!  
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 - Java Interview Questions
collections   what is the way to insert map values into a set in collections
Collections Overview
Java NotesCollections Overview Summary of Collections interfaces Most... that the classes implement. Collections - This is a basic set of methods for working... inheritance. The most useful classes are in bold. Collections // Contains may
collections in java - Java Interview Questions
collections in java  What is collection
Collections (Data Structure Library)
Collections (Data Structure Library) Predefined Libraries Standard... Vector) ver 1.2-1.4: Collections without generics (templaces). Java 5 (1.5... packages fill gaps in Collections, eg, Apache Jakarta Commons Collections
collections - Java Interview Questions
collections  The Java Collections API  Hi friend, Java Collections of API (Application Programming Intreface) Consists of several.../java/jdk6/introduction-collections-api.shtml Thanks
image conversion tools
image conversion tools  how many tools are available in image conversion at particularlly in java
wt are the collections in java
wt are the collections in java  plese send me the reply   Hi Friend, Java Collections API (Application Programming Interface) Consists of several interfaces, and classes within the java.util package. It provides tools
Collections - Java Beginners
Collections  I want to know about the new for loop used in collections... can anyone explain in short..? If possible can you give some sites..., http://www.roseindia.net/java/jdk6/introduction-collections-api.shtml
collections - Java Interview Questions
in Array of type Integer. Collections : ArrayList al = new ArrayList
creating own ArrayList without Collections?
creating own ArrayList without Collections?  How can we write our own arraylist without using collections in JAVA
java Collections - Java Beginners
java Collections   HI Friends, I want to pick a random element from a set.How can i achieve this.I have a set of objects , I want to pick a random object every time from the set..  Hi Friend, Try

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.