I want hashmap to array
I create the hashmap
Map<Integer,File> selectedFiles = new Hashmap<>();
and put it some Map data
And I convert this hashmap's values to array so
File[] files = (File[]) selectedFiles.values().toArray();
But errors occur;
java.lang.Object[] cannot be cast to java.io.File[]
I know that when I want the hashmap's values to array, use .values.toArray() but maybe it is not corret;
This way is wrong?
Map<Integer, File> selectedFiles = new HashMap<>();
selectedFiles.put(1, null);
File[] files = selectedFiles.values().toArray(
new File[selectedFiles.size()]);
System.out.println(files);// We will get the object [Ljava.io.File;#15db9742
arrays. toArray without parameters creates an object array because the type information of the list is lost at runtime.
Please use below Code to convert HashMap to Array ,You can change String to File in your case.
//Creating a HashMap object
HashMap<String, String> map = new HashMap<String, String>();
//Getting Collection of values from HashMap
Collection<String> values = map.values();
//Creating an ArrayList of values
ArrayList<String> listOfValues = new ArrayList<String>(values);
// Convert ArrayList to Array
String stringArray[]=listOfValues.toArray(new String[listOfValues.size()])
Related
Why we add hash map in array list.For example
HashMap<String,String> hashMap = new HashMap<String,String>();
hashMap.put("1","bmw");
hashMap.put("2","mercedez");
hashMap.put("3","audi");
hashMap.put("4","sunny");
ArrayList<HashMap<String,String>> arrayList =new ArrayList<HashMap<String,String>>();
arrayList.add(hashMap);
Can we directly fetch data from hashmap like hashMap("1"); like this
We can directly fetch data from hashmap like hashMap.get("1");
I have HashMap with jsonobject I want to store all those values into JSONArray.
Is it possible ?
Please help me.
Suppose you have two json objects stored in hashmap :
HashMap<String, JSONOnject> stringJson = new HashMap<String, JSONObject>();
stringJson.put("key", jsonobject1);
stringJson.put("key1", jsonobject2); //remember the hashmap structure, its key value structure.
You can access this jsonObject by get method of hashmap:
JSONArray jarray = stringjson.get("key").getJsonArray("json array name"); //stringjson.get("key") will return jsonObject you stored.
But you can do this directly without saving json object to hashmap.
In arrayList I can add n number of values like the below code
RowOneCollection = new ArrayList<Button>((Arrays.asList(btn1,btn2,btn3,btn4)));
Similarly in hash map I am adding n number of vales like this
Map<String, String>map= new HashMap<String, String>();
map.put("bi","biology");
map.put("ma","maths");
like this I am adding n number of values.How I can add all the values in single line like the arraylist
You can put the data using For loop if you have 2 String arrays consisting of Keys and Values.
String[] keys = {"a","aa","aaa"}; //keep both array of same size.
String[] values = {"a1","aa2","aaa3"};
Map<String, String> map = new HashMap<String, String>();
for(int i = 0 ; i < keys.length; i++)
{
map.put(kays[i], values[i]);
}
Hope this helps.
You probably want to adjust how you are populating your HashMap as follows:
HashMap<String,xyz> hMap=new HashMap<String,xyz>();
for(int i=0;i<list.size();i++)
{
hMap.put("Data"+i, list);
}
Where xyz is the object, that you are working with. This approach allows you to take advantage of Java's Generics capability in its Collection objects such as HashMap.
public ArrayList<HashMap<String,String>> c_tmp= new ArrayList<HashMap<String,String>>();
public HashMap<String,String>mapa=new HashMap<String, String>();
String[] name_Val = null;
//i fill the map and the array list in a while()
mapa.put(numberz,nnn);
c_tmp.add(mapa);
and an error occurs( force close)when trying to fill the next line name_Val=....
name_Val = (String[]) c_tmp.toArray(new String[c_tmp.size()]);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,name_Val);
txtPhoneName.setAdapter(adapter);
/// am I writing that line wrong ( because i've copied it from ArrayList<String>)
You should try to do it first with a map and when you initialize it then to hash map, and use a simple adapter
mAdapter = new SimpleAdapter(this, peopleList, R.layout.row ,new String[]
{ "Name", "Phone" }, new int[] { R.id.text1, R.id.text2 });
txtPhoneName.setAdapter(mAdapter);
But before all in another function populate the list of people...
What I would suggest is, you can simply use a HashMap<String,String> Data type as map has the property of adding values for a unique key
After you are done with storing the Data in the hashmap you can use the Iterator to iterate and get all keys from the HashMap and thus the Values from all those keys which shall be Stored in an ArrayList , and then converting it to your String Array would not be a problem for you.
Hi friends i got stucked into these problem at the last stage of my program.Here is some description about my project:i am calling a web service with the help of Ksoap and getting the JSON response from the server than,i am parsed that response and store it into the correspondent arraylist.Till here everything is working fine.Now the problem starts here i want to store all these AppID,AppName,AppTabID,Icon,Tabname into a multimap with same key for the same index.How can i achieve that ?Any help would highly appreciated!
private SoapPrimitive response;
ArrayList<String> AppID = new ArrayList<String>();
ArrayList<String> AppName = new ArrayList<String>();
ArrayList<String> AppTabId = new ArrayList<String>();
ArrayList<String> Icon = new ArrayList<String>();
ArrayList<Drawable> drawables=new ArrayList<Drawable>();
ArrayList<String> TabName = new ArrayList<String>();
<!--here are the Arraylist declaration->
public String parse(String a) throws Exception {
JSONArray jsonArry1 = new JSONArray(res); // create a json object from a string
// JSONArray jsonEvents = jsonObj.optJSONArray("AppItems"); // get all events as json objects from AppItems array
System.out.println("Length of array for AppItem tag is.. "+jsonArry1.length());
for(int i = 0; i < jsonArry1.length(); i++){
JSONObject event = jsonArry1.getJSONObject(i); // create a single event jsonObject
String AppID=event.getString("AppId");
System.out.println("AppId is "+AppID);
String AppName=event.getString("AppName");
System.out.println("AppName is "+AppName);
String AppTabId=event.getString("AppTabId");
System.out.println("AppTabId is "+AppTabId);
String Icon=event.getString("Icon");
System.out.println("Icon is "+Icon);
TabHtml=event.getString("TabHtml");
System.out.println("TabHtml = "+TabHtml);
}
System.out.println("return"+TabHtml);
return TabHtml;
}
the above code i am using for saving all the content into arraylist object.From here i want to set all these diferent Arraylist into same MultiMap.How can i achieve that?
I'd just define an AppData container class to hold all data related to a single response, and then just put AppData objects into your multimap.
On this link (Java Map Interface) search for multimap which gives a straight forward implementation of a multimap.
However from what I understand of your question, you'll have to put all your Lists into a List and that would go into a MultiMap (which will just be a HashMap<String,ArrayList<ArrayList>>).
It is not very good approach to have all these different lists for different fields in response, but as you have implemented almost completed, I would lead this approach further:
You can have a Map into map to resolve this prob:
All you need to have follow the below loop:
HashMap<Integer, HashMap<String, String> parsed=new HashMap<Integer, HashMap<String, String>();
for(int i=0;i<AppID.size();i++)
{
HashMap<String, String> keyValues= new HashMap<String, String>();
keyValues.put("id", AppName .get(0));
keyValues.put("id", AppTabId .get(0));
keyValues.put("id", Icon .get(0));
.....
parsed(new Integer(i), keyValues);
}