{"id":2632,"date":"2025-12-23T10:00:14","date_gmt":"2025-12-23T15:00:14","guid":{"rendered":"http:\/\/www.mymiller.name\/wordpress\/?p=2632"},"modified":"2025-12-23T10:00:14","modified_gmt":"2025-12-23T15:00:14","slug":"list-intersection-union-unique","status":"publish","type":"post","link":"https:\/\/www.mymiller.name\/wordpress\/lists\/list-intersection-union-unique\/","title":{"rendered":"List Intersection, Union, &#038; Unique"},"content":{"rendered":"\n<p>Ever have two lists that you need to pull the unique elements from? Or need to pull the elements in both? Here is a ListUtils class for doing just those steps.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package name.mymiller.extensions.utils;\n\nimport java.util.ArrayList;\nimport java.util.List;\nimport java.util.stream.Collectors;\n\n\/**\n *\n * @author jmiller Provide a set of utilities to use on lists.\n *\/\npublic class ListUtils {\n\n\t\/**\n\t * Find the common elements between two Lists\n\t * \n\t * @param &lt;E> Type used in lists, which must have overridden equals();\n\t * @param a   List A\n\t * @param b   List B\n\t * @return List&lt;E> that contains the common elements between the Lists.\n\t *\/\n\tpublic static &lt;E> List&lt;E> intersection(List&lt;E> a, List&lt;E> b) {\n\t\treturn a.stream().distinct().filter(b::contains).collect(Collectors.toList());\n\t}\n\n\t\/**\n\t * Join two lists together to form a new List\n\t * \n\t * @param &lt;E> Type used in lists, which must have overridden equals();\n\t * @param a   List A\n\t * @param b   List B\n\t * @return List&lt;E> that contains the contents of both Lists.\n\t *\/\n\tpublic static &lt;E> List&lt;E> union(List&lt;E> a, List&lt;E> b) {\n\t\tfinal List&lt;E> unionList = new ArrayList&lt;>(a);\n\t\tunionList.addAll(b);\n\t\treturn unionList.stream().distinct().collect(Collectors.toList());\n\t}\n\n\t\/**\n\t * Create a list of the unique elements in both Lists.\n\t * \n\t * @param &lt;E> Type used in lists, which must have overridden equals();\n\t * @param a   List A\n\t * @param b   List B\n\t * @return List&lt;E> that contains the unique elements not shared between lists.\n\t *\/\n\tpublic static &lt;E> List&lt;E> unique(List&lt;E> a, List&lt;E> b) {\n\t\tfinal List&lt;E> unionList = ListUtils.union(a, b);\n\t\tunionList.removeAll(ListUtils.intersection(a, b));\n\t\treturn unionList.stream().distinct().collect(Collectors.toList());\n\t}\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Ever have two lists that you need to pull the unique elements from? Or need to pull the elements in both? Here is a ListUtils class for doing just those steps.<\/p>\n","protected":false},"author":1,"featured_media":2637,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[455],"tags":[69],"series":[287],"class_list":["post-2632","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-lists","tag-java-2","series-listutils"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2019\/09\/grocery-list-1670408_640.jpg?fit=640%2C426&ssl=1","jetpack-related-posts":[{"id":2739,"url":"https:\/\/www.mymiller.name\/wordpress\/lists\/listutils-page\/","url_meta":{"origin":2632,"position":0},"title":"ListUtils Page","author":"Jeffery Miller","date":"December 24, 2025","format":false,"excerpt":"Ever have a list, that you need to paginate through? I certainly have, I created a simple method to ListUtils to make it easier. \/** * Performs pagination on the List, returning the indicated page of elements, or an empty ist. * @param list List to paginate * @param pageSize\u2026","rel":"","context":"In &quot;Lists&quot;","block_context":{"text":"Lists","link":"https:\/\/www.mymiller.name\/wordpress\/category\/lists\/"},"img":{"alt_text":"Pick a page","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2020\/04\/books-1245690_640.jpg?fit=640%2C426&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2020\/04\/books-1245690_640.jpg?fit=640%2C426&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2020\/04\/books-1245690_640.jpg?fit=640%2C426&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":2711,"url":"https:\/\/www.mymiller.name\/wordpress\/lists\/listutils-size\/","url_meta":{"origin":2632,"position":1},"title":"ListUtils.size()","author":"Jeffery Miller","date":"December 23, 2025","format":false,"excerpt":"Simple yet handy method for your ListUtils class. Handle if null, and return the number of elements in the list. Effective and simple.","rel":"","context":"In &quot;Lists&quot;","block_context":{"text":"Lists","link":"https:\/\/www.mymiller.name\/wordpress\/category\/lists\/"},"img":{"alt_text":"List size","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2020\/04\/number-703136_640.jpg?fit=640%2C426&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2020\/04\/number-703136_640.jpg?fit=640%2C426&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2020\/04\/number-703136_640.jpg?fit=640%2C426&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":2717,"url":"https:\/\/www.mymiller.name\/wordpress\/lists\/listutils-isempty-notempty\/","url_meta":{"origin":2632,"position":2},"title":"ListUtils isEmpty() &#038; notEmpty()","author":"Jeffery Miller","date":"December 23, 2025","format":false,"excerpt":"Standard isEmpty() and notEmpty() methods for a ListUtils class. Nothing fancy, just simple methods and functionality. See the entire ListUtils series.","rel":"","context":"In &quot;Lists&quot;","block_context":{"text":"Lists","link":"https:\/\/www.mymiller.name\/wordpress\/category\/lists\/"},"img":{"alt_text":"empty container?","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2020\/04\/soda-4017612_640.jpg?fit=640%2C429&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2020\/04\/soda-4017612_640.jpg?fit=640%2C429&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2020\/04\/soda-4017612_640.jpg?fit=640%2C429&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":3140,"url":"https:\/\/www.mymiller.name\/wordpress\/lists\/listutils-iterate\/","url_meta":{"origin":2632,"position":3},"title":"ListUtils.iterate()","author":"Jeffery Miller","date":"December 24, 2025","format":false,"excerpt":"Ever have one of those objects that want to give you an iterator and don't have a method for getting it as a list? Well created another new method for my ListUtils class. ListUtils::iteratre() it takes an iterator and converts it into a List for you. \/** * Convert and\u2026","rel":"","context":"In &quot;Lists&quot;","block_context":{"text":"Lists","link":"https:\/\/www.mymiller.name\/wordpress\/category\/lists\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2022\/01\/castles-ga8e4bd9a6_640.jpg?fit=640%2C360&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2022\/01\/castles-ga8e4bd9a6_640.jpg?fit=640%2C360&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2022\/01\/castles-ga8e4bd9a6_640.jpg?fit=640%2C360&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":2736,"url":"https:\/\/www.mymiller.name\/wordpress\/lists\/listutils-safe\/","url_meta":{"origin":2632,"position":4},"title":"ListUtils.safe()","author":"Jeffery Miller","date":"December 24, 2025","format":false,"excerpt":"Sometimes you need to guarantee a list is safe to use. A simple method can make sure your safe to use the list, or have an empty list that would be safe for use. \/** * Guarantees a list will be safe to use, even if null. * @param list\u2026","rel":"","context":"In &quot;Lists&quot;","block_context":{"text":"Lists","link":"https:\/\/www.mymiller.name\/wordpress\/category\/lists\/"},"img":{"alt_text":"Keeping Containers Safe","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2020\/04\/rappelling-755399_640.jpg?fit=640%2C480&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2020\/04\/rappelling-755399_640.jpg?fit=640%2C480&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2020\/04\/rappelling-755399_640.jpg?fit=640%2C480&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":1846,"url":"https:\/\/www.mymiller.name\/wordpress\/challenge\/dividing-game\/","url_meta":{"origin":2632,"position":5},"title":"Dividing Game Interview Coding Sample","author":"Jeffery Miller","date":"August 29, 2016","format":false,"excerpt":"I was recently given the challenge to write an solution for the Dividing Game. \u00a0The objective Player 1 and Player 2 each choose a number. \u00a0The solution should output the number of common divisor's they share in common between the selected numbers. This logic is rather simple, create a list\u2026","rel":"","context":"In &quot;Challenge&quot;","block_context":{"text":"Challenge","link":"https:\/\/www.mymiller.name\/wordpress\/category\/challenge\/"},"img":{"alt_text":"dividing game","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2016\/08\/calculator-1330104_640.jpg?fit=640%2C426&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2016\/08\/calculator-1330104_640.jpg?fit=640%2C426&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2016\/08\/calculator-1330104_640.jpg?fit=640%2C426&ssl=1&resize=525%2C300 1.5x"},"classes":[]}],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/2632","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/comments?post=2632"}],"version-history":[{"count":2,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/2632\/revisions"}],"predecessor-version":[{"id":2634,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/2632\/revisions\/2634"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/media\/2637"}],"wp:attachment":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/media?parent=2632"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/categories?post=2632"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/tags?post=2632"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/series?post=2632"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}