Java HashMap根据值获取键。

huangapple go评论81阅读模式
英文:

Java HashMap take out keys based on value

问题

这是我的哈希映射(Hashmap),其中键是一个名为Payment的模型,值是一个String,表示支付过程中发生的错误。

Map<Payment, String> mapPaymentWithError = new HashMap<>()

如果支付没有错误,我们将字符串值存储为null,并将Payment模型数据作为键。

我正在尝试基于上述哈希映射中错误String是否为NullNotNull来对Payment进行分组,该哈希映射包含许多记录。

如下所示:

List<Payment> withNullErrors = .............. // 具有null字符串值 List<Payment> withErrors = ................... // 具有NotNull字符串值

如何实现这一点?

尝试使用Collectors.GroupingBy和其他方法,但未能成功。

英文:

This is my Hashmap in which the key is a model Payment and value is a String, which is an Error occurred during payment.

Map<Payment, String> mapPaymentWithError = new HashMap<>()

If there are no errors in Payment we are storing String value as null and the key as Payment model data.

I am trying to group Payment based on whether error String was Null or NotNull from above HashMap which is having many records.

Like below

List<Payment> withNullErrors = .............. // having String value as Null
List<Payment> withErrors = ................... // having String value as NotNull

How to do this ?

Tried using Collectors.GroupingBy and other ways but its not working

答案1

得分: 1

以下是您要翻译的代码部分:

public class ListFiltering {

   public static void main( String[] args ) {
      Map<Payment, String> mapPaymentWithError = new HashMap<>();
      mapPaymentWithError.put( new Payment( "1" ), "1" );
      mapPaymentWithError.put( new Payment( "null" ), null );
      
      List<Payment> withNullErrors = mapPaymentWithError.entrySet().stream()
              .filter( e ->  e.getValue() == null  )
              .map( e -> e.getKey() )
              .toList();
      
      List<Payment> withErrors  = mapPaymentWithError.entrySet().stream()
              .filter( e ->  e.getValue() != null  )
              .map( e -> e.getKey() )
              .toList();
      
      System.out.println( "with null errors: " + withNullErrors );
      System.out.println( "with other errors: " + withErrors );
   }

   static record Payment( String id ) {}
}
英文:

Do you mean something like this?

public class ListFiltering {

   public static void main( String[] args ) {
      Map&lt;Payment, String&gt; mapPaymentWithError = new HashMap&lt;&gt;();
      mapPaymentWithError.put( new Payment( &quot;1&quot; ), &quot;1&quot; );
      mapPaymentWithError.put( new Payment( &quot;null&quot; ), null );
      
      List&lt;Payment&gt; withNullErrors = mapPaymentWithError.entrySet().stream()
              .filter( e -&gt;  e.getValue() == null  )
              .map( e -&gt; e.getKey() )
              .toList();
      
      List&lt;Payment&gt; withErrors  = mapPaymentWithError.entrySet().stream()
              .filter( e -&gt;  e.getValue() != null  )
              .map( e -&gt; e.getKey() )
              .toList();
      
      System.out.println( &quot;with null errors: &quot; + withNullErrors );
      System.out.println( &quot;with other errors: &quot; + withErrors );
   }

   static record Payment( String id ) {}
}

huangapple
  • 本文由 发表于 2023年2月27日 04:49:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75574924.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定