I thought this would be easy but I am struggling...I want to send 2 Strings to a method and have one as the map key and the other as the name of the set;
Edit: Map declaration;
Edit 2 to add constructor for map;
public HashMap<String, Set<String>> playerMap; //edit 1
public Planets()
{
playerMap = new HashMap<>();
} //edit 2
public void addMapEntry(String newPlayerAdd, String newPlanetAdd)
{
playerMap.put(newPlayerAdd, newPlanetAdd);
}
and am getting error message
Argument mismatch
java.util.Map.put(java.lang.String,jaav.util.Set) is
not applicable; (....) cannot be converted to (...).
I have created the map and previous sets in this same class and all have been added fine...
I have changed the code to
public void addMapEntry(String newPlayerAdd, Set<String> newPlanetAdd) {
planetStats = new TreeSet<>(newPlanetAdd);
playerMap.put(newPlayerAdd, newPlanetAdd);
}
And am getting a runtime error of the same type instead of a compile time error;
I feel I am not passing the variable in the correct format to the method...any help appreciated...
currently sending in the form of
allPlanets.addMapEntry("Jane", "Klethron");
You should declare your map as
Map<String, Set<String>> playerMap = new HashMap<String, Set<String>>()
in case you want to use HashMap implementation.. and should call the method as
Set<String> set = new HashSet<String>();
set.add("Klethron");
allPlanets.addMapEntry("Jane", set);
Declare your Map as
Map<String, Set<String>> playerMap = new HashMap<String, Set<String>>()
Initialize your Set
Set<String> setString = new HashSet<String>();
setString.add("String1"); //fill up the string set in this way
allPlanets.addMapEntry("Jane", setString);
Related
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.
I'm trying to retrieve values from a nested hashmap depending on user selection. I'm having trouble getting the values from the nested Hashmap.
I've created the HashMap like this
private Map<String, Map<String, String>> contentTypesMap = new HashMap<String, Map<String, String>>();
If the user selects MDX i want to go to the nested HashMap and get the two string values in the 2nd hashmap.
contentTypesMap.put("MDX", new HashMap<String,String>()).put("HELLO1", "FOO");
And this my function for where to use hashmap,
public void getDatabaseSelectionValues (){
resourceType = (String) FileUtilityScreen.contentTypes.getSelectedItem();
sqlTableName = contentTypesMap.get(resourceType).get(key);
languageSelected = FileUtilityScreen.languagesFromDataBase.get(
FileUtilityScreen.languageDropDown.getSelectedItem());
}
with your exemple, if the selection is "MDX", you get your second map by doing
HashMap<String,String> selection = contentTypesMap.get("MDX")
then you can use entrySet() method to retrieve all entries of your map and iterate over entries.
See the documentation from Map.put(K key, V value)
#return the previous value associated with key, or
null if there was no mapping for key
So calling contentTypesMap.put("MDX", new HashMap<String,String>()) will always return the value that is not in the contentTypesMap map anymore.
Solution :
HashMap<String, String> map = contentTypesMap.get("MDX");
if (map == null) {
map = new HashMap<>();
contentTypesMap.put("MDX", map);
}
map.put("HELLO1", "FOO");
Or you can use guava Multimap instead.
Is the HELLO1 and FOO meant to be the value for MDX?
You are not putting it correct. Try this:
Map<String, String> value = new HashMap<String, String>();
value.put("HELLO1", "FOO");
contentTypesMap.put("MDX", value);
I'd also consider wrapping this outer map in its own object as it may get complex.
What I meant by a wrapper class in my comment above was something along these lines, to make it more readable. I'm assuming the key in the outer map is the table name, and in the inner map is the column name and data type for that column.
public class DatabaseTables {
private Map<String, Map<String, String>> dbTables = new HashMap<String, Map<String, String>>();
public void addColumn(String tableName, String column, String type){
Map<String, String> columns = dbTables.get(tableName);
if(dbTables.get(tableName) == null) {
columns = new HashMap<String, String>();
}
columns.put(column, type);
dbTables.put(tableName, columns);
}
public Map<String,String> getColumnsForTable(String tableName){
return dbTables.get(tableName);
}
}
That still feels ugly though. I think a better solution than this, would be to have a class called Table, with a string property for the table name, and a map for its columns and data types.
And then, elsewhere, you could have a list or a set of type Table.
Edit: Or, in Table.java, rather than a map for the columns, you could have a list or set of type Column. And then create a column class with two fields, one for the column name and the other for the type.
I want to do something like in this example. I can't figure out why it is not working.
myMap has no value in the main class. But if I put the value in the "putSomethingInTheMap" into the map it has the right value in the main class.
Can you give me a suggestion how to handle something like this?
public static void main(String[] args) {
Map<String, Integer> meineMap = new HashMap<>();
int wert = 1;
putSomethingInTheMap(meineMap, wert);
System.out.println(meineMap.get("A"));
}
private static void putSomethingInTheMap(Map<String, Integer> myMap, int value) {
myMap = insert(value);
}
private static Map<String, Integer> insert(int wert) {
Map<String, Integer> map = new HashMap<>();
map.put("A", wert);
return map;
}
private static void putSomethingInTheMap(Map<String, Integer> myMap, int value) {
myMap.put("A", wert);
}
Basically you are doing it method pass by refrence . In main class your creating a map and passing it to putSomethingInTheMap where it is assigned by the map returned by insert.
So if you have value in main class it is due to refrence passed to method.
Because meineMap reference is same as original. You declared a new reference with new value in insert method, and java method call is pass by value, but you are pass a reference that passed as value in parameter of putSomethingInTheMap!
private static Map<String, Integer> insert(int wert) {
Map<String, Integer> map = new HashMap<>();
map.put("A", wert);
return map;
}
This part is the mistake because what you are doing here is, for every insert operation in a map you are creating a new map ( that will be empty ofcourse) and adding a value in this map and returning this map.
Now
myMap = insert(value);
call will always get a map with only 1 value every time he makes an insert operation.
Fix:
You don't need to create a new map in insert( int wert ), you just need to call put() of java map. Code for solution is already posted by #Maurice Perry
I have got some troubles converting each value in my HashMap to a String.
private static HashMap<String, List<Music>> musiksammlung = new
HashMap<String, List<Music>>();
This is my constructor for the HashMap. The key represents the album, the value a list of tracks from this album.
Now I want to convert each Music object to a String without creating a new HashMap, is this
possible?
I've tried it with the Iterator scheme, for loop over the entry set and so on but nothing seems to work.
Edit://
My code for the convertmethod:
public HashMap<String, List<String>> generateFormatList() {
HashMap<String, List<String>> formatList = new HashMap<String, List<String>>();
for(String key : musiksammlung.keySet())
formatList.put(key, musiksammlung.get(key).toString());
return musiksammlung;
}
But this always results in an error "is not applicable for the Arguments (String, String) so I have no idea. Do I have to override toString()?
You're on the right path but you need to convert the existing List<Music> to a List<String> and put the List<String> into your new HashMap.
You also then want to return your newly created HashMap<String, List<String>> instead of your original one.
public HashMap<String, List<String>> generateFormatList() {
HashMap<String, List<String>> formatList = new HashMap<String, List<String>>();
for(String key : musiksammlung.keySet()) {
// Value to store in map
List<String> value = new ArrayList<String>();
// Get the List<Music>
List<Music> musicList = musiksammlung.get(key);
for (Music m: musicList) {
// Add String of each Music object to the List
value.add(m.toString);
}
// Add the value to your new map
formatList.put(key, value);
}
// Return the new map
return formatList;
}
So answer your question:
Now I want to convert each Music object to a String without creating a
new HashMap, is this possible?
You need to create a new HashMap, because it's storing different type of value: List<Music> is different from List<String>.
Also as mentioned in my previous answer, make sure you override Music.toString() so that it returns a meaningful String for you instead of the one it inherits from its parent classes, which includes at least java.lang.Object
formatList wants a List<String>, but musiksammlung.get(key).toString() returns a String (not a List<String>). Did you mean this?
HashMap<String, String> formatList = new HashMap<String, String>();
Have you tried something like this:
Iterator<String> it = musiksammlung.keySet().iterator();
while (it.hasNext()) {
List<Music> ml = musiksammlung.get(it.next());
for (Music m : ml)
System.out.println(m.toString());
}
And of course you should override the Music#toString() method with something you could use.
Try to change your HashMap like this:
private static HashMap<String, List<Object>> musiksammlung = new HashMap<String,List<Object>>();
So you can save any kind of objects in this HashMap. Also use instanceof to check the type of the object before using it.
I am learning Hashmaps in Java, so I have a simple java program that creates an account. My problem is when it comes to storing the new accounts in a collection, I am trying to do it using a hashmap but just can't figure out where to go.
HashMap<String,CurrentAccount> m = new HashMap<String,String>();
if (Account.validateID(accountID)) {
CurrentAccount ca = new CurrentAccount(cl,accountID, sortCode, 0);
I am unsure of the next stage to add this account to the hashmap I have tried a couple of different ways but always end up with an error.
You have an error with your instantiation statement. The map's type is HashMap<String, CurrentAccount>, but you are instantiating HashMap<String,String>.
To fix this, change your instantiation statement to correspond to the map's type, like the following:
HashMap<String, CurrentAccount> m = new HashMap<String, CurrentAccount>();
Or if you are using JDK 1.7+, you could use diamond notation instead (see Generic Types for more information):
HashMap<String, CurrentAccount> m = new HashMap<>();
In order to add items to the map, you can use Map#put(K, V):
m.put(accountID, ca);
In order to get a value, you can use Map#get(Object):
CurrentAccount ca = m.get(accountID);
See JDK 1.7 Map documentation for more information about maps.
As for the question made by the OP in the comments of this answer, in order to access the map (or any other type) in multiple methods, it has to be declared as a class field:
public class TestClass {
Map<String, CurrentAccount> accountMap;
public TestClass() {
accountMap = new HashMap<String, CurrentAccount>();
}
public void method1() {
// You can access the map as accountMap
}
public void method2() {
// You can also acces it here
}
}
The map declaration is incorrect, as you're typing the value to two different objects. Change the declaration to:
Map<String,CurrentAccount> m = new HashMap<String,CurrentAccount>();
Then, presuming the accountID value is a string, it should be as simple as...
m.put( accountID, ca );
Altogether you'll have:
Map<String,CurrentAccount> m = new HashMap<String,CurrentAccount>();
if (Account.validateID(accountID)) {
CurrentAccount ca = new CurrentAccount(cl,accountID, sortCode, 0);
m.put( accountID, ca );
}
Use put(key, value); See HashMap javadoc
m.put("SomeIdentifierString", ca);
Then whenever you want to access that particular object. Use the key to obtain it
CurrentAccount account = m.get("SomeIdentifierString");
If you want to iterate through the entire map to get key and values you can do this
for (Map.Entry<String, CurrentAccount> entry : m.entrySet()){
String s = entry.getKey();
CurrentAccount accuont = entry.getValue();
// do something with them
}
Your code does not compile because you try to initialize the hasmap bound to the type String and Account to an hashmap of type String String for (key and value type)
HashMap<String, CurrentAccount> accountsMap = new Hashmap<String, String>()
should be
HashMap<String, CurrentAccount> accountsMap = new Hashmap<String, CurrentAccount>()
The first argument is the type for the key value the second is the type of the associated value for the key
To find a value within your hashmap you can use the following code snipped
for (String key : accountsMap.keySet().iterator()) {
CurrentAccount current = accounts.get(key);
}
where accountsMap is your HashMap.