Array/list of hashmap? - java

I am coming from the world of Perl programming and am unfamiliar with how one would create a list of hashes in Java.
In perl, creating a list of hashes is easy.
#rows = (
{ food=>'pizza'},
{ drink=>'coke'}
);
Reading it out is just as easy:
foreach my $row (#$rows){
print $row->{food};
print $row->{drink};
}
How would one accomplish something similar in Java? Either with just strings, or also with the possibility of objects as well?

The following is roughly equivalent to your Perl code.
List<Map<String,String>> mapList = new ArrayList<Map<String,String>>();
Map<String,String>> map1 = new HashMap<String,String>();
map1.put("food", "pizza");
Map<String,String>> map2 = new HashMap<String,String>();
map2.put("drink", "coke");
Collections.addAll(mapList, map1, map2);
...
for (Map<String,String> map : mapList) {
System.out.println("food is " + map.get("food"));
System.out.println("drink is " + map.get("drink"));
}
However, as you can see this is a lot more cumbersome than in Perl. Which brings me to the point that it is usually a better idea to do this kind of thing in Java using custom classes instead of associative arrays (e.g. Map instances). Then you can write this as something like:
List<Diet> dietList = new ArrayList<Diet>();
Collections.addAll(dietList, new Diet("pizza", null), new Diet(null, "coke");
...
for (Diet diet : dietList) {
System.out.println("food is " + diet.getFood());
System.out.println("drink is " + diet.getDrink());
}
This approach (using a custom class) is generally more robust, more efficient, and gives you more readable code.

List<Map<String,String>> mapList = new ArrayList<HashMap<String,String>>()
Without code you've tried or more specification I can just provide an example of what you're
looking for. Ask more specifics and I can provide more info.
To iterate over the list:
for (Map<String, String> map : mapList) {
String value = map.get("food");
}

Actually in java is quite long compared to perl :(
To create:
List<Map<String,String>> list = new ArrayList<Map<String,String>>();
Map value1 = new HashMap();
value1.put("foo", "pizza");
list.add(value1);
Map value2 = new HashMap();
value2.put("drink", "coke");
list.add(value2);
To read:
for (Map<String,String> element : list) {
// print keys and values
}
If you want something more dynamic and still have access to java libraries you can use something like groovy (or scala or clojure).

Related

Convert Map to Custom Map

Map<String, String> characterMap = new HashMap<>();
characterMap.put("Manikanta", "Pretty");
characterMap.put("Amulya", "VeryGood");
characterMap.put("Saroja", "Good");
characterMap.put("Vinitha", "Cool");
characterMap.put("Sravani", "Good");
characterMap.put("Sameera", "Good");
/*String key = characterMap.entrySet().stream().filter(entry -> entry.getValue().equalsIgnoreCase("Good")).map(Map.Entry::getKey).findFirst().orElse(null);*/
Map<String, String> comboMap = new HashMap<>();
String newKey = null;
String val1 = null;
String val2 = null;
for (Map.Entry<String, String> entry : characterMap.entrySet()) {
if (entry.getKey().equalsIgnoreCase("Manikanta"))
newKey = entry.getKey();
if (entry.getKey().equalsIgnoreCase("Amulya"))
val1 = entry.getValue();
if (entry.getKey().equalsIgnoreCase("Vinitha"))
val2 = entry.getValue();
}
comboMap.put(newKey, val1 + "_" + val2);
How can I implement this in a lambda expression? Is it really possible to insert existing map data to customize to add new map like the above code?
You can't.
You're attempting to reduce a map with 6 elements into, evidently, a map with a single element in it (if this is an example, and the resulting map will ever contain anything but exactly 1 key of value "Manikanta" -> "VeryGood_Cool", you're going to need to update your example).
streams (I gather that is what you mean by 'in lambda expression') excel at operating on a single element in the stream (so, say, the notion "Saroja"->"Good"), or on how to turn the entire stream into a different collection (such as: Turn the whole thing into a list of keys).
You could write a custom collector; it would not be any shorter than this code is, and would be a lot harder to read. Hence I don't see the point of showing that.
Since you only check on the key values, you don't even need a for loop:
comboMap.put(characterMap.containsKey("Manikanta")?"Manikanta":null,
characterMap.get("Amulya")+ "_" + characterMap.get("Vinitha"));

Java How to access hash map data held in an array

learning Java and have figured out how to store a hashmap in an array. But I can't figure out how to get to the stored data. Here is a simplified version of what I'm doing. I've got as far as displaying the specific array items, but how do I access the hash map stored in the array?
import java.util.*;
public class HelloWorld {
public static void main(String[] args) {
Map<String, String> custOrder = new HashMap<String, String>();
List ordersPlaced = new ArrayList();
custOrder.put("colour", "blue");
custOrder.put("manu", "bmw");
custOrder.put("body", "4x4");
ordersPlaced.add(custOrder);
custOrder = new HashMap();
custOrder.put("colour", "green");
custOrder.put("manu", "merc");
custOrder.put("body", "saloon");
ordersPlaced.add(custOrder);
System.out.println(ordersPlaced.get(0).toString());
}
}
Hope that makes sense. Thanks in advance
Neil
You're already accessing it.
In order to get the iterate on the map's items, you can:
ordersPlaced.get(0).forEach((key, value) -> {
System.out.println("Key is: " + key + ", Value is: " + value);
});
Or, earlier to Java 8, something like:
for (Map.Entry<String, String> entry : ordersPlaced.get(0).entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
}
Please note that you should avoid using raw type list. Instead, you should have:
List<Map<String, String>> ordersPlaced = new ArrayList<>();
And then:
Map<String, String> m = ordersPlaced.get(0);
You know it already.
You can get back the stored map by writing
Map<String, String> placedCustOrder = ordersPlaced.get(0);
And avoid using raw types while using List. Declare your list as
List<Map<String, String>> ordersPlaced = new ArrayList<>();
I would like to know how to access the colour of the data stored in the array at location 0
Since you got the map as I said in the line 1
Map<String, String> placedCustOrder = ordersPlaced.get(0);
String colorVal = placedCustOrder.get("colour");
I strongly suggest you to look through Map documentation before proceeding further.

Initializing a dictionary with a specific set of data cleanly in Java

I am curious how I can more effectively instantiate a dictionary in Java. At present I have passable code, yet I am filling it with data in a very obfuscated fashion.
Is there any way for me to initialize my dictionary similar to this? This is python for the record:
westernCanadaAdjList = { 'BC': ['AB'],
'AB': ['BC', 'SK'],
'SK': ['AB', 'MB'],
'MB': ['SK']
}
I find for presentation purposes that is a whole lot more clear.
My current code in Java:
public class Main {
public static void main(String[] args) {
//Adjacency List representation through a dictionary. Allows fast O(1) lookup time.
Map<String,ArrayList<String>> adjList = new HashMap<String,ArrayList<String>>();
//Adding values for Edmonton
adjList.put("Edmonton", new ArrayList<String>());
adjList.get("Edmonton").add("Neighbour1");
adjList.get("Edmonton").add("Neighbour2");
adjList.get("Edmonton").add("Neighbour3");
//Adding values for Vancouver
adjList.put("Vancouver", new ArrayList<String>());
adjList.get("Vancouver").add("V neighbour1");
adjList.get("Vancouver").add("V neighbour2");
System.out.println(adjList.keySet() +" And Values " + adjList.values());
for (String neighbour: adjList.get("Edmonton")){
System.out.println(neighbour);
}
for (String neighbour: adjList.get("Vancouver")){
System.out.println(neighbour);
}
}
}
Thank you very much!
Note: The original answer is over 8 years old and Java has come a long way since then. As of now I'd recommend:
var map = Map.of(
"BC", List.of("AB"),
"AB", List.of("BC", "SK"),
"SK", List.of("AB", "MB"),
"MB", List.of("SK")
);
This is the best technique I know of:
Map<String, String> myMap = new HashMap<String, String>() {{
put("foo", "bar");
put("key", "value");
//etc
}};
Note the double braces -- this is commonly called double brace initialization.
What you're actually doing is creating an anonymous inner class that extends HashMap, and your new subclass contains an initializer block, in which you can call any arbitrary code that is guaranteed to be executed before the instance can be used.
Also note the 'diamond operator' cannot be used with anonymous classes, for whatever reason.
This is a nice technique for test classes, but I tend to avoid it for production code.
EDIT: Thought I should answer your actual question!
double-brace initialization is probably the best solution in "pure" Java, your Map would specifically look like:
Map<String, List<String>> westernCanadaAdjList = new HashMap<String, List<String>> () {{
put("BC", new ArrayList<String>(){{ add("AB"); }});
put("AB", new ArrayList<String>(){{ add("BC"); add("SK"); }});
put("SK", new ArrayList<String>(){{ add("AB"); add("MB"); }});
put("MB", new ArrayList<String>(){{ add("SK"); }});
}};
... Still not super awesome. Java really does need a Map literal, and it does not have one.
For production code, I'd use a Guava's MultiMap, but honestly populating it with literals isn't much better:
Multimap<String, String> multimap = ArrayListMultimap.create();
multimap.put("BC", "AB");
multimap.put("AB", "BC");
multimap.put("AB", "SK");
multimap.put("SK", "SK");
multimap.put("SK", "SK");
multimap.put("SK", "SK");
I recently faced a similar issue. I represented the data as a 2d array, relatively easy to type and parse, and wrote a utility method to parse it into the data structure. e.g. for your case
static String[][] CANADA_DATA = {
{"BC"," AB"},
{"AB","BC","SK"},
// rest of Canada here
}
Example code
public Map<String, List<String>> parseIt() {
Map<String, List<String>> map = new HashMap();
for (String[] provinceData : CANADA_DATA ) {
String name = provinceData [0];
ArrayList neighbors = new ArrayList(Arrays.asList(provinceData ));
neighbors.remove(0); // remove ourself
map.put(name, neighbors);
}
return map;
}
Obviously you can change the data format and parsing code to fit your specific needs.
I agree with Louis and didn't intend to add anything.
The use of streams in this case allows you to compact the code into one line but I realize this is not an answer to your question (just to closest I could think of).
Map<String, List<String>> adjList = Stream.of(
new SimpleEntry<>("Edmonton", Arrays.asList("E N1", "E N2", "E N3")),
new SimpleEntry<>("Vancouver", Arrays.asList("V N1", "V N2", "V N3")))
.collect(Collectors.toMap((e) -> e.getKey(), (e) -> e.getValue()));
Yes, you can: Parse it as json:
import com.fasterxml.jackson.databind.ObjectMapper;
String json = "{'BC': ['AB']," +
"'AB': ['BC', 'SK']," +
"'SK': ['AB', 'MB']," +
"'MB': ['SK']"
"}";
Map<String, Object> map = new ObjectMapper().readValue(json, HashMap.class);

Concatenating two hashmaps without removing common entires from both the maps

I have two hashmaps, in particular vocabs of two languages say english and german.I would like to concatenate both these map to return a single map.I tried :
hashmap.putall()
But, removed some of the entries which are common in both maps and replace it by single entry only.But i want to keep both the vocabs intact just concatenate those. Is there any method to do it? if not any other way to do. I would prefer any methods in hashmap.
[EDIT]
To make more clear, lets see two maps
at the 500 um die 500
0 1 2 0 1 2
resutls into
at the 500 um die 500
0 1 2 3 4 5
You'll have to write your own custom "putAll()` method then. Something like this would work:
HashMap<String> both = new HashMap<String>(english);
for(String key : german.keySet()) {
if(english.containsKey(key)) {
both.put(key, english.get(key)+german.get(key));
}
}
This first copies the English HashMap. Then puts in all the German words, concatenating if there is a duplicate key. You might want some kind of separator character like a / in between so you can later extract the two.
There isn't anything like that in the Java main library itself, you will have to use something provided by third parties like Google Guava's Multimap, it does exactly what you want, or build something like this manually.
You can download the Guava library at the project's website. Using a multimap is the same as using a map, as in:
Multimap<String,String> both = new ArrayListMultimap <String,String>();
both.putAll( german );
both.putAll( english);
for ( Entry<String,String> entry : both.entrySet() ) {
System.out.printf( "%s -> %s%n", entry.getKey(), entry.getValue() );
}
This code will print all key-value pairs including the ones that are present on both maps. So, if you have me->me at both german and english they would be printed twice.
You cannot do that directly with any Map implementation, since in a map, each key is unique.
A possible workaround is to use Map<Key, List<Value>>, and then do the concatenation of your maps manually. The advantage of using a List for the concatenated map, is that it will be easy to retrieve each of the individual values without any extra fiddling.
Something like that would work:
public Map<Key, List<Value>> concat(Map<Key, Value> first, Map<Key, Value> second){
Map<Key, List<Value>> concat = new HashMap<Key, List<Value>>();
putMulti(first, concat);
putMulti(second, concat);
return concat;
}
private void putMulti(Map<Key, Value> content, Map<Key, List<Value>> dest){
for(Map.Entry<Key, Value> entry : content){
List<Value> vals = dest.get(entry.getKey());
if(vals == null){
vals = new ArrayList<Value>();
dest.put(entry.getKey(), vals);
}
vals.add(entry.getValue());
}
}
Similar to #tskuzzy's answer
Map<String, String> both = new HashMap<String, String>();
both.putAll(german);
both.putAll(english);
for (String e : english.keySet())
if (german.containsKey(e))
both.put(e, english.get(e) + german.get(e));
Slight improvisation of #tskuzzy and #Peter's answer here. Just define your own StrangeHashMap by extending HashMap.
public class StrangeHashMap extends HashMap<String, String> {
#Override
public String put(String key, String value) {
if(this.containsKey(key)) {
return super.put(key, super.get(key) + value);
} else {
return super.put(key, value);
}
}
}
You can use it as so:
Map<String, String> map1 = new HashMap<String, String>();
map1.put("key1", "Value1");
map1.put("key2", "Value2");
Map<String, String> map2 = new HashMap<String, String>();
map2.put("key1", "Value2");
map2.put("key3", "Value3");
Map<String, String> all = new StrangeHashMap();
all.putAll(map1);
all.putAll(map2);
System.out.println(all);
The above prints the below for me:
{key3=Value3, key2=Value2, key1=Value1Value2}
Given the new elements in the question, it seems that what you actually need to use is lists. In this case, you can just do:
List<String> english = ...;
List<String> german = ...;
List<String> concat = new ArrayList<String>(english.size() + german.size());
concat.addAll(english);
concat.addAll(german);
And there you are. You can still use concat.get(n) to retreive the value nth value in the concatenated list.

Two-dimensional ArrayList

Just a very small question... I seem to run into too much complexity here: I have to realize an index-structure like {42, someString}. I tried:
Object entry[][] = new Object[1][1];
ArrayList<Object> my_list = new ArrayList<Object>();
However that looks really strange. Isn't there a better much simpler solution to just store some Integer and a String? I need to perfrom search for the Strings and return the Integer... so I thought Collections and ArrayLists are good friends in the Java API.
Solution: use a Map
Uhm, do you perhaps need a Map?
Map<String,Integer> map = new HashMap<String,Integer>();
map.put("Some String", 42);
// or, more correctly:
map.put("Some String", Integer.valueOf(42));
You can search it using
Integer result = map.get("Some String");
Reference: Sun Java Tutorial > Collection Trail > Interfaces > The Map Interface
Fixing the OP's Code
BTW, the code in the question is flawed. Here's how you would do it if you wanted to use a List of object arrays (which you shouldn't):
// single dimension, not multi-dimension
Object[] entry = new Object[]{"Some String",Integer.valueOf(42)};
// use interface as variable, not implementation type
// generic type is object array, not object
List<Object[]> myList = new ArrayList<Object[]>();
// add array to list
myList.add(entry);
Now you could search like this:
for(final Object[] candidate : myList){
if("Some String".equals(candidate[0])){
System.out.println("Result: " + candidate[1]);
break;
}
}
However, this is just for reference, don't do it this way. The Collections Framework contains solutions for almost all standard cases. Use a Map.
Make a tuple class
public Class IntegerStringTuple {
private Integer number;
private String string;
//setters and getters etc.
}
If I understand correctly you should use a Map.
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(42, "someString");
String str = map.get(42);
Simply use a HashMap
Map<String,Integer> map = new HashMap<String,Integer>();
map.put("foo",42);
why not use a map?
Map<String,Object>
It sounds like you want a Map
I would use a Map. Maps are used to store key value pairs.
Map<String, Integer> map = new HashMap<String, Integer>();
Map may not be used instead of an ArrayList when you require the order to be maintained.
ArrayList arr1 = new ArrayList();
ArrayList arr2 = new ArrayList();
arr2.add(1);
arr2.add(2);
arr2.add(3);
arr1.add(arr2);
for(int i=0;i<arr1.size();i++){
System.out.println("i:"+arr1.get(i));
for(int j=0;j<((ArrayList)arr1.get(i)).size();j++){
System.out.println("j:"+((ArrayList)arr1.get(i)).get(j));
}
}
output: i:[1, 2, 3]
j:1
j:2
j:3
ArrayList<String> lcname = new ArrayList<String>();
lcname.add(cname);
ArrayList<String> lsize = new ArrayList<String>();
lsize.add(size);
Dictionary dictionary = new Hashtable();
Hashtable<String, ArrayList<ArrayList>> hashtable =
new Hashtable<String, ArrayList<ArrayList>>();
hashtable.put(fname, new ArrayList<>());
hashtable.get(fname).add(lcname);
hashtable.get(fname).add(lsize);
System.out.println(hashtable);
Here is the code for dictionaries of list(list).
OUTPUT
{file name=[[column name], [size]]}

Categories

Resources