how to convert a python dict object to a java equivalent object?

dictionary, hashmap, java, python

Solution

It's probably easiest to just create a class for the (Name, Strength) tuple:

class NameStrength {
    public String name;
    public String strength;
}

Add getters, setters and a constructor if appropriate.

Then you can use the new class in your map:

Map<Integer, NameStrength> nodesMap = new HashMap<Integer, NameStrength>();

In Java 5 and up, you can iterate like this:

for (NameStrength nameStrength : nodesMap.values()) {}

or like this:

for (Entry<Integer, NameStrength> entry : nodesMap.entrySet()) {}

Problem

I need to convert a python code into an equivalent java code. Python makes life very easy for the developers by providing lots of shortcut functionalities. But now I need to migrate the same to Java. I was wondering what will the equivalent of dict objects in java? I have tried using HashMap but life is hell. For starters consider this, ``` # Nodes is a dictionary -> Key : (Name, Strength) for node, (name, strength) in nodes.items(): nodes[node] = (name, new_strength) ``` So how to go about converting this into Java? For starters I used HashMap object so, ``` Map<Integer, List> nodesMap = new HashMap<Integer,List>(); /* For iterating over the map */ Iterator updateNodeStrengthIterator = nodesMap.entrySet().iterator(); while(updateNodeStrengthIterator.hasNext()){ } ``` My problem is in getting the List part which contains Name & Strength & then updating the Strength part. Is there any feasible way to do this? Should I consider some different data structure? Please help.

Original source

Related problems