I am trying to get the first name of a linkedhashmap but its only spitting out the last one.
This is my hashmap, Im only getting the last name printed out. How do I get the first one? Is there a way to get it linked to the key. For example "gumt" prints out Gumtäkts uni.bibliotek?
LinkedHashMap<String,Node> nodes = new LinkedHashMap<>();
nodes.put("gumt", new Node("Gumtäkts uni.bibliotek ", 60.2039, 24.9638));
nodes.put("lill", new Node("Lillhoplax bibliotek ", 60.2008, 24.8954));
nodes.put("bush", new Node("Busholmens bibliotek ", 60.1593, 24.9207));
for (String key : nodes.keySet()) {
nodes.get(key).setKey(key);
System.out.println("[" key "]" " " nodes.get(key).getName());
}
return nodes;
Output:
[gumt] Busholmens bibliotek
[lill] Busholmens bibliotek
[bush] Busholmens bibliotek
I also have a Node class that comes into effect. Node class:
import java.util.ArrayList;
public class Node{
private static String name;
private static String key;
private double latitude;
private double longitude;
private static ArrayList<Node> neighbours;
public Node(String name, double latitude, double longitude) {
this.name = name;
this.key = key;
this.latitude = latitude;
this.longitude = longitude;
this.neighbours = neighbours;
}
public static String getName() {
return name;
}
public static String getKey(){
return key;
}
public void setName(String name){
this.name = name;
}
public void setKey(String name){
this.key = key;
}
public void addNeighbour(Node neighbour){
this.neighbours = neighbours;
}
public static ArrayList<Node> getNeighbours(){
return neighbours;
}
}
CodePudding user response:
Try using entrySet()
instead of keySet()
:
for (Map.Entry<String, Node> entry : nodes.entrySet()) {
String key = entry.getKey();
Node node = entry.getValue();
System.out.println("[" key "] " node.getName());
}
CodePudding user response:
A second association map can be created to keep track the order of elements inserted.
public class TestFirst {
public static void main(String[] args)
{
Main m = new TestFirst().new Main();
System.out.println(m.nodes.get(m.map.get(1)));
System.out.println(m.nodes.get(m.map.get(3)));
}
class Main
{
int counter=0;
LinkedHashMap<String,Node> nodes = new LinkedHashMap<>();
Map <Integer, String> map = new HashMap<>();
Main()
{
nodes.put("gumt", new Node("Gumtäkts uni.bibliotek ", 60.2039, 24.9638));
map.put(incCounter(), "gumt");
nodes.put("lill", new Node("Lillhoplax bibliotek ", 60.2008, 24.8954));
map.put(incCounter(), "lill");
nodes.put("bush", new Node("Busholmens bibliotek ", 60.1593, 24.9207));
map.put(incCounter(),"bush");
}
public int incCounter()
{
return counter;
}
class Node
{
private String name;
private String key;
private double latitude;
private double longitude;
private ArrayList<Node> neighbours;
public Node(String name, double latitude, double longitude) {
this.name = name;
this.key = key;
this.latitude = latitude;
this.longitude = longitude;
this.neighbours = neighbours;
}
public String toString()
{
return name "(" latitude "," longitude ")";
}
public String getName() {
return name;
}
public String getKey(){
return key;
}
public void setName(String name){
this.name = name;
}
public void setKey(String name){
this.key = key;
}
public void addNeighbour(Node neighbour){
this.neighbours = neighbours;
}
public ArrayList<Node> getNeighbours(){
return neighbours;
}
}
}
}
Output:
Gumtäkts uni.bibliotek (60.2039,24.9638)
Busholmens bibliotek (60.1593,24.9207)
Note: Check also the modifiers: static
.
CodePudding user response:
System.out.println("[" key "]" " " nodes.get(key).getName())
In this line you are trying to print the values, which are linked to the keys i suppose. But, the getName()
function in the class node
, only returns the name attribute of nodes
, which in your case is always the same, because the field name
is defined as static in your class Node