{"id":5276,"date":"2012-04-11T19:09:18","date_gmt":"2012-04-11T13:39:18","guid":{"rendered":"https:\/\/2thenew.xyz\/blog\/?p=5276"},"modified":"2016-12-19T15:30:08","modified_gmt":"2016-12-19T10:00:08","slug":"dealing-with-immutable-collections-in-java","status":"publish","type":"post","link":"https:\/\/2thenew.xyz\/blog\/dealing-with-immutable-collections-in-java\/","title":{"rendered":"Dealing with immutable collections in Java"},"content":{"rendered":"<p>Perhaps, you know it before or just skipped it after going through Java API. But I really found it very helpful at this point.<\/p>\n<p>&nbsp;<\/p>\n<p>Recently, I had a requirement in an open-source project, where users can iterate through the objects saved in collections (esp. List). But the problem was, we really didn\u2019t want users to directly or indirectly update those collection objects. Or in other words, we wanted to provide them collections with Read-Only behavior.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>So to fulfill this requirement,<\/strong> I had few solutions &#8211;<br \/>\n1) Writing a wrapper class for iterating the objects in collection, in a manner that it behaves like Immutable collection objects.<br \/>\n2) Implementing your own Collection classes to support read-only behavior.<br \/>\n3) Returning the output as Iterable.<br \/>\n4) And many more &#8211; as complicated as we want to make it.<\/p>\n<p>&nbsp;<\/p>\n<p>Well, need not to worry guys!<br \/>\n<strong>Java provides an in-built class &#8220;<a href=\"http:\/\/docs.oracle.com\/javase\/1.5.0\/docs\/api\/java\/util\/Collections.html\">java.util.Collections<\/a>&#8220;<\/strong> &#8211; to deal with such kind of problems.<\/p>\n<p>Collections class comes up with a good list of static methods. Like, for converting a modifiable list to unmodifiable list (read-only list), we have following method:<br \/>\n[java]<br \/>\nList myUnmodifiableList = Collections.unmodifiableList(myModifiableList)<br \/>\n[\/java]<\/p>\n<p>and now, if anyone will try to modify &#8216;myUnmodifiableList&#8217;, it will throw <strong>UnsupportedOperationException<\/strong>.<br \/>\nSimilarly, there are other methods to handle different types of collections.<\/p>\n<p>&nbsp;<\/p>\n<p>I hope this might help someone.<\/p>\n<p>&nbsp;<\/p>\n<p>Please put your comment, if there&#8217;s any better way out there.<\/p>\n<p>&nbsp;<\/p>\n<p>Cheers!<br \/>\nSalil Kalia<br \/>\nSalil [at] IntelliGrape [dot] com<br \/>\nTwitter LinkedIn<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Perhaps, you know it before or just skipped it after going through Java API. But I really found it very helpful at this point. &nbsp; Recently, I had a requirement in an open-source project, where users can iterate through the objects saved in collections (esp. List). But the problem was, we really didn\u2019t want users [&hellip;]<\/p>\n","protected":false},"author":17,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1,"footnotes":""},"categories":[7],"tags":[504,9,4844],"class_list":["post-5276","post","type-post","status-publish","format-standard","hentry","category-grails","tag-collections","tag-groovy","tag-java"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"Perhaps, you know it before or just skipped it after going through Java API. But I really found it very helpful at this point. Recently, I had a requirement in an open-source project, where users can iterate through the objects saved in collections (esp. List). But the problem was, we really didn\u2019t want users\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Salil\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/2thenew.xyz\/blog\/dealing-with-immutable-collections-in-java\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.0.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"TO THE NEW BLOG\" \/>\n\t\t<meta property=\"og:type\" content=\"blog\" \/>\n\t\t<meta property=\"og:title\" content=\"Dealing with immutable collections in Java | TO THE NEW Blog\" \/>\n\t\t<meta property=\"og:description\" content=\"Perhaps, you know it before or just skipped it after going through Java API. But I really found it very helpful at this point. Recently, I had a requirement in an open-source project, where users can iterate through the objects saved in collections (esp. List). But the problem was, we really didn\u2019t want users\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/2thenew.xyz\/blog\/dealing-with-immutable-collections-in-java\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/2thenew.xyz\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/2thenew.xyz\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:site\" content=\"@tothenew\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Dealing with immutable collections in Java | TO THE NEW Blog\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Perhaps, you know it before or just skipped it after going through Java API. But I really found it very helpful at this point. Recently, I had a requirement in an open-source project, where users can iterate through the objects saved in collections (esp. List). But the problem was, we really didn\u2019t want users\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/2thenew.xyz\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/dealing-with-immutable-collections-in-java\\\/#article\",\"name\":\"Dealing with immutable collections in Java | TO THE NEW Blog\",\"headline\":\"Dealing with immutable collections in Java\",\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/salil\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"},\"datePublished\":\"2012-04-11T19:09:18+05:30\",\"dateModified\":\"2016-12-19T15:30:08+05:30\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/dealing-with-immutable-collections-in-java\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/dealing-with-immutable-collections-in-java\\\/#webpage\"},\"articleSection\":\"Grails, collections, Groovy, Java\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/dealing-with-immutable-collections-in-java\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/#listItem\",\"name\":\"Grails\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/#listItem\",\"position\":2,\"name\":\"Grails\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/dealing-with-immutable-collections-in-java\\\/#listItem\",\"name\":\"Dealing with immutable collections in Java\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/dealing-with-immutable-collections-in-java\\\/#listItem\",\"position\":3,\"name\":\"Dealing with immutable collections in Java\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/#listItem\",\"name\":\"Grails\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\",\"name\":\"TO THE NEW Blog\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/salil\\\/#author\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/salil\\\/\",\"name\":\"Salil\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/dealing-with-immutable-collections-in-java\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/97e216cba95b296d5909ad7b93d1c338d0fdad7b95410c9412b126a90f4735d9?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Salil\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/dealing-with-immutable-collections-in-java\\\/#webpage\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/dealing-with-immutable-collections-in-java\\\/\",\"name\":\"Dealing with immutable collections in Java | TO THE NEW Blog\",\"description\":\"Perhaps, you know it before or just skipped it after going through Java API. But I really found it very helpful at this point. Recently, I had a requirement in an open-source project, where users can iterate through the objects saved in collections (esp. List). But the problem was, we really didn\\u2019t want users\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/dealing-with-immutable-collections-in-java\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/salil\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/salil\\\/#author\"},\"datePublished\":\"2012-04-11T19:09:18+05:30\",\"dateModified\":\"2016-12-19T15:30:08+05:30\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\",\"name\":\"TO THE NEW Blog\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Dealing with immutable collections in Java | TO THE NEW Blog","description":"Perhaps, you know it before or just skipped it after going through Java API. But I really found it very helpful at this point. Recently, I had a requirement in an open-source project, where users can iterate through the objects saved in collections (esp. List). But the problem was, we really didn\u2019t want users","canonical_url":"https:\/\/2thenew.xyz\/blog\/dealing-with-immutable-collections-in-java\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/2thenew.xyz\/blog\/dealing-with-immutable-collections-in-java\/#article","name":"Dealing with immutable collections in Java | TO THE NEW Blog","headline":"Dealing with immutable collections in Java","author":{"@id":"https:\/\/2thenew.xyz\/blog\/author\/salil\/#author"},"publisher":{"@id":"https:\/\/2thenew.xyz\/blog\/#organization"},"datePublished":"2012-04-11T19:09:18+05:30","dateModified":"2016-12-19T15:30:08+05:30","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/2thenew.xyz\/blog\/dealing-with-immutable-collections-in-java\/#webpage"},"isPartOf":{"@id":"https:\/\/2thenew.xyz\/blog\/dealing-with-immutable-collections-in-java\/#webpage"},"articleSection":"Grails, collections, Groovy, Java"},{"@type":"BreadcrumbList","@id":"https:\/\/2thenew.xyz\/blog\/dealing-with-immutable-collections-in-java\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog#listItem","position":1,"name":"Home","item":"https:\/\/2thenew.xyz\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog\/category\/grails\/#listItem","name":"Grails"}},{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog\/category\/grails\/#listItem","position":2,"name":"Grails","item":"https:\/\/2thenew.xyz\/blog\/category\/grails\/","nextItem":{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog\/dealing-with-immutable-collections-in-java\/#listItem","name":"Dealing with immutable collections in Java"},"previousItem":{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog\/dealing-with-immutable-collections-in-java\/#listItem","position":3,"name":"Dealing with immutable collections in Java","previousItem":{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog\/category\/grails\/#listItem","name":"Grails"}}]},{"@type":"Organization","@id":"https:\/\/2thenew.xyz\/blog\/#organization","name":"TO THE NEW Blog","url":"https:\/\/2thenew.xyz\/blog\/"},{"@type":"Person","@id":"https:\/\/2thenew.xyz\/blog\/author\/salil\/#author","url":"https:\/\/2thenew.xyz\/blog\/author\/salil\/","name":"Salil","image":{"@type":"ImageObject","@id":"https:\/\/2thenew.xyz\/blog\/dealing-with-immutable-collections-in-java\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/97e216cba95b296d5909ad7b93d1c338d0fdad7b95410c9412b126a90f4735d9?s=96&d=mm&r=g","width":96,"height":96,"caption":"Salil"}},{"@type":"WebPage","@id":"https:\/\/2thenew.xyz\/blog\/dealing-with-immutable-collections-in-java\/#webpage","url":"https:\/\/2thenew.xyz\/blog\/dealing-with-immutable-collections-in-java\/","name":"Dealing with immutable collections in Java | TO THE NEW Blog","description":"Perhaps, you know it before or just skipped it after going through Java API. But I really found it very helpful at this point. Recently, I had a requirement in an open-source project, where users can iterate through the objects saved in collections (esp. List). But the problem was, we really didn\u2019t want users","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/2thenew.xyz\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/2thenew.xyz\/blog\/dealing-with-immutable-collections-in-java\/#breadcrumblist"},"author":{"@id":"https:\/\/2thenew.xyz\/blog\/author\/salil\/#author"},"creator":{"@id":"https:\/\/2thenew.xyz\/blog\/author\/salil\/#author"},"datePublished":"2012-04-11T19:09:18+05:30","dateModified":"2016-12-19T15:30:08+05:30"},{"@type":"WebSite","@id":"https:\/\/2thenew.xyz\/blog\/#website","url":"https:\/\/2thenew.xyz\/blog\/","name":"TO THE NEW Blog","inLanguage":"en-US","publisher":{"@id":"https:\/\/2thenew.xyz\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"TO THE NEW BLOG","og:type":"blog","og:title":"Dealing with immutable collections in Java | TO THE NEW Blog","og:description":"Perhaps, you know it before or just skipped it after going through Java API. But I really found it very helpful at this point. Recently, I had a requirement in an open-source project, where users can iterate through the objects saved in collections (esp. List). But the problem was, we really didn\u2019t want users","og:url":"https:\/\/2thenew.xyz\/blog\/dealing-with-immutable-collections-in-java\/","og:image":"https:\/\/2thenew.xyz\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","og:image:secure_url":"https:\/\/2thenew.xyz\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","twitter:card":"summary","twitter:site":"@tothenew","twitter:title":"Dealing with immutable collections in Java | TO THE NEW Blog","twitter:description":"Perhaps, you know it before or just skipped it after going through Java API. But I really found it very helpful at this point. Recently, I had a requirement in an open-source project, where users can iterate through the objects saved in collections (esp. List). But the problem was, we really didn\u2019t want users","twitter:image":"https:\/\/2thenew.xyz\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png"},"aioseo_meta_data":{"post_id":"5276","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"limit_modified_date":false,"created":"2021-04-29 16:39:53","updated":"2024-02-29 08:43:12","focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/2thenew.xyz\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/2thenew.xyz\/blog\/category\/grails\/\" title=\"Grails\">Grails<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tDealing with immutable collections in Java\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/2thenew.xyz\/blog"},{"label":"Grails","link":"https:\/\/2thenew.xyz\/blog\/category\/grails\/"},{"label":"Dealing with immutable collections in Java","link":"https:\/\/2thenew.xyz\/blog\/dealing-with-immutable-collections-in-java\/"}],"_links":{"self":[{"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/posts\/5276","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/comments?post=5276"}],"version-history":[{"count":0,"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/posts\/5276\/revisions"}],"wp:attachment":[{"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/media?parent=5276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/categories?post=5276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/tags?post=5276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}