<?xml version='1.0' encoding="utf-8"?>
      <rss version='2.0'>
      <channel>
      <title>Форум на Исходниках.RU</title>
      <link>https://forum.sources.ru</link>
      <description>Форум на Исходниках.RU</description>
      <generator>Форум на Исходниках.RU</generator>
  	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=415897&amp;view=findpost&amp;p=3811837</guid>
        <pubDate>Fri, 04 Oct 2019 07:31:22 +0000</pubDate>
        <title>Поиск value в Map&amp;lt;String, Object&amp;gt; и последующая замена</title>
        <link>https://forum.sources.ru/index.php?showtopic=415897&amp;view=findpost&amp;p=3811837</link>
        <description><![CDATA[DRON666: Ого, очень интересно. О таком функциональном решении я не думал, углубиться в стримы пришлось, спасибо.]]></description>
        <author>DRON666</author>
        <category>Java</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=415897&amp;view=findpost&amp;p=3811658</guid>
        <pubDate>Wed, 02 Oct 2019 19:33:10 +0000</pubDate>
        <title>Поиск value в Map&amp;lt;String, Object&amp;gt; и последующая замена</title>
        <link>https://forum.sources.ru/index.php?showtopic=415897&amp;view=findpost&amp;p=3811658</link>
        <description><![CDATA[korvin: <div class='tag-quote'><a class='tag-quote-link' href='https://forum.sources.ru/index.php?showtopic=415897&view=findpost&p=3811627'><span class='tag-quote-prefix'>Цитата</span></a> <span class='tag-quote__quote-info'>DRON666 &#064; <time class="tag-quote__quoted-time" datetime="2019-10-02T11:22:58+00:00">02.10.19, 11:22</time></span><div class='quote '>Думал насчет стримов (Stream API)</div></div><br>
Они тут ничем не помогут.<br>
<br>
<div class='tag-quote'><a class='tag-quote-link' href='https://forum.sources.ru/index.php?showtopic=415897&view=findpost&p=3811627'><span class='tag-quote-prefix'>Цитата</span></a> <span class='tag-quote__quote-info'>DRON666 &#064; <time class="tag-quote__quoted-time" datetime="2019-10-02T11:22:58+00:00">02.10.19, 11:22</time></span><div class='quote '>Код получается не сказать что сложный, но не маленький.</div></div><br>
Да не, вполне небольшой.<br>
<br>
Зачем кидать исключение на непонятный объект? Может его просто пропускать мимо вместе с Long&#39;ами? <br>
<br>
<span class="tag-color tag-color-named" data-value="mergepost" style="color: mergepost"><span class='tag-size' data-value='7' style='font-size:7pt;'>Добавлено <time class="tag-mergetime" datetime="2019-10-02T20:19:54+00:00">02.10.19, 20:19</time></span></span><br>
Ну например:<br>
<br>
<div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">import java.util.List;</div><div class="code_line">import java.util.Map;</div><div class="code_line">import java.util.Map.Entry;</div><div class="code_line">import java.util.TreeMap;</div><div class="code_line">import java.util.function.UnaryOperator;</div><div class="code_line">import java.util.regex.Pattern;</div><div class="code_line">&nbsp;</div><div class="code_line">import static java.util.stream.Collectors.toList;</div><div class="code_line">import static java.util.stream.Collectors.toMap;</div><div class="code_line">&nbsp;</div><div class="code_line">final class FindInMap {</div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp;public static void main(String[] args) {</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;final Map&#60;String, Object&#62; map = Map.of(</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&quot;foo&quot;, &quot;bar&quot;,</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&quot;Foo&quot;, &quot;Bar&quot;,</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&quot;FOO&quot;, &quot;BAZ&quot;,</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&quot;gee&quot;, List.of(123, &quot;bar&quot;, &quot;BAZ&quot;),</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&quot;qux&quot;, Map.of(&quot;foo&quot;, &quot;Bar&quot;, &quot;gee&quot;, 567)</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;);</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;printSortedMap(map);</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;final var pattern = Pattern.compile(&quot;([bB])ar&quot;);</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;final var aaargh = new Updater&#60;&#62;(String.class, s -&#62; pattern.matcher(s).matches()</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;? s.replace(&quot;ar&quot;, &quot;AAARGH!!!&quot;)</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: s);</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;final var aaarghedMap = aaargh.update(map);</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;printSortedMap(aaarghedMap);</div><div class="code_line">&nbsp;&nbsp; &nbsp;}</div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp;private static &#60;K, V&#62; void printSortedMap(Map&#60;K, V&#62; map) {</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;System.out.println(new TreeMap&#60;&#62;(map));</div><div class="code_line">&nbsp;&nbsp; &nbsp;}</div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp;static final class Updater&#60;T&#62; {</div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;private final Class&#60;T&#62; cls;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;private final UnaryOperator&#60;T&#62; mapper;</div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Updater(Class&#60;T&#62; cls, UnaryOperator&#60;T&#62; mapper) {</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.cls = cls;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.mapper = mapper;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}</div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Map&#60;String, Object&#62; update(Map&#60;String, Object&#62; map) {</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return map.entrySet().stream()</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.map(this::update)</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.collect(toMap(Entry::getKey, Entry::getValue));</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}</div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;List&#60;Object&#62; update(List&#60;Object&#62; list) {</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return list.stream()</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.map(this::update)</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.collect(toList());</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}</div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Entry&#60;String, Object&#62; update(Entry&#60;String, Object&#62; entry) {</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return new Pair&#60;&#62;(entry.getKey(), update(entry.getValue()));</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}</div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;Object update(Object o) {</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (cls.isAssignableFrom(o.getClass())) {</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@SuppressWarnings(&quot;unchecked&quot;)</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;final var value = (T) o;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return mapper.apply(value);</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} else if (o instanceof List) {</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@SuppressWarnings(&quot;unchecked&quot;)</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;final var list = (List&#60;Object&#62;) o;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return update(list);</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} else if (o instanceof Map) {</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@SuppressWarnings(&quot;unchecked&quot;)</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;final var map = (Map&#60;String, Object&#62;) o;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return update(map);</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} else {</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return o;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}</div><div class="code_line">&nbsp;&nbsp; &nbsp;}</div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp;private static final class Pair&#60;A, B&#62; implements Entry&#60;A, B&#62; {</div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;private final A first;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;private final B second;</div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;private Pair(A first, B second) {</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.first = first;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.second = second;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}</div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;@Override</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;public A getKey() {</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return first;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}</div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;@Override</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;public B getValue() {</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return second;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}</div><div class="code_line">&nbsp;</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;@Override</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;public B setValue(B value) {</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;throw new UnsupportedOperationException(&quot;Immutable object&quot;);</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}</div><div class="code_line">&nbsp;&nbsp; &nbsp;}</div><div class="code_line">}</div></ol></div></div></div></div><script>preloadCodeButtons('1');</script><br>
<br>
=&gt;<br>
<br>
<div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">{FOO=BAZ, Foo=Bar, foo=bar, gee=[123, bar, BAZ], qux={gee=567, foo=Bar}}</div><div class="code_line">{FOO=BAZ, Foo=BAAARGH!!!, foo=bAAARGH!!!, gee=[123, bAAARGH!!!, BAZ], qux={gee=567, foo=BAAARGH!!!}}</div></ol></div></div></div></div>]]></description>
        <author>korvin</author>
        <category>Java</category>
      </item>
	
      <item>
        <guid isPermaLink='true'>https://forum.sources.ru/index.php?showtopic=415897&amp;view=findpost&amp;p=3811627</guid>
        <pubDate>Wed, 02 Oct 2019 11:22:58 +0000</pubDate>
        <title>Поиск value в Map&amp;lt;String, Object&amp;gt; и последующая замена</title>
        <link>https://forum.sources.ru/index.php?showtopic=415897&amp;view=findpost&amp;p=3811627</link>
        <description><![CDATA[DRON666: Всем привет, кто еще здесь остался :lol: &#33;  Задача в следующем. Есть сложная мапа: Map&lt;String, Object&gt; где <strong class='tag-b'>Object</strong> <em class='tag-i'>может быть</em> <strong class='tag-b'>String</strong>, <strong class='tag-b'>Long</strong>, <strong class='tag-b'>List&lt;Object&gt;</strong>, <strong class='tag-b'>Map&lt;String, Object&gt;</strong>. Так вот, в дебрях этой мапы мне надо найти все значения, которые являются <strong class='tag-b'>String</strong> и которые удовлетворяют некому условию, например, регулярному выражению, и произвести замену в строке (заменить часть строки), обновив исходный Map. Уровень вложенности объектов не большой - не более 4 уровней. Как заменить value в Map или List я знаю. Вопрос в том, как найти. Ничего умнее рекурсивного поиска в лоб я не придумал с базовой конструкцией вида:<br>
<div class='tag-code'><span class='pre_code'></span><div class='code  code_collapsed ' title='Подсветка синтаксиса доступна зарегистрированным участникам Форума.' style=''><div><div><ol type="1"><div class="code_line">void findInMap(Map&#60;String, Object&#62; map){</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;for(Map.Entry&#60;String, Object&#62; entry: map.entrySet()){</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Object objValue = entry.getValue();</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(objValue instanceof String){</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //определяем, соответствует ли строка условию,</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //если соответствует, производим замену</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}else if(objValue instanceof Long){</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //пропускаем</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}else if(objValue instanceof List){</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //ищем в List&#39;е, отдельный метод, но по сути тоже самое что и findInMap</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}else if(objValue instanceof Map){</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //ищем в Map</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; findInMap( (Map&#60;String, Object&#62;)objValue ); </div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}else{</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //не понятный объект, кидаем исключение</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</div><div class="code_line">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}</div><div class="code_line">}</div></ol></div></div></div></div><br>
<br>
Код получается не сказать что сложный, но не маленький. Может есть более красивое решение? Думал насчет стримов (Stream API), но так и не придумал как их использовать для данной задачи, правда я и не очень хорошо эти стримы понимаю.]]></description>
        <author>DRON666</author>
        <category>Java</category>
      </item>
	
      </channel>
      </rss>
	