{"id":291288,"date":"2018-10-24T14:10:02","date_gmt":"2018-10-24T10:10:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=291288"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2018-10-24T10:10:02","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=291288","title":{"rendered":"\u041c\u043e\u0439 \u0443\u043b\u043e\u0432 \u0437\u0430 \u043d\u0435\u0434\u0435\u043b\u044e"},"content":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\">\n<p>\u0427\u0435\u043c \u0431\u043e\u043b\u044c\u0448\u0435 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043d\u0430\u0434 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435\u043c \u0432 \u043a\u043e\u043c\u0430\u043d\u0434\u0435 \u0438 \u0447\u0435\u043c \u043b\u0443\u0447\u0448\u0435 \u0437\u043d\u0430\u0435\u0442 \u0435\u0433\u043e \u043a\u043e\u0434, \u0442\u0435\u043c \u0447\u0430\u0449\u0435 \u043e\u043d \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442\u0441\u044f \u0432\u044b\u0447\u0438\u0442\u043a\u043e\u0439 \u0442\u0432\u043e\u0440\u0447\u0435\u0441\u0442\u0432\u0430 \u0441\u0432\u043e\u0438\u0445 \u0442\u043e\u0432\u0430\u0440\u0438\u0449\u0435\u0439. \u0421\u0435\u0433\u043e\u0434\u043d\u044f \u044f \u043f\u043e\u043a\u0430\u0436\u0443, \u0447\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0432\u044b\u043b\u043e\u0432\u043b\u0435\u043d\u043e \u0437\u0430 \u043e\u0434\u043d\u0443 \u043d\u0435\u0434\u0435\u043b\u044e \u0432 \u043a\u043e\u0434\u0435, \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u043e\u043c \u0432\u0435\u0441\u044c\u043c\u0430 \u043d\u0435\u043f\u043b\u043e\u0445\u0438\u043c\u0438 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430\u043c\u0438. \u041f\u043e\u0434 \u043a\u0430\u0442\u043e\u043c \u0441\u043e\u0431\u0440\u0430\u043d\u0438\u0435 \u044f\u0440\u043a\u0438\u0445 \u0430\u0440\u0442\u0435\u0444\u0430\u043a\u0442\u043e\u0432 \u043d\u0430\u0448\u0435\u0433\u043e \u0442\u0432\u043e\u0440\u0447\u0435\u0441\u0442\u0432\u0430 (\u0438 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043c\u043e\u0438\u0445 \u0440\u0430\u0437\u043c\u044b\u0448\u043b\u0435\u043d\u0438\u0439).<\/p>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<h3 id=\"komparatory\">\u041a\u043e\u043c\u043f\u0430\u0440\u0430\u0442\u043e\u0440\u044b<\/h3>\n<p>  <\/p>\n<p>\u0415\u0441\u0442\u044c \u043a\u043e\u0434<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">@Getter class Dto {   private Long id; }  \/\/ another class  List&lt;Long&gt; readSortedIds(List&lt;Dto&gt; list) {   List&lt;Long&gt; ids = list.stream().map(Dto::getId).collect(Collectors.toList());   ids.sort(new Comparator&lt;Long&gt;() {     public int compare(Long o1, Long o2) {       if (o1 &lt; o2) return -1;       if (o1 &gt; o2) return 1;       return 0;     }  });  return ids; }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041a\u0442\u043e-\u0442\u043e \u043e\u0442\u043c\u0435\u0442\u0438\u0442, \u0447\u0442\u043e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0441\u0442\u0440\u0438\u043c, \u044f \u0436\u0435 \u0445\u043e\u0447\u0443 \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c \u0432\u0430\u0448\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u043a\u043e\u043c\u043f\u0430\u0440\u0430\u0442\u043e\u0440. \u0412 <a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/Comparator.html#compare-T-T-\">\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438<\/a> \u043a \u043c\u0435\u0442\u043e\u0434\u0443 <code>Comparator::compare<\/code> \u0430\u043d\u0433\u043b\u0438\u0439\u0441\u043a\u0438\u043c \u043f\u043e \u0431\u0435\u043b\u043e\u043c\u0443 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043e:<\/p>\n<p>  <\/p>\n<blockquote><p>Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.<\/p><\/blockquote>\n<p>\u0418\u043c\u0435\u043d\u043d\u043e \u044d\u0442\u043e \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043e \u0432 \u043d\u0430\u0448\u0435\u043c \u043a\u043e\u0434\u0435. \u0427\u0442\u043e \u0436\u0435 \u043d\u0435 \u0442\u0430\u043a? \u0414\u0435\u043b\u043e \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u0435\u043b\u0438 \u044f\u0432\u044b \u0432\u0435\u0441\u044c\u043c\u0430 \u0434\u0430\u043b\u044c\u043d\u043e\u0432\u0438\u0434\u043d\u043e \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043b\u0438, \u0447\u0442\u043e \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0439 \u043a\u043e\u043c\u043f\u0430\u0440\u0430\u0442\u043e\u0440 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u043c\u043d\u043e\u0433\u0438\u043c \u0438 \u0441\u0434\u0435\u043b\u0430\u043b\u0438 \u0435\u0433\u043e \u0437\u0430 \u043d\u0430\u0441. \u041d\u0430\u043c \u0436\u0435 \u043e\u0441\u0442\u0430\u0451\u0442\u0441\u044f \u0438\u043c \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0432 \u043d\u0430\u0448 \u043a\u043e\u0434:<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">List&lt;Long&gt; readSortedIds(List&lt;Dto&gt; list) {   List&lt;Long&gt; ids = list.stream().map(Dto::getId).collect(Collectors.toList());   ids.sort(Comparator.naturalOrder());   return ids; }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0422\u043e\u0447\u043d\u043e \u0442\u0430\u043a \u0436\u0435 \u044d\u0442\u043e\u0442 \u043a\u043e\u0434<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">List&lt;Dto&gt; sortDtosById(List&lt;Dto&gt; list) {   list.sort(new Comparator&lt;Dto&gt;() {     public int compare(Dto o1, Dto o2) {        if (o1.getId() &lt; o2.getId()) return -1;        if (o1.getId() &gt; o2.getId()) return 1;        return 0;     }   });   return list; }<\/code><\/pre>\n<p>  <\/p>\n<p>\u043b\u0451\u0433\u043a\u0438\u043c \u0434\u0432\u0438\u0436\u0435\u043d\u0438\u0435\u043c \u0440\u0443\u043a\u0438 \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u0430\u043a\u043e\u0439<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">List&lt;Dto&gt; sortDtosById(List&lt;Dto&gt; list) {   list.sort(Comparator.comparing(Dto::getId));   return list; }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041a\u0441\u0442\u0430\u0442\u0438, \u0432 \u043d\u043e\u0432\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0438 &#171;\u0418\u0434\u0435\u0438&#187; \u043c\u043e\u0436\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u0434\u0435\u043b\u0430\u0442\u044c \u0432\u043e\u0442 \u0442\u0430\u043a:<\/p>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041e\u043a\u0430\u0442\u0438\u0442\u044c\u0441\u044f \u0447\u0430\u0440\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u043e\u043c<\/b><\/p>\n<div class=\"spoiler_text\">\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/b_\/om\/ki\/b_omkixybpa-jvxblirsruyvglo.gif\"><\/p>\n<\/div>\n<\/div>\n<p>  <\/p>\n<h3 id=\"zloupotreblenie-optional-ami\">\u0417\u043b\u043e\u0443\u043f\u043e\u0442\u0440\u0435\u0431\u043b\u0435\u043d\u0438\u0435 Optional-\u0430\u043c\u0438<\/h3>\n<p>  <\/p>\n<p>\u041d\u0430\u0432\u0435\u0440\u043d\u043e\u0435, \u043a\u0430\u0436\u0434\u044b\u0439 \u0438\u0437 \u043d\u0430\u0441 \u0432\u0438\u0434\u0435\u043b \u0447\u0442\u043e-\u0442\u043e \u0432\u0440\u043e\u0434\u0435 \u044d\u0442\u043e\u0433\u043e:<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">List&lt;UserEntity&gt; getUsersForGroup(Long groupId) {   Optional&lt;Long&gt; optional = Optional.ofNullable(groupId);    if (optional.isPresent()) {     return userRepository.findUsersByGroup(optional.get());   }    return Collections.emptyList();    }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0427\u0430\u0441\u0442\u043e <code>Optional<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043d\u0430\u043b\u0438\u0447\u0438\u044f\/\u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u0445\u043e\u0442\u044f \u0441\u043e\u0437\u0434\u0430\u043d \u043e\u043d \u0431\u044b\u043b \u043d\u0435 \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e. \u0417\u0430\u0432\u044f\u0437\u044b\u0432\u0430\u0439\u0442\u0435 \u0441\u043e \u0437\u043b\u043e\u0443\u043f\u043e\u0442\u0440\u0435\u0431\u043b\u0435\u043d\u0438\u0435\u043c \u0438 \u043f\u0438\u0448\u0438\u0442\u0435 \u043f\u0440\u043e\u0449\u0435:<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">List&lt;UserEntity&gt; getUsersForGroup(Long groupId) {   if (groupId == null) {     return Collections.emptyList();   }    return userRepository.findUsersByGroup(groupId); }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041f\u043e\u043c\u043d\u0438\u0442\u0435, \u0447\u0442\u043e <code>Optional<\/code> \u2014 \u044d\u0442\u043e \u043d\u0435 \u043f\u0440\u043e \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 \u043c\u0435\u0442\u043e\u0434\u0430 \u0438\u043b\u0438 \u043f\u043e\u043b\u0435, \u0430 \u043f\u0440\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435. \u0418\u043c\u0435\u043d\u043d\u043e \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043e\u043d \u0441\u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d \u0431\u0435\u0437 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438.<\/p>\n<p>  <\/p>\n<h3 id=\"void-metody-menyayuschie-sostoyanie-argumenta\">void-\u043c\u0435\u0442\u043e\u0434\u044b, \u043c\u0435\u043d\u044f\u044e\u0449\u0438\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430<\/h3>\n<p>  <\/p>\n<p>\u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u044c\u0442\u0435 \u0442\u0430\u043a\u043e\u0439 \u043c\u0435\u0442\u043e\u0434:<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">@Component @RequiredArgsConstructor public class ContractUpdater {   private final ContractRepository repository;    @Transactional   public void updateContractById(Long contractId, Dto contractDto) {     Contract contract = repository.findOne(contractId);     contract.setValue(contractDto.getValue());     repository.save(contract);   } }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041d\u0430\u0432\u0435\u0440\u043d\u044f\u043a\u0430 \u0432\u044b \u043c\u043d\u043e\u0433\u043e \u0440\u0430\u0437 \u0432\u0438\u0434\u0435\u043b\u0438 \u0438 \u043f\u0438\u0441\u0430\u043b\u0438 \u0447\u0442\u043e-\u0442\u043e \u043f\u043e\u0434\u043e\u0431\u043d\u043e\u0435. \u0417\u0434\u0435\u0441\u044c \u043c\u043d\u0435 \u043d\u0435 \u043d\u0440\u0430\u0432\u0438\u0442\u0441\u044f, \u0447\u0442\u043e \u043c\u0435\u0442\u043e\u0434 \u043c\u0435\u043d\u044f\u0435\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438, \u043d\u043e \u043d\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0435\u0451. \u041a\u0430\u043a \u0432\u0435\u0434\u0443\u0442 \u0441\u0435\u0431\u044f \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434\u044b \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u043e\u0432? \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <code>org.springframework.data.jpa.repository.JpaRepository::save<\/code>\u0438 <code>javax.persistence.EntityManager::merge<\/code> \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435. \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u043f\u043e\u0441\u043b\u0435 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u043a\u043e\u043d\u0442\u0440\u0430\u043a\u0442\u0430 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0435\u0433\u043e \u0437\u0430 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u043c\u0438 \u043c\u0435\u0442\u043e\u0434\u0430 <code>update<\/code>. \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0447\u0442\u043e-\u0442\u043e \u0432\u0440\u043e\u0434\u0435 \u044d\u0442\u043e\u0433\u043e:<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">@Transactional public void anotherMethod(Long contractId, Dto contractDto) {   updateService.updateContractById(contractId, contractDto);   Contract contract = repositoroty.findOne(contractId);   doSmth(contract); }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0414\u0430, \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u0431\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0432 \u043c\u0435\u0442\u043e\u0434 <code>UpdateService::updateContract<\/code>, \u0438\u0437\u043c\u0435\u043d\u0438\u0432 \u0435\u0433\u043e \u0441\u0438\u0433\u043d\u0430\u0442\u0443\u0440\u0443, \u043d\u043e \u043b\u0443\u0447\u0448\u0435 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u043e\u0442 \u0442\u0430\u043a:<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">@Component @RequiredArgsConstructor public class ContractUpdater {   private final ContractRepository repository;    @Transactional   public Contract updateContractById(Long contractId, Dto contractDto) {     Contract contract = repository.findOne(contractId);     contract.setValue(contractDto.getValue());     return repository.save(contract);   } }  \/\/\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435  @Transactional public void anotherMethod(Long contractId, Dto contractDto) {   Contract contract = updateService.updateContractById(contractId, contractDto);   doSmth(contract); }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0421 \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u044b, \u044d\u0442\u043e \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u043a\u043e\u0434, \u0441 \u0434\u0440\u0443\u0433\u043e\u0439 \u2014 \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 \u043f\u0440\u0438 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438. \u0412\u043e\u043e\u0431\u0449\u0435 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 <code>void<\/code> \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u043d\u0430 \u0440\u0435\u0434\u043a\u043e\u0441\u0442\u044c \u043c\u0443\u0442\u043e\u0440\u043d\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430, \u0447\u0442\u043e \u044f \u043f\u043e\u043a\u0430\u0436\u0443, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u044d\u0442\u043e\u0442 \u0436\u0435 \u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">@RunWith(MockitoJUnitRunner.class) public class ContractUpdaterTest {   @Mock    private ContractRepository repository;   @InjectMocks   private ContractUpdater updater;    @Test   public void updateContractById() {     Dto dto = new Dto();     dto.setValue(\"\u0447\u0442\u043e-\u0442\u043e \u0442\u0430\u043c\");      Long contractId = 1L;     when(repository.findOne(contractId)).thenReturn(new Contract());      updater.updateContractById(contractId, contractDto); \/\/void      \/\/\u043a\u0430\u043a \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0447\u0442\u043e \u0432 \u043a\u043e\u043d\u0442\u0440\u0430\u043a\u0442 \u0432\u043f\u0438\u0441\u0430\u043b\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 dto? - \u041f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:     ArgumentCaptor&lt;Contract&gt; captor = ArgumentCaptor.forClass(Contract.class);     verify(repository).save(captor.capture());     Contract updated = captor.getValue();      assertEquals(dto.getValue(), updated.getValue());   } }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0410 \u0432\u0435\u0434\u044c \u0432\u0441\u0451 \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043f\u0440\u043e\u0449\u0435, \u0435\u0441\u043b\u0438 \u043c\u0435\u0442\u043e\u0434 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435:<\/p>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0423\u0431\u0435\u0434\u0438\u0442\u044c\u0441\u044f<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"java\">@RunWith(MockitoJUnitRunner.class) public class ContractUpdaterTest {   @Mock    private ContractRepository repository;   @InjectMocks   private ContractUpdater updater;    @Test   public void updateContractById() {     Dto dto = new Dto();     dto.setValue(\"\u0447\u0442\u043e-\u0442\u043e \u0442\u0430\u043c\");      Long contractId = 1L;     when(repository.findOne(contractId)).thenReturn(new Contract());      Contract updated = updater.updateContractById(contractId, contractDto);      assertEquals(dto.getValue(), updated.getValue());   } }<\/code><\/pre>\n<\/div>\n<\/div>\n<p>  <\/p>\n<p>\u041e\u0434\u043d\u0438\u043c \u043c\u0430\u0445\u043e\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442\u0441\u044f \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u044b\u0437\u043e\u0432 <code>ContractRepository::save<\/code>, \u043d\u043e \u0438 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0441\u043e\u0445\u0440\u0430\u043d\u0451\u043d\u043d\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f.<\/p>\n<p>  <\/p>\n<h3 id=\"velosipedostroenie\">\u0412\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435<\/h3>\n<p>  <\/p>\n<p>\u041f\u0440\u0438\u043a\u043e\u043b\u0430 \u0440\u0430\u0434\u0438, \u043e\u0442\u043a\u0440\u043e\u0439\u0442\u0435 \u0441\u0432\u043e\u0439 \u043f\u0440\u043e\u0435\u043a\u0442 \u0438 \u043f\u043e\u0438\u0449\u0438\u0442\u0435 \u0432\u043e\u0442 \u044d\u0442\u043e:<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">lastIndexOf('.')<\/code><\/pre>\n<p>  <\/p>\n<p>\u0421 \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c\u044e \u0432\u0441\u0451 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">String fileExtension = fileName.subString(fileName.lastIndexOf('.'));<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412\u043e\u0442 \u043e \u0447\u0451\u043c \u043d\u0438 \u043e\u0434\u0438\u043d \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0430\u043d\u0430\u043b\u0438\u0437\u0430\u0442\u043e\u0440 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0435\u0434\u0443\u043f\u0440\u0435\u0434\u0438\u0442\u044c, \u0442\u0430\u043a \u044d\u0442\u043e \u043e \u0432\u043d\u043e\u0432\u044c \u0438\u0437\u043e\u0431\u0440\u0435\u0442\u0451\u043d\u043d\u043e\u043c \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434\u0435. \u0413\u043e\u0441\u043f\u043e\u0434\u0430, \u0435\u0441\u043b\u0438 \u0432\u044b \u0440\u0435\u0448\u0430\u0435\u0442\u0435 \u043d\u0435\u043a\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443, \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u0443\u044e \u0441 \u0438\u043c\u0435\u043d\u0435\u043c\/\u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\u043c \u0444\u0430\u0439\u043b\u0430 \u0438\u043b\u0438 \u043f\u0443\u0442\u0451\u043c \u043a \u043d\u0435\u043c\u0443, \u0440\u043e\u0432\u043d\u043e \u043a\u0430\u043a \u0438 \u0441 \u0447\u0442\u0435\u043d\u0438\u0435\u043c\/\u0437\u0430\u043f\u0438\u0441\u044c\u044e\/\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435, \u0442\u043e \u0432 9 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u0438\u0437 10 \u0437\u0430\u0434\u0430\u0447\u0430 \u0443\u0436\u0435 \u0440\u0435\u0448\u0435\u043d\u0430 \u0434\u043e \u0432\u0430\u0441. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0437\u0430\u0432\u044f\u0437\u044b\u0432\u0430\u0439\u0442\u0435 \u0441 \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435\u043c \u0438 \u0431\u0435\u0440\u0438\u0442\u0435 \u0433\u043e\u0442\u043e\u0432\u044b\u0435 (\u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u0435\u043d\u043d\u044b\u0435) \u0440\u0435\u0448\u0435\u043d\u0438\u044f:<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">import org.apache.commons.io.FilenameUtils;  \/\/... String fileExtension = FilenameUtils.getExtension(fileName);<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0432\u044b \u0441\u0431\u0435\u0440\u0435\u0433\u0430\u0435\u0442 \u0432\u0440\u0435\u043c\u044f, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0431\u044b\u043b\u043e \u0431\u044b \u043f\u043e\u0442\u0440\u0430\u0447\u0435\u043d\u043e \u043d\u0430 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u0433\u043e\u0434\u043d\u043e\u0441\u0442\u0438 \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434\u0430, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0435 \u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u043e\u0434\u0432\u0438\u043d\u0443\u0442\u044b\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b (\u0441\u043c. \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e <code>FilenameUtils::getExtension<\/code>).<\/p>\n<p>  <\/p>\n<p>\u0418\u043b\u0438 \u0432\u043e\u0442 \u0442\u0430\u043a\u043e\u0439 \u043a\u043e\u0434, \u043a\u043e\u043f\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u043e\u0434\u043d\u043e\u0433\u043e \u0444\u0430\u0439\u043b\u0430 \u0432 \u0434\u0440\u0443\u0433\u043e\u0439:<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">try {   FileChannel sc = new FileInputStream(src).getChannel();   FileChannel dc = new FileOutputStream(new File(targetName)).getChannel();   sc.transferTo(0, sc.size(), dc);   dc.close();   sc.close(); } catch (IOException ex) {   log.error(\"\u043e\u0439\", ex); }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041a\u0430\u043a\u0438\u0435 \u043e\u0431\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u0430 \u043c\u043e\u0433\u0443\u0442 \u043d\u0430\u043c \u043f\u043e\u043c\u0435\u0448\u0430\u0442\u044c? \u0422\u044b\u0441\u044f\u0447\u0438 \u0438\u0445:<\/p>\n<p>  <\/p>\n<ul>\n<li>\u0442\u043e\u0447\u043a\u0430 \u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043c\u043e\u0436\u0435\u0442 \u043e\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u043f\u0430\u043f\u043a\u043e\u0439, \u0430 \u0432\u043e\u0432\u0441\u0435 \u043d\u0435 \u0444\u0430\u0439\u043b\u043e\u043c<\/li>\n<li>\u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a \u043c\u043e\u0436\u0435\u0442 \u043e\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u043f\u0430\u043f\u043a\u043e\u0439<\/li>\n<li>\u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a \u0438 \u0442\u043e\u0447\u043a\u0430 \u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043c\u043e\u0433\u0443\u0442 \u043e\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u043e\u0434\u043d\u0438\u043c \u0438 \u0442\u0435\u043c \u0436\u0435 \u0444\u0430\u0439\u043b\u043e\u043c<\/li>\n<li>\u0442\u043e\u0447\u043a\u0443 \u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c<\/li>\n<li>\u0438 \u0442. \u0434. \u0438 \u0442. \u043f.<\/li>\n<\/ul>\n<p>  <\/p>\n<p>\u041f\u0435\u0447\u0430\u043b\u044c \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043f\u043e\u043b\u044c\u0437\u0443\u044f\u0441\u044c \u0441\u0430\u043c\u043e\u043f\u0438\u0441\u044c\u044e \u043e\u0431 \u044d\u0442\u043e\u043c \u0432\u0441\u0451\u043c \u043c\u044b \u0443\u0437\u043d\u0430\u0435\u043c \u0443\u0436\u0435 \u0432 \u0445\u043e\u0434\u0435 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f.<br \/>  \u0415\u0441\u043b\u0438 \u0436\u0435 \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u043f\u043e \u0443\u043c\u0443<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">import org.apache.commons.io.FileUtils;  try {   FileUtils.copyFile(src, new File(targetName)); } catch (IOException ex) {   log.error(\"\u043e\u0439\", ex); }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0442\u043e \u0447\u0430\u0441\u0442\u044c \u043f\u0440\u043e\u0432\u0435\u0440\u043e\u043a \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0430 \u0435\u0449\u0451 \u0434\u043e \u043d\u0430\u0447\u0430\u043b\u0430 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u0430 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0435 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0431\u0443\u0434\u0435\u0442 \u0431\u043e\u043b\u0435\u0435 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0432\u043d\u044b\u043c (\u0441\u043c. \u0438\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0438 <code>FileUtils::copyFile<\/code>).<\/p>\n<p>  <\/p>\n<h3 id=\"prenebrezhenie-nullablenotnull\">\u041f\u0440\u0435\u043d\u0435\u0431\u0440\u0435\u0436\u0435\u043d\u0438\u0435 @Nullable\/@NotNull<\/h3>\n<p>  <\/p>\n<p>\u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c:<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">@Entity  @Getter public class UserEntity {   @Id   private Long id;    @Column   private String email;    @Column   private String petName; }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043a\u043e\u043b\u043e\u043d\u043a\u0430 <code>email<\/code> \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u043e\u043f\u0438\u0441\u0430\u043d\u0430 \u043a\u0430\u043a <code>not null<\/code>, \u0432 \u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442 petName. \u0422. \u0435. \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043c\u0435\u0442\u0438\u0442\u044c \u043f\u043e\u043b\u044f \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u043c\u0438 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044f\u043c\u0438:<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">import javax.annotation.Nullable; import javax.annotation.NotNull;  \/\/...    @Column   @NotNull   private String email;    @Column   @Nullable   private String petName;<\/code><\/pre>\n<p>  <\/p>\n<p>\u041d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u0432\u0437\u0433\u043b\u044f\u0434, \u044d\u0442\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043a\u0430\u043a \u043f\u043e\u0434\u0441\u043a\u0430\u0437\u043a\u0430 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0443, \u0438 \u044d\u0442\u043e \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0442\u0430\u043a. \u0412\u043c\u0435\u0441\u0442\u0435 \u0441 \u0442\u0435\u043c \u044d\u0442\u0438 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438 \u2014 \u043a\u0443\u0434\u0430 \u0431\u043e\u043b\u0435\u0435 \u043c\u043e\u0433\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435 \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u043e, \u0447\u0435\u043c \u043e\u0431\u044b\u0447\u043d\u0430\u044f \u043c\u0435\u0442\u043a\u0430.<br \/>  \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0438\u0445 \u043f\u043e\u043d\u0438\u043c\u0430\u044e\u0442 \u0441\u0440\u0435\u0434\u044b \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438, \u0438 \u0435\u0441\u043b\u0438 \u043f\u043e\u0441\u043b\u0435 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0439 \u043c\u044b \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u043e\u0442 \u0442\u0430\u043a:<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">boolean checkIfPetBelongsToUser(UserEnity user, String lostPetName) {   return user.getPetName().equals(lostPetName); }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0442\u043e &#171;\u0418\u0434\u0435\u044f&#187; \u043f\u0440\u0435\u0434\u0443\u043f\u0440\u0435\u0434\u0438\u0442 \u043d\u0430\u0441 \u043e\u0431 \u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438 \u0442\u0430\u043a\u0438\u043c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435\u043c:<\/p>\n<p>  <\/p>\n<blockquote><p>Method invocation &#8216;equals&#8217; may produce &#8216;NullPointerException&#8217;<\/p><\/blockquote>\n<p>\u0412 \u043a\u043e\u0434\u0435 <\/p>\n<p>  <\/p>\n<pre><code class=\"java\">boolean hasEmail(UserEnity user) {   boolean hasEmail = user.getEmail() == null;   return hasEmail; }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0431\u0443\u0434\u0435\u0442 \u0434\u0440\u0443\u0433\u043e\u0435 \u043f\u0440\u0435\u0434\u0443\u043f\u0440\u0435\u0436\u0434\u0435\u043d\u0438\u0435:<\/p>\n<p>  <\/p>\n<blockquote><p>Condition &#8216;user.getEmail() == null&#8217; is always &#8216;false&#8217;<\/p><\/blockquote>\n<p>\u042d\u0442\u043e \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u043c\u0443 \u0446\u0435\u043d\u0437\u043e\u0440\u0443 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0435 \u043e\u0448\u0438\u0431\u043a\u0438 \u0438 \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 \u043d\u0430\u043c \u043b\u0443\u0447\u0448\u0435 \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043a\u043e\u0434\u0430. \u0421 \u0442\u043e\u0439 \u0436\u0435 \u0446\u0435\u043b\u044c\u044e \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438 \u043f\u043e\u043b\u0435\u0437\u043d\u043e \u0440\u0430\u0441\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u043d\u0430\u0434 \u043c\u0435\u0442\u043e\u0434\u0430\u043c\u0438, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0438\u043c\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u0438 \u0438\u0445 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430\u043c\u0438.<br \/>  \u0415\u0441\u043b\u0438 \u043c\u043e\u0438 \u0434\u043e\u0432\u043e\u0434\u044b \u043a\u0430\u0436\u0443\u0442\u0441\u044f \u043d\u0435\u0443\u0431\u0435\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u043c\u0438, \u0442\u043e \u0441\u043c\u043e\u0442\u0440\u0438\u0442\u0435 \u0438\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0438 \u043b\u044e\u0431\u043e\u0433\u043e \u0441\u0435\u0440\u044c\u0451\u0437\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0430, \u0442\u043e\u0433\u043e \u0436\u0435 &#171;\u0421\u043f\u0440\u0438\u043d\u0433\u0430&#187; \u2014 \u043e\u043d\u0438 \u0443\u0432\u0435\u0448\u0430\u043d\u044b \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044f\u043c\u0438 \u043a\u0430\u043a \u043d\u043e\u0432\u043e\u0433\u043e\u0434\u043d\u044f\u044f \u0451\u043b\u043a\u0430. \u0418 \u044d\u0442\u043e \u043d\u0435 \u0431\u043b\u0430\u0436\u044c, \u0430 \u0441\u0443\u0440\u043e\u0432\u0430\u044f \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c.<br \/>  \u0415\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u043a, \u043a\u0430\u043a \u043c\u043d\u0435 \u043a\u0430\u0436\u0435\u0442\u0441\u044f, \u2014 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438 \u0432 \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438. \u0425\u043e\u0442\u044f, \u0435\u0441\u043b\u0438 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f, \u2014 \u044d\u0442\u043e \u0441\u043a\u043e\u0440\u0435\u0435 \u0431\u043b\u0430\u0433\u043e, \u0432\u0435\u0434\u044c \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044f\u0441\u044c \u0440\u0430\u0437 \u0437\u0430 \u0440\u0430\u0437\u043e\u043c \u043a \u043a\u043e\u0434\u0443 \u043c\u044b \u043f\u043e\u043d\u0438\u043c\u0430\u0435\u043c \u0435\u0433\u043e \u0432\u0441\u0451 \u043b\u0443\u0447\u0448\u0435.<\/p>\n<p>  <\/p>\n<h3 id=\"nevnimatelnost\">\u041d\u0435\u0432\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c<\/h3>\n<p>  <\/p>\n<p>\u0412 \u044d\u0442\u043e\u043c \u043a\u043e\u0434\u0435 \u043d\u0435\u0442 \u043e\u0448\u0438\u0431\u043e\u043a, \u043d\u043e \u0435\u0441\u0442\u044c \u0438\u0437\u043b\u0438\u0448\u0435\u0441\u0442\u0432\u043e:<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">Collection&lt;Dto&gt; dtos = getDtos(); Stream.of(1,2,3,4,5)   .filter(id -&gt; {     List&lt;Integer&gt; ids =  dtos.stream().map(Dto::getId).collect(toList());     return ids.contains(id);   })   .collect(toList());<\/code><\/pre>\n<p>  <\/p>\n<p>\u041d\u0435\u043f\u043e\u043d\u044f\u0442\u043d\u043e, \u0437\u0430\u0447\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u043a\u043b\u044e\u0447\u0435\u0439, \u043f\u043e \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043f\u043e\u0438\u0441\u043a, \u0435\u0441\u043b\u0438 \u043e\u043d \u043d\u0435 \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u043f\u0440\u043e\u0445\u043e\u0434\u0435 \u043f\u043e \u0441\u0442\u0440\u0438\u043c\u0443. \u0425\u043e\u0440\u043e\u0448\u043e, \u0447\u0442\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432\u0441\u0435\u0433\u043e 5, \u0430 \u0435\u0441\u043b\u0438 \u0438\u0445 \u0431\u0443\u0434\u0435\u0442 100500? \u0410 \u0435\u0441\u043b\u0438 \u043c\u0435\u0442\u043e\u0434 <code>getDtos<\/code> \u0432\u0435\u0440\u043d\u0451\u0442 100500 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 (\u0432 \u0441\u043f\u0438\u0441\u043a\u0435!), \u0442\u043e \u043a\u0430\u043a\u0430\u044f \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0431\u0443\u0434\u0435\u0442 \u0443 \u044d\u0442\u043e\u0433\u043e \u043a\u043e\u0434\u0430? \u041d\u0438\u043a\u0430\u043a\u0430\u044f, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043b\u0443\u0447\u0448\u0435 \u0432\u043e\u0442 \u0442\u0430\u043a:<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">Collection&lt;Dto&gt; dtos = getDtos(); Set&lt;Integer&gt; ids =  dtos.stream().map(Dto::getId).collect(toSet()); Stream.of(1,2,3,4,5)   .filter(ids::contains)   .collect(toList());<\/code><\/pre>\n<p>  <\/p>\n<p>\u0422\u0430\u043a\u0436\u0435 \u0438 \u0437\u0434\u0435\u0441\u044c:<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">static &lt;T, Q extends Query&gt; void setParams(   Map&lt;String, Collection&lt;T&gt;&gt; paramMap,   Set&lt;String&gt; notReplacedParams,   Q query) {     notReplacedParams.stream()       .filter(param -&gt; paramMap.keySet().contains(param))       .forEach(param -&gt; query.setParameter(param, paramMap.get(param))); }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0435 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435 <code>inParameterMap.keySet()<\/code> \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u043d\u043e, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0435\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u043d\u0435\u0441\u0442\u0438 \u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e:<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">static &lt;T, Q extends Query&gt; void setParams(   Map&lt;String, Collection&lt;T&gt;&gt; paramMap,   Set&lt;String&gt; notReplacedParams,   Q query) {     Set&lt;String&gt; params = paramMap.keySet();     notReplacedParams.stream()       .filter(params::contains)       .forEach(param -&gt; query.setParameter(param, paramMap.get(param))); }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041a\u0441\u0442\u0430\u0442\u0438, \u0442\u0430\u043a\u0438\u0435 \u0443\u0447\u0430\u0441\u0442\u043a\u0438 \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 &#8216;Object allocation in a loop&#8217;.<\/p>\n<p>  <\/p>\n<h3 id=\"kogda-staticheskiy-analiz-bessilen\">\u041a\u043e\u0433\u0434\u0430 \u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0430\u043d\u0430\u043b\u0438\u0437 \u0431\u0435\u0441\u0441\u0438\u043b\u0435\u043d<\/h3>\n<p>  <\/p>\n<p>\u0412\u043e\u0441\u044c\u043c\u0430\u044f \u044f\u0432\u0430 \u0434\u0430\u0432\u043d\u043e \u043e\u0442\u0433\u0440\u0435\u043c\u0435\u043b\u0430, \u043d\u043e \u0432\u0441\u0435 \u043c\u044b \u043b\u044e\u0431\u0438\u043c \u0441\u0442\u0440\u0438\u043c\u044b. \u041d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0438\u0437 \u043d\u0430\u0441 \u043b\u044e\u0431\u044f\u0442 \u0438\u0445 \u0442\u0430\u043a \u0441\u0438\u043b\u044c\u043d\u043e, \u0447\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u043f\u043e\u0432\u0441\u044e\u0434\u0443:<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">public Optional&lt;EmailAdresses&gt; getUserEmails() {   Stream&lt;UserEntity&gt; users = getUsers().stream();    if (users.count() == 0) {     return Optional.empty();   }    return users.findAny(); }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0421\u0442\u0440\u0438\u043c, \u043a\u0430\u043a \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e, \u0441\u0432\u0435\u0436 \u0434\u043e \u0432\u044b\u0437\u043e\u0432\u0430 \u043d\u0430 \u043d\u0451\u043c \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0435 \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043a \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 <code>users<\/code> \u0432 \u043d\u0430\u0448\u0435\u043c \u043a\u043e\u0434\u0435 \u043f\u0440\u0438\u0432\u0435\u0434\u0451\u0442 \u043a <code>IllegalStateException<\/code>.<br \/>  \u0421\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0430\u043d\u0430\u043b\u0438\u0437\u0430\u0442\u043e\u0440\u044b \u043f\u043e\u043a\u0430 \u043d\u0435 \u0443\u043c\u0435\u044e\u0442 \u0441\u043e\u043e\u0431\u0449\u0430\u0442\u044c \u043e \u0442\u0430\u043a\u0438\u0445 \u043e\u0448\u0438\u0431\u043a\u0430\u0445, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u0437\u0430 \u0438\u0445 \u0441\u0432\u043e\u0435\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0435 \u0432\u044b\u043b\u0430\u0432\u043b\u0438\u0432\u0430\u043d\u0438\u0435 \u043b\u043e\u0436\u0438\u0442\u0441\u044f \u043d\u0430 \u0440\u0435\u0432\u044c\u044e\u0435\u0440\u0430.<br \/>  \u041c\u043d\u0435 \u043a\u0430\u0436\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0442\u0438\u043f\u0430 <code>Stream<\/code>, \u0440\u0430\u0432\u043d\u043e \u043a\u0430\u043a \u0438\u0445 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0442 \u0438\u0437 \u043c\u0435\u0442\u043e\u0434\u043e\u0432, \u043f\u043e\u0445\u043e\u0436\u0435 \u043d\u0430 \u0445\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u043f\u043e \u043c\u0438\u043d\u043d\u043e\u043c\u0443 \u043f\u043e\u043b\u044e. \u041c\u043e\u0436\u0435\u0442 \u043f\u043e\u0432\u0435\u0437\u0442\u0438, \u0430 \u043c\u043e\u0436\u0435\u0442 \u0438 \u043d\u0435\u0442. \u041e\u0442\u0441\u044e\u0434\u0430 \u043f\u0440\u043e\u0441\u0442\u043e\u0435 \u043f\u0440\u0430\u0432\u0438\u043b\u043e: \u043b\u044e\u0431\u043e\u0435 \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0435 <code>Stream&lt;T&gt;<\/code> \u0432 \u043a\u043e\u0434\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c\u0441\u044f (\u0430 \u043f\u043e-\u0445\u043e\u0440\u043e\u0448\u0435\u043c\u0443 \u0441\u0440\u0430\u0437\u0443 \u0436\u0435 \u0432\u044b\u043f\u0438\u043b\u0438\u0432\u0430\u0442\u044c\u0441\u044f).<\/p>\n<p>  <\/p>\n<h3 id=\"prostye-tipy\">\u041f\u0440\u043e\u0441\u0442\u044b\u0435 \u0442\u0438\u043f\u044b<\/h3>\n<p>  <\/p>\n<p>\u041c\u043d\u043e\u0433\u0438\u0435 \u0443\u0432\u0435\u0440\u0435\u043d\u044b, \u0447\u0442\u043e <code>boolean<\/code>, <code>int<\/code> \u0438 \u0442. \u043f. \u2014 \u044d\u0442\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c. \u041e\u0442\u0447\u0430\u0441\u0442\u0438 \u044d\u0442\u043e \u0442\u0430\u043a, \u043d\u043e \u043a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0442\u0438\u043f <code>not null<\/code> \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e. \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u043e\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u043e\u0435 \u043f\u043e\u043b\u0435 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u0441\u0441\u044b\u043b\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043a\u043e\u043b\u043e\u043d\u043a\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0430 \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u043a\u0430\u043a <code>not null<\/code>, \u0442\u043e \u0438\u043c\u0435\u0435\u0442 \u0441\u043c\u044b\u0441\u043b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>int<\/code>, \u0430 \u043d\u0435 <code>Integer<\/code>. \u042d\u0442\u043e \u0441\u0432\u043e\u0435\u0433\u043e \u0440\u043e\u0434\u0430 \u043a\u043e\u043c\u0431\u043e \u2014 \u0438 \u043f\u043e\u0442\u0440\u0435\u0431\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u043d\u0438\u0436\u0435, \u0438 \u043a\u043e\u0434 \u0443\u043f\u0440\u043e\u0449\u0430\u0435\u0442\u0441\u044f \u0432\u0432\u0438\u0434\u0443 \u043d\u0435\u043d\u0443\u0436\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u043e\u043a \u043d\u0430 <code>null<\/code>.<\/p>\n<p>  <\/p>\n<p>\u041d\u0430 \u044d\u0442\u043e\u043c \u0432\u0441\u0451. \u041f\u043e\u043c\u043d\u0438\u0442\u0435, \u0447\u0442\u043e \u0432\u0441\u0451 \u0432\u044b\u0448\u0435\u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u043e\u0435 \u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0438\u0441\u0442\u0438\u043d\u043e\u0439 \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u0438\u043d\u0441\u0442\u0430\u043d\u0446\u0438\u0438, \u0434\u0443\u043c\u0430\u0439\u0442\u0435 \u0441\u0432\u043e\u0435\u0439 \u0433\u043e\u043b\u043e\u0432\u043e\u0439 \u0438 \u043e\u0441\u043c\u044b\u0441\u043b\u0435\u043d\u043d\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442\u0435 \u043a \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044e \u043b\u044e\u0431\u044b\u0445 \u0441\u043e\u0432\u0435\u0442\u043e\u0432.<\/p>\n<\/div>\n<p>        <script class=\"js-mediator-script\">!function(e){function t(t,n){if(!(n in e)){for(var r,a=e.document,i=a.scripts,o=i.length;o--;)if(-1!==i[o].src.indexOf(t)){r=i[o];break}if(!r){r=a.createElement(\"script\"),r.type=\"text\/javascript\",r.async=!0,r.defer=!0,r.src=t,r.charset=\"UTF-8\";var d=function(){var e=a.getElementsByTagName(\"script\")[0];e.parentNode.insertBefore(r,e)};\"[object Opera]\"==e.opera?a.addEventListener?a.addEventListener(\"DOMContentLoaded\",d,!1):e.attachEvent(\"onload\",d):d()}}}t(\"\/\/mediator.mail.ru\/script\/2820404\/\",\"_mediator\")}(window);<\/script>     <br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/post\/426105\/\"> https:\/\/habr.com\/post\/426105\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\">\n<p>\u0427\u0435\u043c \u0431\u043e\u043b\u044c\u0448\u0435 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043d\u0430\u0434 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435\u043c \u0432 \u043a\u043e\u043c\u0430\u043d\u0434\u0435 \u0438 \u0447\u0435\u043c \u043b\u0443\u0447\u0448\u0435 \u0437\u043d\u0430\u0435\u0442 \u0435\u0433\u043e \u043a\u043e\u0434, \u0442\u0435\u043c \u0447\u0430\u0449\u0435 \u043e\u043d \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442\u0441\u044f \u0432\u044b\u0447\u0438\u0442\u043a\u043e\u0439 \u0442\u0432\u043e\u0440\u0447\u0435\u0441\u0442\u0432\u0430 \u0441\u0432\u043e\u0438\u0445 \u0442\u043e\u0432\u0430\u0440\u0438\u0449\u0435\u0439. \u0421\u0435\u0433\u043e\u0434\u043d\u044f \u044f \u043f\u043e\u043a\u0430\u0436\u0443, \u0447\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0432\u044b\u043b\u043e\u0432\u043b\u0435\u043d\u043e \u0437\u0430 \u043e\u0434\u043d\u0443 \u043d\u0435\u0434\u0435\u043b\u044e \u0432 \u043a\u043e\u0434\u0435, \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u043e\u043c \u0432\u0435\u0441\u044c\u043c\u0430 \u043d\u0435\u043f\u043b\u043e\u0445\u0438\u043c\u0438 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430\u043c\u0438. \u041f\u043e\u0434 \u043a\u0430\u0442\u043e\u043c \u0441\u043e\u0431\u0440\u0430\u043d\u0438\u0435 \u044f\u0440\u043a\u0438\u0445 \u0430\u0440\u0442\u0435\u0444\u0430\u043a\u0442\u043e\u0432 \u043d\u0430\u0448\u0435\u0433\u043e \u0442\u0432\u043e\u0440\u0447\u0435\u0441\u0442\u0432\u0430 (\u0438 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043c\u043e\u0438\u0445 \u0440\u0430\u0437\u043c\u044b\u0448\u043b\u0435\u043d\u0438\u0439).<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-291288","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/291288","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=291288"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/291288\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=291288"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=291288"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=291288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}