{"id":408733,"date":"2024-06-29T20:21:10","date_gmt":"2024-06-29T20:21:10","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=408733"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2024-06-29T20:21:10","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=408733","title":{"rendered":"<span>\u0415\u0449\u0435 \u0440\u0430\u0437\u2026 \u0432\u043e\u043f\u0440\u043e\u0441 \u0432\u044b\u0431\u043e\u0440\u0430: LinkedList &amp; ArrayList<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439, \u043d\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u043d\u0430 java, \u0441 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u044f \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u043b\u0441\u044f, \u0441\u043f\u043e\u0434\u0432\u0438\u0433\u043b\u043e \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u043d\u043e\u0439 \u0440\u0430\u0437 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u0438\u0442\u044c\u0441\u044f \u043a \u0442\u0435\u043c\u0435 \u0432\u044b\u0431\u043e\u0440\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 List-\u0442\u0430.<\/p>\n<p>\u0418\u0437\u0432\u0435\u0441\u0442\u043d\u043e, \u0447\u0442\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f Linked- \u0438\u043b\u0438 ArrayList-\u0430 \u0432 \u0440\u0430\u0437\u043d\u044b\u0445 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044f\u0445 (\u0432\u0441\u0442\u0430\u0432\u043a\u0430, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435.. \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0443\/\u043d\u0430\u0447\u0430\u043b\u043e\/\u043a\u043e\u043d\u0435\u0446 \u0441\u043f\u0438\u0441\u043a\u0430) \u0432\u0435\u0434\u0435\u0442 \u0441\u0435\u0431\u044f \u043f\u043e \u0440\u0430\u0437\u043d\u043e\u043c\u0443. \u0427\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442 \u0432\u044b\u0431\u043e\u0440 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0435 \u0432\u0441\u0435\u0433\u0434\u0430 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u044b\u043c. Proof?..<\/p>\n<p>\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0437\u0430\u0434\u0430\u0447\u0430 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0441\u043e\u043e\u0431\u0449\u0435\u0441\u0442\u0432 (\u043f\u043e\u0434\u0433\u0440\u0430\u0444\u043e\u0432). \u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435:<\/p>\n<p>\u0430) \u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0444\u0430\u0439\u043b \u0441\u043e \u0441\u0442\u0440\u043e\u043a\u0430\u043c\u0438 \u0432\u0438\u0434\u0430<\/p>\n<pre><code>A1;C2;F4 C2;Z4;N2 N2;H1;L8 T7;O7 ...<\/code><\/pre>\n<p>\u0431) \u043d\u0430\u0439\u0442\u0438 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0445 \u0441\u0442\u0440\u043e\u043a \u0438 \u0440\u0430\u0437\u0431\u0438\u0442\u044c \u0435\u0433\u043e \u043d\u0430 \u043d\u0435\u043f\u0435\u0440\u0435\u0441\u0435\u043a\u0430\u044e\u0449\u0438\u0435\u0441\u044f \u0433\u0440\u0443\u043f\u043f\u044b \u043f\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u043a\u0440\u0438\u0442\u0435\u0440\u0438\u044e:<\/p>\n<p>&#8212; \u0435\u0441\u043b\u0438 \u0441\u0442\u0440\u043e\u0447\u043a\u0438 \u0438\u043c\u0435\u044e\u0442 \u0441\u043e\u0432\u043f\u0430\u0434\u0435\u043d\u0438\u044f \u043d\u0435\u043f\u0443\u0441\u0442\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0432 \u043e\u0434\u043d\u043e\u0439 \u0438\u043b\u0438 \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u043b\u043e\u043d\u043a\u0430\u0445,<\/p>\n<p>\u043e\u043d\u0438 \u043f\u0440\u0438\u043d\u0430\u0434\u043b\u0435\u0436\u0430\u0442 \u043e\u0434\u043d\u043e\u0439 \u0433\u0440\u0443\u043f\u043f\u0435. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0441\u0442\u0440\u043e\u0447\u043a\u0438:<\/p>\n<pre><code>111;123;222 200;123;100 300;;100<\/code><\/pre>\n<p>\u0432\u0441\u0435 \u043f\u0440\u0438\u043d\u0430\u0434\u043b\u0435\u0436\u0430\u0442 \u043e\u0434\u043d\u043e\u0439 \u0433\u0440\u0443\u043f\u043f\u0435, \u0442\u0430\u043a \u043a\u0430\u043a \u043f\u0435\u0440\u0432\u044b\u0435 \u0434\u0432\u0435 &#8212; \u0438\u043c\u0435\u044e\u0442 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 123 \u0432\u043e \u0432\u0442\u043e\u0440\u043e\u0439 \u043a\u043e\u043b\u043e\u043d\u043a\u0435, \u0430 \u0434\u0432\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0435 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 100 \u0432 \u0442\u0440\u0435\u0442\u044c\u0435\u0439 \u043a\u043e\u043b\u043e\u043d\u043a\u0435. <\/p>\n<p>\u041d\u0430 \u0432\u0445\u043e\u0434 \u043a\u043e\u043d\u0441\u043e\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043f\u043e\u0434\u0430\u0435\u043c \u0444\u0430\u0439\u043b (csv) c \u0431\u043e\u043b\u0435\u0435 \u0447\u0435\u043c \u043c\u043b\u043d. \u0441\u0442\u0440\u043e\u043a.  \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 JGraphT:<\/p>\n<pre><code class=\"java\">@Component public class Processing {      @Autowired     FiosUtil fiosUtil;      public Graph&lt;String, DefaultEdge> graph = new SimpleGraph&lt;>(DefaultEdge.class);     DefaultDirectedWeightedGraph     public Map&lt;String, List&lt;List&lt;String>>> groupMap = new HashMap&lt;>();     public List&lt;List&lt;List&lt;String>>> groupList = new LinkedList&lt;>();     private Integer groupCounter = 1;      public void process() throws IOException {         BufferedReader csvReader = new BufferedReader(new InputStreamReader(fiosUtil.getResourceFileStream()));         String row;         while ((row = csvReader.readLine()) != null) {             List&lt;String> rowSet = fiosUtil.getRowSet(row);             createGraph(rowSet);         }         csvReader.close();         System.out.println(\">> Created graph. Graph vertexes are: \" + graph.vertexSet().size() + \"\\t\\t(unique digits in all row's)\");         boolean isEndOfGroups = graphIterate(graph);         while (!isEndOfGroups) {             isEndOfGroups = graphIterate(graph);         }         outOfGroups();     }      public void createGraph(List&lt;String> row) {         List&lt;List&lt;String>> list = new ArrayList&lt;>();         list.add(row);         String initItem = \"\";         if (!row.isEmpty()) initItem = row.get(0);         for (String item : row) {             if (!item.isEmpty()) {                 graph.addVertex(item);                 groupMap.merge(item, list, (x, y) -> {                     x.addAll(y);                     return x.stream().distinct().collect(Collectors.toList());                 });                 if (!initItem.isEmpty() &amp;&amp; !initItem.equals(item)) {                     graph.addEdge(initItem, item);                     initItem = item;                 }             }         }         if(row.size() == 3) graph.addEdge(row.get(0), row.get(row.size()-1));     }      public boolean graphIterate(Graph&lt;String, DefaultEdge> graph) {         List&lt;List&lt;String>> groupRows = new LinkedList&lt;>();         List&lt;String> groupVertex = new ArrayList&lt;>();         Iterator&lt;String> graphIterator = graph.vertexSet().iterator();         if (graphIterator.hasNext()) {             DepthFirstIterator&lt;String, DefaultEdge> depthFirstIterator = new DepthFirstIterator&lt;>(graph, graphIterator.next());             while (depthFirstIterator.hasNext()) {                 String vertex = depthFirstIterator.next();                 groupVertex.add(vertex);             }             for (String vertex : groupVertex) {                 groupRows.addAll(new ArrayList&lt;>(groupMap.get(vertex)));             }         }         removeGraphEntrySaveRows(groupRows);         return graph.vertexSet().size() == 0;     }      private void removeGraphEntrySaveRows(List&lt;List&lt;String>> group){         List&lt;List&lt;String>> rowsGroup = group.stream()                 .peek(row -> row.forEach(vertex -> graph.removeVertex(vertex)))                 .distinct()                 .sorted(Comparator.comparingInt(List::size))                 .collect(Collectors.toList());         groupList.add(rowsGroup);     }      public void outOfGroups(){         Iterator&lt;List&lt;List&lt;String>>> groupsIterator = groupList.iterator();         while (groupsIterator.hasNext()){             List&lt;List&lt;String>> currentGroup = groupsIterator.next();             groupList.stream()                     .filter(iterateGroup -> iterateGroup.equals(currentGroup))                     .peek(iterateGroup -> {                         iterateGroup.addAll(currentGroup);                         groupList.remove(currentGroup);                     });         }          for (List&lt;List&lt;String>> rowsGroup : groupList){             System.out.println(\"\\nGroup&lt;\" + groupCounter++ + \">:\" +                     \"\\n----------------------------------------------------\");             rowsGroup.forEach(System.out::println);         }     } }<\/code><\/pre>\n<p>\u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e, \u0432 \u043a\u0430\u0436\u0434\u043e\u0439 \u0432\u0445\u043e\u0434\u044f\u0449\u0435\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0441\u043e\u0437\u0434\u0430\u0435\u043c vertex \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043c\u0435\u0436\u0434\u0443 \u043d\u0438\u043c\u0438 edges. \u0417\u0430\u0442\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u043c \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0439 \u0433\u0440\u0430\u0444 DepthFirstIterator-\u0438\u0442\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u043c, \u0438\u0441\u043a\u043b\u044e\u0447\u0430\u044f \u043f\u0440\u043e\u0439\u0434\u0435\u043d\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0438 \u043d\u0430\u0445\u043e\u0434\u0438\u043c \u0442.\u043e., \u043e\u0447\u0435\u0440\u0435\u0434\u043d\u043e\u0439 \u043f\u043e\u0434\u0433\u0440\u0430\u0444 (\u0438\u043b\u0438 \u043f\u043e\u0434\u0433\u0440\u0443\u043f\u043f\u0443, \u0441\u043e\u0433\u043b\u0430\u0441\u043d\u043e \u0443\u0441\u043b\u043e\u0432\u0438\u044e \u0437\u0430\u0434\u0430\u0447\u0438). <\/p>\n<p>\u041f\u0440\u0438 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0430 \u0441\u0440\u0435\u0434\u043d\u0435\u043c \u043d\u043e\u0443\u0442\u0431\u0443\u043a\u0435 \u043c\u044b \u0432\u043f\u043e\u043b\u043d\u0435 \u0443\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0435\u043c\u0441\u044f \u0432 30 sec. \u041a\u0430\u043a \u0434\u0443\u043c\u0430\u0435\u0442\u0435, \u043d\u0430 \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u0441\u044f \u0432\u0440\u0435\u043c\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f, \u0435\u0441\u043b\u0438 \u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e groupList \u043d\u0430 \u00a0ArrayList&lt;>()? <\/p>\n<p><a href=\"https:\/\/github.com\/Alexandr179\/LngJava\/tree\/graph_impl\" rel=\"noopener noreferrer nofollow\">\u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 GitHub<\/a>  <\/p>\n<p><a href=\"https:\/\/github.com\/Alexandr179\/LngJava\/tree\/master\" rel=\"noopener noreferrer nofollow\">\u0412\u0430\u0440\u0438\u0430\u043d\u0442 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0447\u0435\u0440\u0435\u0437 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c<\/a><\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><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\/ru\/articles\/742392\/\"> https:\/\/habr.com\/ru\/articles\/742392\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439, \u043d\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u043d\u0430 java, \u0441 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u044f \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u043b\u0441\u044f, \u0441\u043f\u043e\u0434\u0432\u0438\u0433\u043b\u043e \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u043d\u043e\u0439 \u0440\u0430\u0437 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u0438\u0442\u044c\u0441\u044f \u043a \u0442\u0435\u043c\u0435 \u0432\u044b\u0431\u043e\u0440\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 List-\u0442\u0430.<\/p>\n<p>\u0418\u0437\u0432\u0435\u0441\u0442\u043d\u043e, \u0447\u0442\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f Linked- \u0438\u043b\u0438 ArrayList-\u0430 \u0432 \u0440\u0430\u0437\u043d\u044b\u0445 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044f\u0445 (\u0432\u0441\u0442\u0430\u0432\u043a\u0430, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435.. \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0443\/\u043d\u0430\u0447\u0430\u043b\u043e\/\u043a\u043e\u043d\u0435\u0446 \u0441\u043f\u0438\u0441\u043a\u0430) \u0432\u0435\u0434\u0435\u0442 \u0441\u0435\u0431\u044f \u043f\u043e \u0440\u0430\u0437\u043d\u043e\u043c\u0443. \u0427\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442 \u0432\u044b\u0431\u043e\u0440 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0435 \u0432\u0441\u0435\u0433\u0434\u0430 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u044b\u043c. Proof?..<\/p>\n<p>\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0437\u0430\u0434\u0430\u0447\u0430 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0441\u043e\u043e\u0431\u0449\u0435\u0441\u0442\u0432 (\u043f\u043e\u0434\u0433\u0440\u0430\u0444\u043e\u0432). \u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435:<\/p>\n<p>\u0430) \u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0444\u0430\u0439\u043b \u0441\u043e \u0441\u0442\u0440\u043e\u043a\u0430\u043c\u0438 \u0432\u0438\u0434\u0430<\/p>\n<pre><code>A1;C2;F4 C2;Z4;N2 N2;H1;L8 T7;O7 ...<\/code><\/pre>\n<p>\u0431) \u043d\u0430\u0439\u0442\u0438 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0445 \u0441\u0442\u0440\u043e\u043a \u0438 \u0440\u0430\u0437\u0431\u0438\u0442\u044c \u0435\u0433\u043e \u043d\u0430 \u043d\u0435\u043f\u0435\u0440\u0435\u0441\u0435\u043a\u0430\u044e\u0449\u0438\u0435\u0441\u044f \u0433\u0440\u0443\u043f\u043f\u044b \u043f\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u043a\u0440\u0438\u0442\u0435\u0440\u0438\u044e:<\/p>\n<p>&#8212; \u0435\u0441\u043b\u0438 \u0441\u0442\u0440\u043e\u0447\u043a\u0438 \u0438\u043c\u0435\u044e\u0442 \u0441\u043e\u0432\u043f\u0430\u0434\u0435\u043d\u0438\u044f \u043d\u0435\u043f\u0443\u0441\u0442\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0432 \u043e\u0434\u043d\u043e\u0439 \u0438\u043b\u0438 \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u043b\u043e\u043d\u043a\u0430\u0445,<\/p>\n<p>\u043e\u043d\u0438 \u043f\u0440\u0438\u043d\u0430\u0434\u043b\u0435\u0436\u0430\u0442 \u043e\u0434\u043d\u043e\u0439 \u0433\u0440\u0443\u043f\u043f\u0435. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0441\u0442\u0440\u043e\u0447\u043a\u0438:<\/p>\n<pre><code>111;123;222 200;123;100 300;;100<\/code><\/pre>\n<p>\u0432\u0441\u0435 \u043f\u0440\u0438\u043d\u0430\u0434\u043b\u0435\u0436\u0430\u0442 \u043e\u0434\u043d\u043e\u0439 \u0433\u0440\u0443\u043f\u043f\u0435, \u0442\u0430\u043a \u043a\u0430\u043a \u043f\u0435\u0440\u0432\u044b\u0435 \u0434\u0432\u0435 &#8212; \u0438\u043c\u0435\u044e\u0442 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 123 \u0432\u043e \u0432\u0442\u043e\u0440\u043e\u0439 \u043a\u043e\u043b\u043e\u043d\u043a\u0435, \u0430 \u0434\u0432\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0435 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 100 \u0432 \u0442\u0440\u0435\u0442\u044c\u0435\u0439 \u043a\u043e\u043b\u043e\u043d\u043a\u0435. <\/p>\n<p>\u041d\u0430 \u0432\u0445\u043e\u0434 \u043a\u043e\u043d\u0441\u043e\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043f\u043e\u0434\u0430\u0435\u043c \u0444\u0430\u0439\u043b (csv) c \u0431\u043e\u043b\u0435\u0435 \u0447\u0435\u043c \u043c\u043b\u043d. \u0441\u0442\u0440\u043e\u043a.  \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 JGraphT:<\/p>\n<pre><code class=\"java\">@Component public class Processing {      @Autowired     FiosUtil fiosUtil;      public Graph&lt;String, DefaultEdge> graph = new SimpleGraph&lt;>(DefaultEdge.class);     DefaultDirectedWeightedGraph     public Map&lt;String, List&lt;List&lt;String>>> groupMap = new HashMap&lt;>();     public List&lt;List&lt;List&lt;String>>> groupList = new LinkedList&lt;>();     private Integer groupCounter = 1;      public void process() throws IOException {         BufferedReader csvReader = new BufferedReader(new InputStreamReader(fiosUtil.getResourceFileStream()));         String row;         while ((row = csvReader.readLine()) != null) {             List&lt;String> rowSet = fiosUtil.getRowSet(row);             createGraph(rowSet);         }         csvReader.close();         System.out.println(\">> Created graph. Graph vertexes are: \" + graph.vertexSet().size() + \"\\t\\t(unique digits in all row's)\");         boolean isEndOfGroups = graphIterate(graph);         while (!isEndOfGroups) {             isEndOfGroups = graphIterate(graph);         }         outOfGroups();     }      public void createGraph(List&lt;String> row) {         List&lt;List&lt;String>> list = new ArrayList&lt;>();         list.add(row);         String initItem = \"\";         if (!row.isEmpty()) initItem = row.get(0);         for (String item : row) {             if (!item.isEmpty()) {                 graph.addVertex(item);                 groupMap.merge(item, list, (x, y) -> {                     x.addAll(y);                     return x.stream().distinct().collect(Collectors.toList());                 });                 if (!initItem.isEmpty() &amp;&amp; !initItem.equals(item)) {                     graph.addEdge(initItem, item);                     initItem = item;                 }             }         }         if(row.size() == 3) graph.addEdge(row.get(0), row.get(row.size()-1));     }      public boolean graphIterate(Graph&lt;String, DefaultEdge> graph) {         List&lt;List&lt;String>> groupRows = new LinkedList&lt;>();         List&lt;String> groupVertex = new ArrayList&lt;>();         Iterator&lt;String> graphIterator = graph.vertexSet().iterator();         if (graphIterator.hasNext()) {             DepthFirstIterator&lt;String, DefaultEdge> depthFirstIterator = new DepthFirstIterator&lt;>(graph, graphIterator.next());             while (depthFirstIterator.hasNext()) {                 String vertex = depthFirstIterator.next();                 groupVertex.add(vertex);             }             for (String vertex : groupVertex) {                 groupRows.addAll(new ArrayList&lt;>(groupMap.get(vertex)));             }         }         removeGraphEntrySaveRows(groupRows);         return graph.vertexSet().size() == 0;     }      private void removeGraphEntrySaveRows(List&lt;List&lt;String>> group){         List&lt;List&lt;String>> rowsGroup = group.stream()                 .peek(row -> row.forEach(vertex -> graph.removeVertex(vertex)))                 .distinct()                 .sorted(Comparator.comparingInt(List::size))                 .collect(Collectors.toList());         groupList.add(rowsGroup);     }      public void outOfGroups(){         Iterator&lt;List&lt;List&lt;String>>> groupsIterator = groupList.iterator();         while (groupsIterator.hasNext()){             List&lt;List&lt;String>> currentGroup = groupsIterator.next();             groupList.stream()                     .filter(iterateGroup -> iterateGroup.equals(currentGroup))                     .peek(iterateGroup -> {                         iterateGroup.addAll(currentGroup);                         groupList.remove(currentGroup);                     });         }          for (List&lt;List&lt;String>> rowsGroup : groupList){             System.out.println(\"\\nGroup&lt;\" + groupCounter++ + \">:\" +                     \"\\n----------------------------------------------------\");             rowsGroup.forEach(System.out::println);         }     } }<\/code><\/pre>\n<p>\u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e, \u0432 \u043a\u0430\u0436\u0434\u043e\u0439 \u0432\u0445\u043e\u0434\u044f\u0449\u0435\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0441\u043e\u0437\u0434\u0430\u0435\u043c vertex \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043c\u0435\u0436\u0434\u0443 \u043d\u0438\u043c\u0438 edges. \u0417\u0430\u0442\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u043c \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0439 \u0433\u0440\u0430\u0444 DepthFirstIterator-\u0438\u0442\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u043c, \u0438\u0441\u043a\u043b\u044e\u0447\u0430\u044f \u043f\u0440\u043e\u0439\u0434\u0435\u043d\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0438 \u043d\u0430\u0445\u043e\u0434\u0438\u043c \u0442.\u043e., \u043e\u0447\u0435\u0440\u0435\u0434\u043d\u043e\u0439 \u043f\u043e\u0434\u0433\u0440\u0430\u0444 (\u0438\u043b\u0438 \u043f\u043e\u0434\u0433\u0440\u0443\u043f\u043f\u0443, \u0441\u043e\u0433\u043b\u0430\u0441\u043d\u043e \u0443\u0441\u043b\u043e\u0432\u0438\u044e \u0437\u0430\u0434\u0430\u0447\u0438). <\/p>\n<p>\u041f\u0440\u0438 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0430 \u0441\u0440\u0435\u0434\u043d\u0435\u043c \u043d\u043e\u0443\u0442\u0431\u0443\u043a\u0435 \u043c\u044b \u0432\u043f\u043e\u043b\u043d\u0435 \u0443\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0435\u043c\u0441\u044f \u0432 30 sec. \u041a\u0430\u043a \u0434\u0443\u043c\u0430\u0435\u0442\u0435, \u043d\u0430 \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u0441\u044f \u0432\u0440\u0435\u043c\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f, \u0435\u0441\u043b\u0438 \u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e groupList \u043d\u0430 \u00a0ArrayList&lt;>()? <\/p>\n<p><a href=\"https:\/\/github.com\/Alexandr179\/LngJava\/tree\/graph_impl\" rel=\"noopener noreferrer nofollow\">\u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 GitHub<\/a>  <\/p>\n<p><a href=\"https:\/\/github.com\/Alexandr179\/LngJava\/tree\/master\" rel=\"noopener noreferrer nofollow\">\u0412\u0430\u0440\u0438\u0430\u043d\u0442 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0447\u0435\u0440\u0435\u0437 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c<\/a><\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><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\/ru\/articles\/742392\/\"> https:\/\/habr.com\/ru\/articles\/742392\/<\/a><br \/><\/br><\/br><\/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-408733","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/408733","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=408733"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/408733\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=408733"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=408733"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=408733"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}