Q:How to store a list of values in Java and retrieve the details [closed] |
Q:如何在java中存储的值的列表和检索资料[关闭] |
I want to create a list of nodes which stores their name (String), IP(String), port name(Integer), current workload(Integer), and maximum workload(Integer). Tried searching for ways to do this using a hashtable/map or an array list but I only found examples of people using two elements in their lists. Would I be able to retrieve all of these values based on which has the lowest current workload?. |
我想创建一个节点列表,其中存储他们的名字(字符串),IP(字符串),端口名称(整数),当前工作量(整数),最大工作量(整数)。试着寻找方法来做这个使用哈希表/图或数组列表,但我发现人们使用他们列出两元素。我可以检索所有这些值的基础上,目前的最低工作量?。 |
answer1: |
回答1: |
You can create class Node for example:
import java.util.ArrayList;
import java.util.List;
public class Node {
private String name;
private String IP;
private Integer portName;
private Integer currentWorkload;
private Integer maximumWorkload;
public Node (String name, String IP, Integer portName, Integer currentWorkload,
Integer maximumWorkload) {
this.name = name;
this.IP = IP;
this.portName = portName;
this.currentWorkload = currentWorkload;
this.maximumWorkload = maximumWorkload;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIP() {
return IP;
}
public void setIP(String iP) {
IP = iP;
}
public Integer getPortName() {
return portName;
}
public void setPortName(Integer portName) {
this.portName = portName;
}
public Integer getCurrentWorkload() {
return currentWorkload;
}
public void setCurrentWorkload(Integer currentWorkload) {
this.currentWorkload = currentWorkload;
}
public Integer getMaximumWorkload() {
return maximumWorkload;
}
public void setMaximumWorkload(Integer maximumWorkload) {
this.maximumWorkload = maximumWorkload;
}
}
And after that adding elements of type Node to List:
public static void main(String[] args) {
List<Node> nodes = new ArrayList<Node>();
nodes.add(new Node("name1", "0.0.0.0", 111, 1, 3));
// and you can adds other nodes like this
}
|
您可以创建类节点例如:
import java.util.ArrayList;
import java.util.List;
public class Node {
private String name;
private String IP;
private Integer portName;
private Integer currentWorkload;
private Integer maximumWorkload;
public Node (String name, String IP, Integer portName, Integer currentWorkload,
Integer maximumWorkload) {
this.name = name;
this.IP = IP;
this.portName = portName;
this.currentWorkload = currentWorkload;
this.maximumWorkload = maximumWorkload;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIP() {
return IP;
}
public void setIP(String iP) {
IP = iP;
}
public Integer getPortName() {
return portName;
}
public void setPortName(Integer portName) {
this.portName = portName;
}
public Integer getCurrentWorkload() {
return currentWorkload;
}
public void setCurrentWorkload(Integer currentWorkload) {
this.currentWorkload = currentWorkload;
}
public Integer getMaximumWorkload() {
return maximumWorkload;
}
public void setMaximumWorkload(Integer maximumWorkload) {
this.maximumWorkload = maximumWorkload;
}
}
之后添加类型节点元素到列表中:
public static void main(String[] args) {
List<Node> nodes = new ArrayList<Node>();
nodes.add(new Node("name1", "0.0.0.0", 111, 1, 3));
// and you can adds other nodes like this
}
|
answer2: |
回答2: |
You can use PriorityQueue<Node> with custom comparator. |
你可以使用优先级& lt;节点>;自定义比较器。 |
java
|
|