有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java需要帮助遍历HashMap的ArrayList

我需要循环一个ArrayList,寻找一个特定的“keys”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类,但为了本文的目的,我删除了getter和setter

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) 个答案

  1. # 1 楼答案

    重新设计这个答案,因为我最初误解了这个问题

    根据提供的代码,假设映射中的MatrixTransactionInfoKeys实际上匹配,并且创建方式类似,我在以下输出中得到“find it”:

    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 + "]";
            }
    
        }
    }