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>>
Related
In my code I am using a set of interleaved LinkedHashMaps inside each other as below. The code is fine and gives me the result I want except it automatically removes the duplicates. I couldnt find out how I can use TreeMap or Set in order to keep the duplicates.
LinkedHashMap<String, LinkedHashMap<Integer, LinkedHashMap<String, Vector<String>>>>
dataAll =new LinkedHashMap<String, LinkedHashMap<Integer, LinkedHashMap<String,
Vector<String>>>>();
LinkedHashMap is still a Map data structure. It maps a unique key to a value. If you assign two different values to a key the second value will simply replace the first value assigned to that key.
Also imagine why do you need a Map of duplicated key? The sole purpose of Map is to provide a one to one relationship between key/value pair. It does not handle one to many relationship.
If you have to map a key with a list of values, use something like:
LinkedHashMap<String, List<..>>
This allows you to have one key maps to a list of values.
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?
I store in a HashMap 3 types of object.
HashMap<String, ArrayList<Car>>
['Lorry', [list of lorries]]
['Sport', [list of sport's cars]]
The HashMap string key keeps the type of object (a subclass of Car), the second element stores in array the objects that have e.g. attributes like: ID, date etc.
The four main things I have to do are:
Check if certain ID exist in HashMap when there is no information provided about its type
Print elements of certain ID given the type.
Print all elements of certain type
Print all element from the collection (of different types) if certain attribute that each object has assigned has a Boolean value of e.g. "true";
Is the HashMap the proper structure? I find it problematic if it comes to the first point. It seems like I will have to traverse the whole collection and if so what other collection is better for such requirements?
The basic approach is sound, however since you only want to store each instance once, a Set is a better choice than a List for the map entry value:
Map<String, Set<Car>> typeCache = new HashMap<String, HashSet<Car>>();
The contains() method of HashSet is very fast indeed, so finding if your map contains a particular instance in it values is not going to cost much.
Using two maps would probably be better though - once for each type of lookup, so also use:
Map<String, Object> idCache = new HashMap<String, Object>();
A HashMap is the right data structure for the job, but in your case you might consider using two HashMaps: One holding the relation 'Car Type' -> 'Cars of that Type', and a second one for the relation 'ID' -> 'Car with that ID'.
I couldn't find a better title than that so pardon me for any kind of confusion.
I want to combine two hashmap into one hashmap.I am actually using hashmaps to contain the datas of table where key=coloumName and value= coloumValues.
My code looks something like
HashMap<String, ArrayList> FTMap = table1.getColumn().getColumnValues()
HashMap<String, ArrayList> STMap = table2.getColumn().getColumnValues()
HashMap<String, ArrayList> FinalTableMap = new HashMap()
FinalTableMap.putAll(FTMap)
FinalTableMap.putAll(STMap)
I don't have any problem with column names but the order of coloumvalues are not working after the combining. Since i am using arraylist,i am trying to compare two arraylist and put it in a final arraylist which can be used as the value of the final hashmap.I need some advice or suggestion for this matching between two arraylist.
After you have used addAll to combine your lists, you can use Collections.sort(List, Comparator) to sort your ArrayList. In your Comparator's compare function, you will compare the two objects using the values you wish to sort by (you mentioned serial id?).
See Sort ArrayList of custom Objects by property for a brief example.
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.