需要帮助遍历HashMap的ArrayList

时间:2020-08-06 12:36:55

标签: java arraylist hashmap

我需要遍历ArrayList并查找特定的“键” HashMap,并返回相应的“ params” HashMap,如屏幕截图所示。

IntelliJ Variables

这是我到目前为止的内容,但是没有用

private void getParam() {
    List<Map<String, Object>> matrix = transactionInfoMatrix.getMatrixTransactionInfo();

    MatrixTransactionInfoKeys key = new MatrixTransactionInfoKeys("OP/OP", "2777", "CT", "NBCTRANSFER", "AMT");

    for (Map<String, Object> entry : matrix) {
        if (entry.containsValue(key)) {
            System.out.println("Found it");
        }
    }
}

这里是MatrixTransactionInfoKeys类,但是出于本文的目的,我删除了吸气剂和吸气剂。

public class MatrixTransactionInfoKeys {
  
  private String accountType;
  private String applicationSourceCode;
  private String operationType;
  private String service;
  private String transactionType;

  public MatrixTransactionInfoKeys(String accountType, String applicationSourceCode, String operationType, String service, String transactionType) {
    this.accountType = accountType;
    this.applicationSourceCode = applicationSourceCode;
    this.operationType = operationType;
    this.service = service;
    this.transactionType = transactionType;
  }

  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((accountType == null) ? 0 : accountType.hashCode());
    result = prime * result + ((applicationSourceCode == null) ? 0 : applicationSourceCode.hashCode());
    result = prime * result + ((operationType == null) ? 0 : operationType.hashCode());
    result = prime * result + ((service == null) ? 0 : service.hashCode());
    result = prime * result + ((transactionType == null) ? 0 : transactionType.hashCode());
    return result;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj) {
      return true;
    }
    if (obj == null) {
      return false;
    }
    if (getClass() != obj.getClass()) {
      return false;
    }
    MatrixTransactionInfoKeys other = (MatrixTransactionInfoKeys) obj;
    if (accountType == null) {
      if (other.accountType != null) {
        return false;
      }
    } else if (!accountType.equals(other.accountType)) {
      return false;
    }
    if (applicationSourceCode == null) {
      if (other.applicationSourceCode != null) {
        return false;
      }
    } else if (!applicationSourceCode.equals(other.applicationSourceCode)) {
      return false;
    }
    if (operationType == null) {
      if (other.operationType != null) {
        return false;
      }
    } else if (!operationType.equals(other.operationType)) {
      return false;
    }
    if (service == null) {
      if (other.service != null) {
        return false;
      }
    } else if (!service.equals(other.service)) {
      return false;
    }
    if (transactionType == null) {
      return other.transactionType == null;
    } else
      return transactionType.equals(other.transactionType);
  }

  @Override
  public String toString() {
    return "MatrixTransactionInfoKeys [service=" + service + ", applicationSourceCode=" + applicationSourceCode
        + ", transactionType=" + transactionType + ", operationType=" + operationType + ", accountType=" + accountType + "]";
  }

}

1 个答案:

答案 0 :(得分:0)

重新回答这个问题,因为我最初对这个问题有误解。

根据提供的代码,假设地图中的MatrixTransactionInfoKeys实际上匹配并以类似方式创建,我在此输出中得到“找到”:

public class MatrixCheck {

    public TransactionInfoMatrix transactionInfoMatrix;

    private void getParam() {
        List<Map<String, Object>> matrix = transactionInfoMatrix.getMatrixTransactionInfo();

        MatrixTransactionInfoKeys key = new MatrixTransactionInfoKeys("OP/OP", "2777", "CT", "NBCTRANSFER", "AMT");

        for (Map<String, Object> entry : matrix) {
            if (entry.containsValue(key)) {
                System.out.println("Found it");
            }
        }
    }

    public static void main( String[] args ) {
        MatrixCheck matrixCheck = new MatrixCheck();
        matrixCheck.transactionInfoMatrix = new TransactionInfoMatrix();
        matrixCheck.transactionInfoMatrix.transactionInfo = new ArrayList<>();
        matrixCheck.transactionInfoMatrix.transactionInfo.add( new HashMap<>() );
        // Add the object to the map exactly as how it is 
        // constructed in the "getParam" portion
        matrixCheck.transactionInfoMatrix.transactionInfo.get(0).put( "MyTest", new MatrixTransactionInfoKeys("OP/OP", "2777", "CT", "NBCTRANSFER", "AMT") );
        matrixCheck.getParam();
    }

    static class TransactionInfoMatrix {
        List<Map<String,Object>> transactionInfo;

        public List<Map<String,Object>> getMatrixTransactionInfo() {
            return transactionInfo;
        }
    }

    static class MatrixTransactionInfoKeys {

        private String accountType;
        private String applicationSourceCode;
        private String operationType;
        private String service;
        private String transactionType;

        public MatrixTransactionInfoKeys(String accountType, String applicationSourceCode, String operationType, String service, String transactionType) {
            this.accountType = accountType;
            this.applicationSourceCode = applicationSourceCode;
            this.operationType = operationType;
            this.service = service;
            this.transactionType = transactionType;
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((accountType == null) ? 0 : accountType.hashCode());
            result = prime * result + ((applicationSourceCode == null) ? 0 : applicationSourceCode.hashCode());
            result = prime * result + ((operationType == null) ? 0 : operationType.hashCode());
            result = prime * result + ((service == null) ? 0 : service.hashCode());
            result = prime * result + ((transactionType == null) ? 0 : transactionType.hashCode());
            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            MatrixTransactionInfoKeys other = (MatrixTransactionInfoKeys) obj;
            if (accountType == null) {
                if (other.accountType != null) {
                    return false;
                }
            } else if (!accountType.equals(other.accountType)) {
                return false;
            }
            if (applicationSourceCode == null) {
                if (other.applicationSourceCode != null) {
                    return false;
                }
            } else if (!applicationSourceCode.equals(other.applicationSourceCode)) {
                return false;
            }
            if (operationType == null) {
                if (other.operationType != null) {
                    return false;
                }
            } else if (!operationType.equals(other.operationType)) {
                return false;
            }
            if (service == null) {
                if (other.service != null) {
                    return false;
                }
            } else if (!service.equals(other.service)) {
                return false;
            }
            if (transactionType == null) {
                return other.transactionType == null;
            } else
                return transactionType.equals(other.transactionType);
        }

        @Override
        public String toString() {
            return "MatrixTransactionInfoKeys [service=" + service + ", applicationSourceCode=" + applicationSourceCode
                    + ", transactionType=" + transactionType + ", operationType=" + operationType + ", accountType=" + accountType + "]";
        }

    }
}