This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 6 years ago.
I'm newbie with Java
Is it possible to convert a String, or a String[] as the keys of an Hashmap?
I mean:
String keysToConvert = "key1,key2,key3";
into
HashMap<String, Double> hashmap = new HashMap<String, Double>();
[apply in some way the keys of the string]
hashmap.get("key2");
I know that hashmap.get("key2");has no value in this moment. I just would know if there is a way to load the keys in an HashMap.
hashmap.put("key2", 1.0d);
System.out.println(hashmap.get("key2")); // prints 1.0
This is the basic usage of a Map
If you have String[], you can add them using a for-each loop:
String[] keys = {"key1", "key2", "key3"};
HashMap<String, Double> hashmap = new HashMap<String, Double>();
for (String key : keys) {
hashmap.put(key, null);
}
Related
This question already has answers here:
How does one convert a HashMap to a List in Java?
(8 answers)
Closed 5 years ago.
I am getting all the keys in a map into a HashSet.
I want to be able to transfer this into an ArrayList.
Did you mean this :
Map<K, V> map = new HashMap<>();
List<K> list = new ArrayList<>(map.keySet());
It appears you are already aware that Set<KeyType> set = map.keySet() returns a Set of the keys:
to put these into an ArrayList<KeyType> you can simply do new ArrayList(set);
Map<String,String> mymap = new HashMap<>();
// add data to the map
String[] array = mymap.keySet().toArray(new String[0]);
Why not use the addAll method?
Set<KeyType> keys = map.keySet();
List<KeyType> res = new ArrayList<>();
res.addAll(keys);
No?
Arrays.asList(map.keySet().toArray());
Would be a good place to start
This question already has answers here:
How to create a HashMap with two keys (Key-Pair, Value)?
(14 answers)
Closed 6 years ago.
Can I have a hash map in Java that looks like this?
HashMap<String, String, Integer> hmap = new HashMap<String, String, Integer>()
My question is similar to this one hereQuestion
I'm a newbie to Java. So what I want to know is, what would be the best data structure to use if I need something like above, if that is not valid?
Create a simple class holding two String objects:
public class MyKey {
private String a;
private String b;
// ... accessors, mutators etc.
}
And then use it's objects as keys in your map:
HashMap<MyKey, Integer> hmap = new HashMap<>()
Later, to add a new entry:
hmap.put(new MyKey("a", "b"), 2);
This question already has answers here:
Intersection of java.util.Map
(6 answers)
Closed 8 years ago.
I have two HashMaps, say HashMapA and HashMapB. What would be an efficient way of finding keys that exists in both HashMaps? My current implementation looks like this:
Integer key;
/* create iterator */
Iterator<Map.Entry<Integer, Foo>> it = HashMapA.entrySet().iterator;
/* iterate through HashMapA using iterator*/
while (it.hasNext()) {
key = it.next().getKey();
if (HashMapB.containsKey(key)) {
/* matching key found */
System.out.println("Got one: " + key);
}
}
This appears to work, but looks quiet inefficient. Is there something like
Integer keyInBothMaps = HashMapA.containsKeyOf(HashMapB);
?
You are looking at the keys of the map so start with the keySet();
You can then look at the Set interface and see the method retainAll
http://docs.oracle.com/javase/8/docs/api/java/util/Set.html#retainAll-java.util.Collection-
This gives you:
map1.keySet().retainAll(map2.keySet())
However that will modify the map so you should copy the set:
new HashSet<>(map1.keySet()).retainAll(map2.keySet())
You can use Set.retainAll.
Here's an ugly example:
Map<String, String> m0 = new HashMap<String, String>();
m0.put("a", "a");
m0.put("b", "b");
Map<String, String> m1 = new HashMap<String, String>();
m1.put("c", "c");
m1.put("b", "b");
Set<String> s = new HashSet<String>(m0.keySet());
s.retainAll(m1.keySet());
System.out.println(s);
Output
[b]
This question already has answers here:
Sort a Map<Key, Value> by values
(64 answers)
Closed 9 years ago.
For example you have a HashMap<String, String>
how can you sort values in the hashmap and print them to console
what would be the best ways to do it?
This should do it:
Map<String, String> asso = new HashMap<String, String>
// add some tuples to asso
List<String> values = new ArrayList(asso.values());
Collections.sort(values); // assumes an appropriate comparator implementation for the value type
There isn't a way to retrieve a HashMap tuple by value sorted order afaik.
This question already has answers here:
Does Java have a HashMap with reverse lookup?
(7 answers)
Closed 9 years ago.
I have a hash map and values in it. Now i want to set the values in the map as keys and keys as values. Can anyone suggest any idea?
My Map is
Map<String, String> col=new HashMap<String, String>();
col.put("one","four");
col.put("two","five");
col.put("three","Six");
Now i want to create an another map and put it in other way as i told above. ie,
Map<String, String> col2=new HashMap<String, String>();
col.put("five","one");
col.put("four","two");
col.put("Six","three");
Anybody has idea? Thanks
Like so:
Map<String, String> col2 = new HashMap<String, String>();
for (Map.Entry<String, String> e : col.entrySet()) {
col2.put(e.getValue(), e.getKey());
}
Assuming your values are unique in your hashmap, you can do like this.
// Get the value collection from the old HashMap
Collection<String> valueCollection = col.values();
Iterator<String> valueIterator = valueCollection.iterator();
HashMap<String, String> col1 = new HashMap<String, String>();
while(valueIterator.hasNext()){
String currentValue = valueIterator.next();
// Find the value in old HashMap
Iterator<String> keyIterator = col.keySet().iterator();
while(keyIterator.hasNext()){
String currentKey = keyIterator.next();
if (col.get(currentKey).equals(currentValue)){
// When found, put the value and key combination in new HashMap
col1.put(currentValue, currentKey);
break;
}
}
}
Create another Map and iterate through keys/values one by one and put in new Map. finally delete old one.