Conversation
- implemented Functions.java
# Conflicts: # account-analytics/src/main/java/com.bobocode/AccountAnalytics.java
# Conflicts: # crazy-lambdas/src/main/java/com/bobocode/CrazyLambdas.java
# Conflicts: # account-analytics/src/main/java/com.bobocode/AccountAnalytics.java
| public Map<Boolean, List<Account>> partitionMaleAccounts() { | ||
| throw new UnsupportedOperationException("It's your job to implement this method"); // todo | ||
| return accounts.stream() | ||
| .collect(partitioningBy(a -> a.getSex().equals(Sex.MALE))); |
There was a problem hiding this comment.
В мене в IDE метод .collect(partition____) не розпізнає за замовчуванням, та не знаходить параметри,
геттери в Account додавав.
Запрацювало після статік імпорта, але це як підножка при написанні, вчора теж на цьому завис.
There was a problem hiding this comment.
You can start by typing Collectors. and it will show you all available methods like partitionBy()
There was a problem hiding this comment.
Account class is mapped with lombok annotations. So it requires to enable annotation processing in your IDE + lombok plugin
| public Map<String, List<Account>> groupAccountsByEmailDomain() { | ||
| throw new UnsupportedOperationException("It's your job to implement this method"); // todo | ||
| return accounts.stream() | ||
| .collect(groupingBy(a -> a.getEmail().split("@")[1])); |
There was a problem hiding this comment.
It means, that after splitting email by string @ you will have an array String[]. The first element of that array will contain a username, and the second will contain email domain name.
E.g. "omgkanamikun@gmail.com".split("@") will result in array ["omgkanamikun", "gmail.com"]. So to get a domain name, you need to get an element from array by index 1.
tboychuk
left a comment
There was a problem hiding this comment.
Answered question
| public Map<Boolean, List<Account>> partitionMaleAccounts() { | ||
| throw new UnsupportedOperationException("It's your job to implement this method"); // todo | ||
| return accounts.stream() | ||
| .collect(partitioningBy(a -> a.getSex().equals(Sex.MALE))); |
There was a problem hiding this comment.
You can start by typing Collectors. and it will show you all available methods like partitionBy()
| public Map<Boolean, List<Account>> partitionMaleAccounts() { | ||
| throw new UnsupportedOperationException("It's your job to implement this method"); // todo | ||
| return accounts.stream() | ||
| .collect(partitioningBy(a -> a.getSex().equals(Sex.MALE))); |
There was a problem hiding this comment.
Account class is mapped with lombok annotations. So it requires to enable annotation processing in your IDE + lombok plugin
| public Map<String, List<Account>> groupAccountsByEmailDomain() { | ||
| throw new UnsupportedOperationException("It's your job to implement this method"); // todo | ||
| return accounts.stream() | ||
| .collect(groupingBy(a -> a.getEmail().split("@")[1])); |
There was a problem hiding this comment.
It means, that after splitting email by string @ you will have an array String[]. The first element of that array will contain a username, and the second will contain email domain name.
E.g. "omgkanamikun@gmail.com".split("@") will result in array ["omgkanamikun", "gmail.com"]. So to get a domain name, you need to get an element from array by index 1.
| throw new UnsupportedOperationException("It's your job to implement this method"); // todo | ||
| return accounts.stream() | ||
| .map(Account::getBalance) | ||
| .reduce(BigDecimal.ZERO, BigDecimal::add); |
There was a problem hiding this comment.
Why do you use ZERO after "BigDecimal" ? It's something about method reduce?
There was a problem hiding this comment.
Method reduce is overloaded. So you have two options:
Optional<T> reduce(BinaryOperator<T> accumulator);T reduce(T identity, BinaryOperator<T> accumulator);
I used the second one, that has an identity (initial) value, because I wanted to receive real value rather than optional
| throw new UnsupportedOperationException("It's your job to implement this method"); // todo | ||
| return accounts.stream() | ||
| .map(Account::getFirstName) | ||
| .flatMapToInt(String::chars) |
There was a problem hiding this comment.
Can you please explain how works this function? The most important thing, what does method chars?
There was a problem hiding this comment.
Sure. Method String#chars() creates a IntStream of characters. (It's because char is basically an int, and JDK doesn't provide a special stream class to hold primitive characters). So, if you want to get a stream of primitive char, you get IntStream.
accounts.stream().map(Account::getFirstName) will return a Stream<String>, so if you will map each string element into a stream of its chars, you will get Stream<IntStream> - a stream of streams.
flatMapToInt(String::chars) is a complex operation that consists of two basic operations:
- "MapToInt" which receives a mapper - function that maps
Stringinto anIntStream - "flat" transforms
Stream<IntStream>into aIntStream, e.g. ((3, 4), (3, 5, 7), (4)) -> (3, 4, 3, 5, 7, 4). Makes it flat.
Now, when you have an IntStream that consists of character codes, you want to transform it into a Stream<Character>, because after you will want to group them into a Map<Character, Long>
mapToObj(c -> (char) c)uses a mapper that castinttochar, and then mapcharto objectCharacter
Now, when you have a Stream<Character>, you can collect them into a map.
groupingBy(Function.identity(), counting())will group characters using its real value (becauseFunction.identity()maps each element into itself) as a key, and then will count occurrences of each element (counting()) and use that number as map value
| return accounts.stream() | ||
| .collect(groupingBy(a -> a.getCreationDate().getMonth(), | ||
| mapping(Account::getBalance, | ||
| reducing(BigDecimal.ZERO, BigDecimal::add)))); |
There was a problem hiding this comment.
Can't understand what means T identity, and how it works in the method "reduce".
Method ===> "reduce(T identity, BinaryOperator accumulator);"
There was a problem hiding this comment.
The value of identity will be used as a first value, when it reduces your stream. In case your stream is empty, reduce() will return identity value.
| throw new UnsupportedOperationException("It's your job to implement this method"); // todo | ||
| return accounts.stream() | ||
| .sorted(comparing(Account::getFirstName) | ||
| .thenComparing(Account::getLastName)) |
There was a problem hiding this comment.
Почему здесь не получается использовать лямбду а нужно обязательно использовать methodReference? Не дает сделать Идея лямбду.
# Conflicts: # crazy-lambdas/src/main/java/com/bobocode/CrazyLambdas.java
Complete exercises: