how to convert Map<Integer, List> to List - java

I have a Map Map<Integer, List> with key, values. I need to convert this into List and pass it to the calling function. Once I have the list i need to convert this back to Map<Integer, List>
The reason I am converting the map to list is because i need to create webservice for this method. As I cannot expose the Map to webservice I need to convert this to list.
How to achieve this..?

A map has two functions called keySet() and values(). They return the keys and values of the map respectively. The keyes are returned as a Set and the values as a Collection.
You can create a list from either of these.
Here is an example:
Map<Integer, List> map = // map creation;
List<Integer> keyList = new ArrayList<Integer>(map.keySet());
List<List> valueList = new ArrayList<List>(map.values());

From what i read you are trying to pass map via list (as your service limitation).
In case you don't find any better solution, you can always use two lists. One for keys and one for values.
(but you are risking breaking map consistency with this approach).

You can create a class with two attributes (Integer, List) and create a List with one object by key.
Also, you can use:
new ArrayList(Map.entrySet());
and convert the resulting set to a list.

To get the values (Strings):
List<List<String>> listVals = new ArrayList<List<String>>(map.values());
To get the keys:
List<Integer> listKeys = new ArrayList<Integer>(map.keySet());

A map contains key value pair but list does not contains key value pair. So u can create a bean class to set key and values and then create list which contains objects of the bean class.

Related

How to add data and iterate Map of maps (Nested map) in Java?

Using Java8/Collections/
I want to add data in Map in collections as:
Map<String,Map<String,Double>> OuterMap=new LinkedHashMap<String,Map<String,Double>>();
Inner map:
Map<String,Double> InnerMap=new LinkedHashMap<String,Double>();
OuterMap.put("Str1",InnerMap);
OuterMap.put("Str2",InnerMap);
The issue is I want to check the keys of InnerMap and it may vary to each other in InnerMap.
When I compare the keys of InnerMap. The OuterMap overrides the previous elements of the map.
I want the output like:
Str1={"SNP1"=0.3,"SNP2"=0.56,"SNP3"="0.76"} , Str2={"SNP1"="0.16","SNP2"=0.56,"SNP3"=0.78,"SNP4"=.56}
You should be creating new map instances when storing the values.
The simplest way to create a copy of the map is to use appropriate copy constructor:
Map<String,Double> innerMap=new LinkedHashMap<>();
outerMap.put("Str1", innerMap);
outerMap.put("Str2", new LinkedHashMap<>(innerMap));
Please also make sure you adhere to Java naming conventions: camelCase for variables/fields/methods, UpperCamelCase for classes/interfaces/enums.

Meaning of List<HashMap<String, String>> nearbyPlacesList

I would like if someone could explain me what is the meaning of this in java
List<HashMap<String, String>> nearbyPlacesList
That means that you have a List containing instances of HashMaps that takes String data as both keys and values.
Oh, and the variable is called nearbyPlacesList.
It's a List which contains HashMap elements with a key of String type and a value of String type.
It is a List consisting of possibly multiple HashMap's where the key-value are Strings.
e.g.
nearbyPlacesList.get(0); // returns first HashMap in the List
nearbyPlacesList.get(0).get("park"); // returns value of the first Map which has the key park
and so on.
A HashMap contains key-value pairs of different kinds (in your case String).
Let's take an example right here. A store. In a store there can be different clothes.
HashMap<String, String> tShirts;
HashMap<String, String> jeans;
where the key-value pair could be for example "Price" and "100€", and they can all be stored in the following list containing different clothes:
List<HashMap<String, String>> clothes;
clothes.add(tShirts);
clothes.add(jeans);
The first piece of information that we see is a list, so we know it's a list. Then we see it is a hashmap (take a look here at the java docs if you are not familiar https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html)
So it's a list of hashmaps, with two strings as the key value pair.
HashMap<String,String>
It uses to store your data as a key and value pair.
When There is more than one HashMap you want to store. you can store all hashmaps into a single list like this.
List<HashMap<String,String>>

ArrayList Object storing multiple values

