Java - 通过HashMap访问对象的数组列表(Key为object)
Java - Access Object's Array List through HashMap (Key is object)
我正在尝试遍历 HashMap,然后对于每个键,我想访问与该键关联的对象 (Shipment),并访问我的数组列表以进行进一步分析。 HashMap中的每个object/key都有相同的数组列表(metricList)。我似乎无法访问它,尽管我已经检查了 private/public 东西。有人能指出我正确的方向吗?
我想我可能需要获取我的对象的 class 然后使用方法 "getList"...我尝试过但没有成功。
这是代码示例(删除了不相关的部分)如果有帮助:
这是我的对象:
public class Shipment{
//Members of shipment
private final String shipment;
public Date creationDate;
public int creationTiming;
public int processingTiming;
public ArrayList<Integer> metricList;
public void createArrayList() {
// create list
metricList = new ArrayList<Integer>();
// add metric to list
metricList.add(creationTiming);
metricList.add(processingTiming);
}
public ArrayList<Integer> getList() {
return metricList;
}
}
这是class我通过不同的分析创建hashMap和运行的地方:
public class AnalysisMain {
public static Map<String, Shipment> shipMap = new HashMap();
public static void main(String[] args) {
try {
... // Different calls to analysis
}
catch {}
}
}
这是问题所在(它不识别我已经有一个"metricList",询问我是否要创建局部变量)
public class Metric_Analysis{
public static void analyze() throws Exception{
ResultSet rs;
try {
rs = getSQL("SELECT * FROM TEST_METRICS");
}
catch(Exception e) {
//Pass the error
throw new java.lang.Exception("DB Error: " + e.getMessage());
}
Iterator<Map.Entry<String, Shipment>> iterator = shipMap.entrySet().iterator();
while(iterator.hasNext()){
Iterator<String> metricIterator = metricList.iterator();
//Above is the Array List I want to access and loop through
//I will then perform certain checked against other values on a table...
while (metricIterator.hasNext()) {
//I will perform certain things here
}
}
}
}
您需要从您的货件中取出清单。
您可以通过以下方式从迭代器访问对象:iterator.next();
这还将在您的 List/Map.
中设置指向下一个条目的指针
更改您的代码:
Iterator<Map.Entry<String, Shipment>> iterator = shipMap.entrySet().iterator();
while(iterator.hasNext()){
// Get the Entry from your Map and get the value from the Entry
Entry<String, Shipment> entry = iterator.next();
List<Integer> metricList = entry.getValue().getList();
Iterator<String> metricIterator = metricList.iterator();
//Above is the Array List I want to access and loop through
//I will then perform certain checked against other values on a table...
while (metricIterator.hasNext()) {
//I will perform certain things here
}
}
我正在尝试遍历 HashMap,然后对于每个键,我想访问与该键关联的对象 (Shipment),并访问我的数组列表以进行进一步分析。 HashMap中的每个object/key都有相同的数组列表(metricList)。我似乎无法访问它,尽管我已经检查了 private/public 东西。有人能指出我正确的方向吗?
我想我可能需要获取我的对象的 class 然后使用方法 "getList"...我尝试过但没有成功。
这是代码示例(删除了不相关的部分)如果有帮助:
这是我的对象:
public class Shipment{
//Members of shipment
private final String shipment;
public Date creationDate;
public int creationTiming;
public int processingTiming;
public ArrayList<Integer> metricList;
public void createArrayList() {
// create list
metricList = new ArrayList<Integer>();
// add metric to list
metricList.add(creationTiming);
metricList.add(processingTiming);
}
public ArrayList<Integer> getList() {
return metricList;
}
}
这是class我通过不同的分析创建hashMap和运行的地方:
public class AnalysisMain {
public static Map<String, Shipment> shipMap = new HashMap();
public static void main(String[] args) {
try {
... // Different calls to analysis
}
catch {}
}
}
这是问题所在(它不识别我已经有一个"metricList",询问我是否要创建局部变量)
public class Metric_Analysis{
public static void analyze() throws Exception{
ResultSet rs;
try {
rs = getSQL("SELECT * FROM TEST_METRICS");
}
catch(Exception e) {
//Pass the error
throw new java.lang.Exception("DB Error: " + e.getMessage());
}
Iterator<Map.Entry<String, Shipment>> iterator = shipMap.entrySet().iterator();
while(iterator.hasNext()){
Iterator<String> metricIterator = metricList.iterator();
//Above is the Array List I want to access and loop through
//I will then perform certain checked against other values on a table...
while (metricIterator.hasNext()) {
//I will perform certain things here
}
}
}
}
您需要从您的货件中取出清单。 您可以通过以下方式从迭代器访问对象:iterator.next(); 这还将在您的 List/Map.
中设置指向下一个条目的指针更改您的代码:
Iterator<Map.Entry<String, Shipment>> iterator = shipMap.entrySet().iterator();
while(iterator.hasNext()){
// Get the Entry from your Map and get the value from the Entry
Entry<String, Shipment> entry = iterator.next();
List<Integer> metricList = entry.getValue().getList();
Iterator<String> metricIterator = metricList.iterator();
//Above is the Array List I want to access and loop through
//I will then perform certain checked against other values on a table...
while (metricIterator.hasNext()) {
//I will perform certain things here
}
}