0

After running a query my code will have a list of Object ExchangeTradeStatus.

List<ExchangeTradeStatus> pendingBseUsers = exchangeTradeStatusRepository.findByStatus("PENDING");

ExchangeTradeStatus has a userId field. I want to create a Map<String, Boolean> using java streams to extract userId from each item in the list and by default set the Boolean as False for each Key.

Map<String, Boolean> pendingUcc = pendingBseUsers.stream().collect(Collectors.toMap(ExchangeTradeStatus::getUcc, Boolean.FALSE));

But clearly there's some mistake. Would want to know how to fix this or if this is even possible.

In fact, initially I wanted a Set which would have the 2 required fields but I wasn't sure If That was Possible without creating an additional class with the two member variables.

Update:

Set<UccTradingPermission> pendingBseUcc = pendingBseUsers.stream().map(user -> new UccTradingPermission(user.getUcc())).collect(Collectors.toSet());

that's the only way I can think of to create a Set where the class used is:

public class UccTradingPermission {

    String ucc;
    Boolean status = Boolean.FALSE

    public UccTradingPermission(String ucc){
        this.ucc = ucc;
    }
}
Aditya K
  • 47
  • 7
  • *the 2 required fields* - so what are the **two field** you're talking about? Please describe the actual result. It's obvious that `Map` where every value is `false` is not useful anyhow, but it's not clear what you're trying to achieve. – Alexander Ivanchenko Jul 01 '22 at 15:58
  • the two fields are- String userId and Boolean.False – Aditya K Jul 01 '22 at 16:00
  • But there's the same issue with the set, field `status` of every element would be `false`. Or I'm missing something? Try to describe what you would like to see as a result, what should be the structure of this object. – Alexander Ivanchenko Jul 01 '22 at 16:04
  • 2
    If you know every user ID is mapped to `false`, then what's the point of having the map at all? Anyway, perhaps you want `toMap(ExchangeTradeStatus::getUcc, ets -> false)`? – Slaw Jul 01 '22 at 16:06
  • cause I'm iterating over the map and changing some of the values to true so my base value needs to be false – Aditya K Jul 01 '22 at 16:08
  • If your question was about compilation error in the `toMap()` have a look at the comment by *@Slaw* – Alexander Ivanchenko Jul 01 '22 at 16:09
  • Yes that worked thanks a lot! Also needed a suggestion- if I were to use the Set should I override equals and hashcode in my class? ref: [stackoverflow_question](https://stackoverflow.com/questions/50604761/java-8-streams-converting-a-list-of-objects-to-a-set-of-objects) – Aditya K Jul 01 '22 at 16:10
  • @AdityaK Exactly, with a set you have to implement `equals` and `hashCode` based on the field would not be mutated. – Alexander Ivanchenko Jul 01 '22 at 16:12
  • @AlexanderIvanchenko but can I avoid that by using a Map? I might be fundamentally wrong here as Maps too use hashcode and equals – Aditya K Jul 01 '22 at 16:15
  • @AlexanderIvanchenko Thank you for your feedback, will read up about this some more – Aditya K Jul 01 '22 at 16:32
  • @AdityaK There's nothing to avoid, these methods are not going to bite you. And you can ask the IDE to generate them for you (search the short-cuts for your IDE). As the rule of thumb, remember: it's a good practice to implement `equals/hashCode` contract for objects that are intended to be used with collections. In the map you've shown, you're using only standard classes `String` and `Boolean`, hence you're not required to implement anything. – Alexander Ivanchenko Jul 01 '22 at 16:35

0 Answers0