-
Notifications
You must be signed in to change notification settings - Fork 22
Exercise/completed #2
base: master
Are you sure you want to change the base?
Changes from all commits
6d57b66
b8fd66e
9059a3c
5488706
f42e1e8
d66e448
2c22e72
8ba44af
ea54727
3aa059f
8e2c8b7
d5b89fc
de0d56a
429e5e6
ae3d67c
691adf0
fdfb50a
d5a4c0c
9aa98d0
56ab37b
0a7dd78
1598f60
9e8112f
b9baf56
cad70b9
9e7c40e
3d8c9c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,17 @@ | |
|
||
import com.bobocode.exception.EntityNotFoundException; | ||
import com.bobocode.model.Account; | ||
import com.bobocode.model.Sex; | ||
|
||
import java.math.BigDecimal; | ||
import java.time.Month; | ||
import java.util.*; | ||
import java.util.function.Function; | ||
import java.util.stream.Stream; | ||
|
||
import static java.util.Comparator.comparing; | ||
import static java.util.function.Function.identity; | ||
import static java.util.stream.Collectors.*; | ||
|
||
import static java.util.stream.Collectors.mapping; | ||
import static java.util.stream.Collectors.toMap; | ||
|
@@ -30,7 +37,8 @@ private AccountAnalytics(Collection<Account> accounts) { | |
* @return account with max balance wrapped with optional | ||
*/ | ||
public Optional<Account> findRichestPerson() { | ||
throw new UnsupportedOperationException("It's your job to implement this method"); // todo | ||
return accounts.stream() | ||
.max(comparing(Account::getBalance)); | ||
} | ||
|
||
/** | ||
|
@@ -40,7 +48,9 @@ public Optional<Account> findRichestPerson() { | |
* @return a list of accounts | ||
*/ | ||
public List<Account> findAccountsByBirthdayMonth(Month birthdayMonth) { | ||
throw new UnsupportedOperationException("It's your job to implement this method"); // todo | ||
return accounts.stream() | ||
.filter(a -> a.getBirthday().getMonth().equals(birthdayMonth)) | ||
.collect(toList()); | ||
} | ||
|
||
/** | ||
|
@@ -50,7 +60,8 @@ public List<Account> findAccountsByBirthdayMonth(Month birthdayMonth) { | |
* @return a map where key is true or false, and value is list of male, and female accounts | ||
*/ | ||
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))); | ||
} | ||
|
||
/** | ||
|
@@ -60,7 +71,8 @@ public Map<Boolean, List<Account>> partitionMaleAccounts() { | |
* @return a map where key is an email domain and value is a list of all account with such email | ||
*/ | ||
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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. що значить [1]? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It means, that after splitting email by string E.g. |
||
} | ||
|
||
/** | ||
|
@@ -69,7 +81,9 @@ public Map<String, List<Account>> groupAccountsByEmailDomain() { | |
* @return total number of letters of first and last names of all accounts | ||
*/ | ||
public int getNumOfLettersInFirstAndLastNames() { | ||
throw new UnsupportedOperationException("It's your job to implement this method"); // todo | ||
return accounts.stream() | ||
.mapToInt(a -> a.getFirstName().length() + a.getLastName().length()) | ||
.sum(); | ||
} | ||
|
||
/** | ||
|
@@ -78,7 +92,9 @@ public int getNumOfLettersInFirstAndLastNames() { | |
* @return total balance of all accounts | ||
*/ | ||
public BigDecimal calculateTotalBalance() { | ||
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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you use ZERO after "BigDecimal" ? It's something about method reduce? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Method
I used the second one, that has an identity (initial) value, because I wanted to receive real value rather than optional |
||
} | ||
|
||
/** | ||
|
@@ -87,7 +103,10 @@ public BigDecimal calculateTotalBalance() { | |
* @return list of accounts sorted by first and last names | ||
*/ | ||
public List<Account> sortByFirstAndLastNames() { | ||
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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Почему здесь не получается использовать лямбду а нужно обязательно использовать methodReference? Не дает сделать Идея лямбду. |
||
.collect(toList()); | ||
} | ||
|
||
/** | ||
|
@@ -97,7 +116,9 @@ public List<Account> sortByFirstAndLastNames() { | |
* @return true if there is an account that has an email with provided domain | ||
*/ | ||
public boolean containsAccountWithEmailDomain(String emailDomain) { | ||
throw new UnsupportedOperationException("It's your job to implement this method"); // todo | ||
return accounts.stream() | ||
.map(Account::getEmail) | ||
.anyMatch(email -> email.split("@")[1].equals(emailDomain)); | ||
} | ||
|
||
/** | ||
|
@@ -108,7 +129,11 @@ public boolean containsAccountWithEmailDomain(String emailDomain) { | |
* @return account balance | ||
*/ | ||
public BigDecimal getBalanceByEmail(String email) { | ||
throw new UnsupportedOperationException("It's your job to implement this method"); // todo | ||
return accounts.stream() | ||
.filter(account -> account.getEmail().equals(email)) | ||
.findFirst() | ||
.map(Account::getBalance) | ||
.orElseThrow(() -> new EntityNotFoundException(String.format("Cannot find Account by email=%s", email))); | ||
} | ||
|
||
/** | ||
|
@@ -117,7 +142,8 @@ public BigDecimal getBalanceByEmail(String email) { | |
* @return map of accounts by its ids | ||
*/ | ||
public Map<Long, Account> collectAccountsById() { | ||
throw new UnsupportedOperationException("It's your job to implement this method"); // todo | ||
return accounts.stream() | ||
.collect(toMap(Account::getId, identity())); | ||
} | ||
|
||
/** | ||
|
@@ -128,7 +154,9 @@ public Map<Long, Account> collectAccountsById() { | |
* @return map of account by its ids the were created in a particular year | ||
*/ | ||
public Map<String, BigDecimal> collectBalancesByIdForAccountsCreatedOn(int year) { | ||
throw new UnsupportedOperationException("It's your job to implement this method"); // todo | ||
return accounts.stream() | ||
.filter(account -> account.getCreationDate().getYear() == year) | ||
.collect(toMap(Account::getEmail, Account::getBalance)); | ||
} | ||
|
||
/** | ||
|
@@ -138,7 +166,8 @@ public Map<String, BigDecimal> collectBalancesByIdForAccountsCreatedOn(int year) | |
* @return a map where key is a last name and value is a set of first names | ||
*/ | ||
public Map<String, Set<String>> groupFirstNamesByLastNames() { | ||
throw new UnsupportedOperationException("It's your job to implement this method"); // todo | ||
return accounts.stream() | ||
.collect(groupingBy(Account::getLastName, mapping(Account::getFirstName, toSet()))); | ||
} | ||
|
||
/** | ||
|
@@ -148,7 +177,9 @@ public Map<String, Set<String>> groupFirstNamesByLastNames() { | |
* @return a map where a key is a birthday month and value is comma-separated first names | ||
*/ | ||
public Map<Month, String> groupCommaSeparatedFirstNamesByBirthdayMonth() { | ||
throw new UnsupportedOperationException("It's your job to implement this method"); // todo | ||
return accounts.stream() | ||
.collect(groupingBy(a -> a.getBirthday().getMonth(), | ||
mapping(Account::getFirstName, joining(", ")))); | ||
} | ||
|
||
/** | ||
|
@@ -158,7 +189,10 @@ public Map<Month, String> groupCommaSeparatedFirstNamesByBirthdayMonth() { | |
* @return a map where key is a creation month and value is total balance of all accounts created in that month | ||
*/ | ||
public Map<Month, BigDecimal> groupTotalBalanceByCreationMonth() { | ||
throw new UnsupportedOperationException("It's your job to implement this method"); // todo | ||
return accounts.stream() | ||
.collect(groupingBy(a -> a.getCreationDate().getMonth(), | ||
mapping(Account::getBalance, | ||
reducing(BigDecimal.ZERO, BigDecimal::add)))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't understand what means T identity, and how it works in the method "reduce". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The value of |
||
} | ||
|
||
/** | ||
|
@@ -168,7 +202,11 @@ public Map<Month, BigDecimal> groupTotalBalanceByCreationMonth() { | |
* @return a map where key is a letter and value is its count in all first names | ||
*/ | ||
public Map<Character, Long> getCharacterFrequencyInFirstNames() { | ||
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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please explain how works this function? The most important thing, what does method chars? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure. Method
Now, when you have an
Now, when you have a
|
||
.mapToObj(c -> (char) c) | ||
.collect(groupingBy(identity(), counting())); | ||
} | ||
|
||
/** | ||
|
@@ -178,7 +216,12 @@ public Map<Character, Long> getCharacterFrequencyInFirstNames() { | |
* @return a map where key is a letter and value is its count ignoring case in all first and last names | ||
*/ | ||
public Map<Character, Long> getCharacterFrequencyIgnoreCaseInFirstAndLastNames() { | ||
throw new UnsupportedOperationException("It's your job to implement this method"); // todo | ||
return accounts.stream() | ||
.flatMap(a -> Stream.of(a.getFirstName(), a.getLastName())) | ||
.map(String::toLowerCase) | ||
.flatMapToInt(String::chars) | ||
.mapToObj(c -> (char) c) | ||
.collect(groupingBy(identity(), counting())); | ||
} | ||
|
||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
В мене в IDE метод .collect(partition____) не розпізнає за замовчуванням, та не знаходить параметри,
геттери в Account додавав.
Запрацювало після статік імпорта, але це як підножка при написанні, вчора теж на цьому завис.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can start by typing
Collectors.
and it will show you all available methods likepartitionBy()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Account
class is mapped with lombok annotations. So it requires to enable annotation processing in your IDE + lombok plugin