Converting integers to strings in a String, Object hashmap - java

If I have a HashMap<String,Object> that has three entries that are <String,String> and one that is <String,Integer> is there a way to "cast" this into a HashMap<String,String> easily? Right now I and making a new HashMap and copying all of the values over so that I can convert the Integer during the copying.

Should be able to cast it, but expect an exception when trying to retrieve one of the integers out of the map.
What I think you're asking is to cast the content of the hashmap, in this case the values, so they all come out as strings, and that's not going to happen.
Best solution is to convert the integer into a string when populating the map in the first place.

You can cast the original map, no need to copy:
map.put("intKey", map.get("intKey").toString());
Map<String, String> fixedMap = (Map) map;
It does look like something is wrong with your design though. It would be better to fix the code that shoves the integer into the map to begin with, so that you would not need to deal with this trickery downstream.

Something like this should be easier:
Class<? extends Object> className = obj.getClass();
if (className.getName().contains("String")){
//add the object
}else {
//convert it to String and then add to HashMap
}

If you're using java 8, you can use forEach with BiConsumer.
Take a look in the code below
Map<String, Object> map = new HashMap<>();
map.put("a", "a");
map.put("b", "b");
map.put("1", Integer.valueOf(1));
map.forEach((k, v) -> map.put(k, v.toString()));
After line map.forEach((k, v) -> map.put(k, v.toString())); all values in the map are Strings. Of courste that loop is still there, but you are using a language feature/resource.

Related

Adding a value to the front of a TreeMap

Previously I had the TreeMap that is sorted via:
Map<String, Set<Stuff>> map = new TreeMap<>(String::compareToIgnoreCase);
I'm thinking of adding a special case. If the string is "Front", I want to ignore this ordering and just have it in the beginning of the map.
Map<String, Set<Stuff>> map = new TreeMap<>((s1, s2) -> {
//do a check to see if the string is "Front", otherwise use the above
});
This feels a bit convoluted to me... Is there a simpler way to do this?

How to get guava's Splitter.split to not return an unmodifiable map

Seems as if the Splitter.MapSplitter method returns an unmodifiablemap. Is there no way to get this to not return an unmodifiablemap so I can add to that map later? Or must I take the unmodifiable map and copy it into a regular map to add?
Map<String, String> testParametersMap = new HashMap<String, String>();
testParametersMap = Splitter.on(",").withKeyValueSeparator(":").split(browser:chrome,browser-version:56,language:english);
testParametersMap.putIfAbsent("country", "US");
The last line throws an exception...
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.Collections$UnmodifiableMap.putIfAbsent(Collections.java:1515)
Copy the result to an implementation of your choice
Guava's Splitter always return immutable collections, so you can't make it return something mutable. Instead you should copy its result to a mutable Map implementation.
Map<String, String> testParametersMap = new LinkedHashMap<>(Splitter.on(",").withKeyValueSeparator(":").split("browser:chrome,browser-version:56,language:english"));
testParametersMap.putIfAbsent("country", "US");
Or, similarly to what you wrote, you should then use putAll instead of reassigning the variable:
Map<String,String> testParametersMap = new LinkedHashMap<>();
testParametersMap.putAll(Splitter.on(",").withKeyValueSeparator(":").split("browser:chrome,browser-version:56,language:english"));
testParametersMap.putIfAbsent("country", "US");
You can use any implementation, like HashMap, TreeMap, LinkedHashMap and others. As Xaerxess mentions in the comments, favor LinkedHashMap as it preserves the order or the elements, if that's any importance for you.

Casting raw Map to Map<Object, Object>, will there be any issue?

I need to convert raw Map to Map<string,string>, and I think I have to first convert the raw map to Map<Object,Object> and then convert it again to Map<String,String>.
code snippet goes like below.
Map obj1 = new HashMap();
obj1.put("key1", 1);
obj1.put("key2", false);
obj1.put("key3", 3.94f);
Map<Object, Object> obj2 = obj1;
Map<String, String> obj = new HashMap<String,String>();
for (Map.Entry<Object, Object> entry: obj2.entrySet()) {
obj.put(entry.getKey().toString(), entry.getValue().toString());
}
I guess it would work in any condition but I want to hear from others about possible danger of this code.(any possiblities for ClassCastException for example?)
Please also let me know if you have a better idea.
-- revised code
Map obj1 = new HashMap();
obj1.put(2, 1);
obj1.put(true, false);
obj1.put(4.4f, 3.94f);
Map<String, String> obj = new HashMap<String,String>();
for (Object k : obj1.keySet()){
obj.put(k.toString(), obj1.get(k).toString());
}
Since raw Map entries will contain key/value of Objects anyway, I think I don't need temporary Map<Object,Object>. Just iterating over each item works well and I don't see any issues so far.
If You Look out the Definition of HashMap in jdk 1.4 It was earlier Implements using Object Class when generics Concept not came.
When generics is Introduced this object is Replaced with <T>. But If you Still don't use Generics Type Safe then Internally this Statement new HashMap() reflects a instance of <Object, Object>. Better To use directly a
a new HashMap() is better idea. There should no need of Map <Object, Object> obj2.
So, GO For this.. a better approach.
Map obj1 = new HashMap();
obj1.put("key1", 1);
obj1.put("key2", false);
obj1.put("key3", 3.94f);
Map<Object, Object> obj2 = obj1;
Map<String, String> obj = new HashMap<String,String>();
for (Object obj_Entry : obj1.entrySet()) {
Map.Entry entry = (Map.Entry) obj_Entry; // This will Work Fine all Time.
obj.put(entry.getKey().toString(), entry.getValue().toString());
}
Your code will not generate ClassCastExceptions. Actually you are not doing any casting here. You just call the toString() method of every key/value pair to make it a string. As long as toString() returns a valid value of your objects. Your code will be fine.
But your code may produce NullPointerExceptions if your obj1 contain null keys or objects
obj1.put(null, "null value")
Also note that some key collisions may occur if toString() methods return same String value for two keys. This is unlikely but it is possible.

Return HashMap added to List<Object>

I have a problem in JAVA when i'm trying to return a HashMap that I have added to a list of type: List<Object>. I know I can use other type of lists, but I need to use List<Object>
List<Object> listOfObjects = new ArrayList<Object>();
HashMap<String, String> hashmap = new HashMap<String,String>();
hashmap.put("x", "foo");
hashmap.put("y", "bar");
listOfObjects.add(hashmap);
for (int i = 0; i < listOfObjects.size(); i++) {
System.out.println(listOfObjects.get(i));
}
I have added my hashmap to my listOfObject, but how do I get the HashMap from the listOfObject such that I can use the HashMap-commands. fx: hashmap.get("x) and it will return "foo".
Normally i thought i could just write: listOfObjects.get(0).get("x") and it would return "foo" but that does not work.
If anyone know another work around that's find but I just need to use a List.
Normally i thought i could just write: listOfObjects.get(0).get("x") and it would return "foo" but that does not work.
No, it wouldn't - because the type of listOfObjects.get(0) is just Object. How do you expect the compiler to know that it's meant to be a map?
You can use:
HashMap<String, String> map = (HashMap<String, String>) listOfObjects.get(0);
// Use map...
... but be aware that due to the nature of generics in Java, that cast isn't really ensuring that all the key/value pairs in the map are "string to string". The cast would work even if you'd originally used:
Map<Integer, Integer> badMap = new HashMap<Integer, Integer>();
badMap.put(0, 10);
listOfObjects.add(badMap);
You'll get a warning for this, but it's important to understand what it means. It's not clear what your use case is, but if you can make it more strongly typed (perhaps create a new class which contains a Map<String, String>?) that would be good. Is every element of your list going to be a map? If so, why are you using List<Object> rather than a more strongly-typed list? If some elements aren't going to be maps, how can you tell which ones will be? (These are the sort of things you should be thinking about carefully.)
I hope this will help u..
List<Object> listOfObjects = new ArrayList<Object>();
HashMap<String, String> hashmap = new HashMap<String,String>();
hashmap.put("x", "foo");
hashmap.put("y", "bar");
listOfObjects.add(hashmap);
for (int i = 0; i < listOfObjects.size(); i++) {
System.out.println(((HashMap<String, String>)listOfObjects.get(i)).get("x"));
}
Normally as your list is of type of object . so first cast it to HashMap type and then get the value from map
please notice the following code
System.out.println(((HashMap<String, String>)listOfObjects.get(i)).get("x"));

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.

Categories

Resources