Нужна помощь в обходе ArrayList HashMap - PullRequest
0 голосов
/ 06 августа 2020

Мне нужно l oop через ArrayList и искать конкретные «ключи» HashMap и возвращать соответствующие «параметры» HashMap, как показано на снимке экрана.

Переменные IntelliJ

Это то, что у меня есть, но это не работает

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 голосов
/ 06 августа 2020

Переделав этот ответ, поскольку я изначально неверно истолковал вопрос.

На основе предоставленного кода, предполагая, что 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 + "]";
        }

    }
}
...