Adding an Object to a collection - java

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.

Related

How can I get a value from Hashmap<String, Object>?

I need a Map that can have values of any type. So I've tried to use HashMap. However, if I try to get a value from the map, the IDE says that the type doesn't match.
This is a part of my code with the matter:
Map<String, Object> state1 = new HashMap<>();
state1.put("location", exit1);
state1.put("direction", direction);
state1.put("number", 0);
Deque<Map> q = new ArrayDeque<>();
q.add(state1);
List<Integer> numbers = new ArrayList<>();
while (!q.isEmpty()) {
Map<String, Object> state = q.poll();
Pair location = state.get("location");
The variables exit1, direction are already defined before this part, so you may ignore about that. Also, you don't have to care about the code below this part.
My IDE says that the line Map<String, Object> state = q.poll(); has incompatible types — Pair is required but Object is found. How can I make this compatible?
I am working on a problem that can run only one file, so I can't implement a new class.
...
Pair location = null;
if(state.get("location") instanceof Pair){
Pair location = (Pair)state.get("location");
}
else{
...
}
You can specify the generic type like this:
Deque<Map<String, Object>> q = new ArrayDeque<>();

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.

How to get or create new map entry

I'm trying to write a simple utility function to get a value from a Map and, if it's not found to create a new value class and put that in the map.
It seems though very difficult to get the classes of the map's key and value at runtime and the best I can come up with is something horrible along the following lines.
Is there a better way?
private Object getOrCreate( Map<Object, Object> map, Object key, Class<?> mapValueClass ) {
Object value = map.get( key );
if (value == null) {
Constructor<?> con = mapValueClass.getConstructor( key.getClass() );
value = con.newInstance( key );
map.put( key, value );
}
return value;
}
You should ckeck out Map::getOrDefault and Map::computeIfAbsent (added in Java 8); those do pretty much exactly what your function is supposed to do. The difference between the two is that getOrDefault will accept an existing instance (created before the method is invoked) and return it if needed, but will not add it to the map, while computeIfAbsend accepts a function for lazily creating a new value, and will also add that value to the map.
Map<String, List<Integer>> map = new HashMap<>();
List<Integer> list1 = map.getOrDefault("foo", Collections.emptyList());
System.out.println(list1); // empty list
System.out.println(map); // map is still empty
List<Integer> list2 = map.computeIfAbsent("bar", s -> new ArrayList<Integer>());
System.out.println(list2); // empty list
System.out.println(map); // entry added to map
Assuming that you always want to create a new instance of the Value class with the key as parameter, and assuming that that class actually has such a constructor) you could e.g. use this:
YourClass obj = map.computeIfAbsent(key, YourClass::new);

Retrieving Values From Nested HashMap from user input

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.

Converting Each Value in static HashMap to String Java

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.

Categories

Resources