List<Dictionary> DictionaryList= new ArrayList<Dictionary>();
I created ArrayList object, i need to store multiple name with keys and want to display name along with keys. How can i do it. Please help me
You should consider using Map<K,V> instead.
Considering you need a String to String mapping, a Map can be instantiated as :
Map<String, String> mapOfNames = new HashMap<String, String>();
Insertions are as easy as :
mapOfNames.put(key, name);
And retrievals :
String name = mapOfNames.get(key);
Now considering that there might be multiple values associated with the same key, you will have to modify the Map to a HashMap<String, List<String>>. That is, each key will correspond to a list of values.
So the definition will then be of the form :
Map<String, List<String>> mapOfNames = new HashMap<String, List<String>>();
Insertion :
mapOfNames.get(key).add(name);
Retrieval :
List<String> retrievedNamesForKey = mapOfNames.get(key);
More about HashMaps can be found here : HashMap (Java Platform SE 7 )
I think the object you are looking for is a map
consider the java 7 HashMap
HashMap<String,Value> map = new HashMap<String,Value>();
Insert an object for Value in your implementation.
Add a key value pair... The key is whatever you are using for the value
map.put("name1",23);
Now lets access one of those entries
map.get("name1"); // returns 23
Using parameterized classes is simple once you get the hang of it. See the Docs
Make sure you use objects to specify the pairs
So with this example use Integer() for numbers not int

Java: Setting key of a hashmap explicitly and keeping reference to it

public static HashMap<ArrayList<Integer>, String> map = new HashMap<ArrayList<Integer>, String>();
public static ArrayList<ArrayList<Integer>> keys = new ArrayList<>(map.keySet());
Then in main
map.put(key, "c");
(assume key is a valid ArrayList). But keys still has size 0 after that.
How can I make the relationship of keys stronger so that it will be actually tied to the HashMap and contain all its keys.
The copy constructor of ArrayList copies all the keys in the map to the ArrayList but if you change the map after that point it will not be reflected.
I can think of 3 options:
write your own map implementation that embeds an ArrayList and keeps it up to date
update the ArrayList manually everytime you update the map
don't use an ArrayList at all (keySet() is there when you need to access the keys so I'm not sure why you would need one)
You can't.
Map.keySet() returns the Map's current key set, which you then load into your list. Changes to the map after that have no effect on the contents of the list.
Most people would just re-get the key set if needed. Why don't you just do that?

Get ArrayList element by custom text indexing

What I'm doing is storing classes into an ArrayList and retrieve them by its index number. But are there any list classes in Java where I can retrieve a list element by, lets say, its name? Like this:
ArrayList<string> myArr = new ArrayList<string>();
myArr.add( "ID_name", "String to store" );
ands then retrieve it by:
myArr.get( "ID_name" );
Also, are there any other alternatives to ArrayList? I need a list class to be optimized for:
Random access
Only need to push items into the list
Never need to delete anything from the list
If all you want to store is key-value pairs, and don't care about iteration order, I think you might like the HashMap class:
Map<String, String> map = new HashMap<String, String>();
map.put("foo", "bar");
String bar = map.get("foo"); // bar is "bar"
You can use LinkedHashMap, so it will preserve the order, but you can extract elements by key as in regular map. Though you won't be able to extract entries by index.
An ArrayList is just that: an array. If you want to access values by something else than their indices, look for the various implementations of the Map interface (such as HashMap).
Use a Map<String, String>. In such structure, an element is added with a key. So you can get the element through the key:
Map<String, String> map = new HashMap<String, String>();
map.put("id", "string");
String s = map.get("id"); // s will be equals to "string".
As the other people have mentioned, a HashMap is probably what you want if you don't care about iteration order.
If you do, you can use a LinkedHashMap, which is really a HashMap bolted onto an LinkedList, giving you the best of both worlds: fast random access and preservation of iteration order.
Use a hashmap. You can add elements to a hashmap in much the same way as an arraylist. Also, you can create a set of keys ( 1 elements in the set per (key, value) pair)). You can then iterate over the set of keys.

Categories