{"id":2790,"date":"2025-12-24T10:00:22","date_gmt":"2025-12-24T15:00:22","guid":{"rendered":"https:\/\/www.mymiller.name\/wordpress\/?p=2790"},"modified":"2025-12-24T10:00:22","modified_gmt":"2025-12-24T15:00:22","slug":"list-vs-array","status":"publish","type":"post","link":"https:\/\/www.mymiller.name\/wordpress\/lists\/list-vs-array\/","title":{"rendered":"List v&#8217;s Array"},"content":{"rendered":"\n<p>Creating a method or function and you need to return a number of objects, which do you use a List or an Array?<\/p>\n\n\n\n<p>Both have their uses, and maybe your project&#8217;s design would add weight to which you decide to do.  Setting that factor aside let&#8217;s look at our options on what we can do.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Array<\/h2>\n\n\n\n<p>It gives you access to the member variable length.  A simple int that tells you the number of items in the array. This is great if your array is not null, you can directly access the length of the array to determine it&#8217;s size<\/p>\n\n\n\n<p>Secondly it gives you access to the clone() method to create a copy of the array.  This is a shallow copy of the array.  If you are looking at implementing your own sort, being able to easily create a shallow copy of the array could be extremely beneficial.  <\/p>\n\n\n\n<p>Then an Array is also lighter weight.  Fewer objects for garbage collection when it goes away.  This is extremely important when you are working on real-time projects or on hardware with limited processing or memory.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">List<\/h2>\n\n\n\n<p>I have to admin I&#8217;m extremely partial to returning lists, even if the list is empty.  Why?<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>mylist.stream()......<\/code><\/pre>\n\n\n\n<p>Java 8 was a game-changer with the addition of <a href=\"https:\/\/docs.oracle.com\/en\/java\/javase\/14\/docs\/api\/java.base\/java\/util\/stream\/package-summary.html\" target=\"_blank\" rel=\"noreferrer noopener\">streams<\/a>.  With everything you can do with a stream, having your method\/function return a stream or <a href=\"https:\/\/www.mymiller.name\/wordpress\/programming\/java\/not-returning-null\/\" target=\"_blank\" rel=\"noreferrer noopener\">empty stream<\/a> at the least is extremely helpful. It really does simplify when you can do something like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>this.getOpenCustomer().stream().filter(customer -> customer.getAge() > 18).sorted(Comparator.comparing(Customer::getLastName)).collect(Collectors.toList());<\/code><\/pre>\n\n\n\n<p>Streams do offer great flexibility.  However, they are not the only reason to return a List. With a list, it&#8217;s easy to intersections, unions, and unique entries with another list.  Combining two lists together is a simple method call.<\/p>\n\n\n\n<p>Overall a List provides the greater flexibility, than an array.  If you need an array, then you can always:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>this.getOpenCustomer().stream().toArray(Customer&#91;]::new);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Discussion on which is the better choice of type to return from a method or function for multiple items. Both have their values, but flexibility gives you the most options. Brief discussion on the advantages.<\/p>\n","protected":false},"author":1,"featured_media":2793,"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":[],"class_list":["post-2790","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-lists","tag-java-2"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2020\/04\/ship-56569_640.jpg?fit=640%2C442&ssl=1","jetpack-related-posts":[{"id":1936,"url":"https:\/\/www.mymiller.name\/wordpress\/java\/advancedstring-java-lang-string-steroids\/","url_meta":{"origin":2790,"position":0},"title":"AdvancedString &#8211; java.lang.String on steroids!","author":"Jeffery Miller","date":"April 20, 2026","format":false,"excerpt":"Every need an additional method on the String Class? \u00a0Well I have and it would have made life much easier. \u00a0Unfortunately you can't subclass String as it is Final. \u00a0So what are you to do? \u00a0Well you wrap the String class. \u00a0I have created AdvancedString, which contains additional functionality and\u2026","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.mymiller.name\/wordpress\/category\/java\/"},"img":{"alt_text":"AdvancedString","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2016\/09\/rope-1379561_640.jpg?fit=640%2C425&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2016\/09\/rope-1379561_640.jpg?fit=640%2C425&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2016\/09\/rope-1379561_640.jpg?fit=640%2C425&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":2459,"url":"https:\/\/www.mymiller.name\/wordpress\/java_tips\/not-returning-null\/","url_meta":{"origin":2790,"position":1},"title":"Not Returning Null","author":"Jeffery Miller","date":"December 23, 2025","format":false,"excerpt":"We all have done it, in fact, I'm in the process of changing my APIs over to not do.\u00a0 Returning NULL for a return type.\u00a0 After doing some work with streams, I see that return NULL often adds additional complexity to the code.\u00a0 In this article, we are going to\u2026","rel":"","context":"In &quot;Java Tips&quot;","block_context":{"text":"Java Tips","link":"https:\/\/www.mymiller.name\/wordpress\/category\/java_tips\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2018\/11\/paper-3204064_640.jpg?fit=640%2C372&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2018\/11\/paper-3204064_640.jpg?fit=640%2C372&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2018\/11\/paper-3204064_640.jpg?fit=640%2C372&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":1788,"url":"https:\/\/www.mymiller.name\/wordpress\/challenge\/algorithm-max-diff\/","url_meta":{"origin":2790,"position":2},"title":"Algorithm: Max Diff between consecutive elements in an ordered array","author":"Jeffery Miller","date":"July 3, 2024","format":false,"excerpt":"So you have an array that is ordered from highest to lowest, and now you need to know what is the maximum difference between consecutive elements in the array. \u00a0So let's take a look at what we are first talking about. int[] array = {1,2,3,5,7,11,13,17,19} As you can see we\u2026","rel":"","context":"In &quot;Challenge&quot;","block_context":{"text":"Challenge","link":"https:\/\/www.mymiller.name\/wordpress\/category\/challenge\/"},"img":{"alt_text":"algorithm max diff array","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2016\/07\/silhouette-936724_640.jpg?fit=640%2C452&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2016\/07\/silhouette-936724_640.jpg?fit=640%2C452&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2016\/07\/silhouette-936724_640.jpg?fit=640%2C452&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":3421,"url":"https:\/\/www.mymiller.name\/wordpress\/java_extra\/convert-csv-to-json-and-json-to-csv-with-csvjsonconverter\/","url_meta":{"origin":2790,"position":3},"title":"Convert CSV to JSON and JSON to CSV with CSVJSONConverter","author":"Jeffery Miller","date":"December 24, 2025","format":false,"excerpt":"In today's data-driven world, it's common to work with different data formats like CSV (Comma-Separated Values) and JSON (JavaScript Object Notation). Converting data between these formats is a common task in data processing and integration workflows. In this article, we'll explore how to use the CSVJSONConverter class, a versatile utility\u2026","rel":"","context":"In &quot;Java Extras&quot;","block_context":{"text":"Java Extras","link":"https:\/\/www.mymiller.name\/wordpress\/category\/java_extra\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/07\/computer-g610baba23_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\/2023\/07\/computer-g610baba23_640.jpg?fit=640%2C360&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/07\/computer-g610baba23_640.jpg?fit=640%2C360&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":1932,"url":"https:\/\/www.mymiller.name\/wordpress\/java_http\/http-server-filters-jar\/","url_meta":{"origin":2790,"position":4},"title":"HTTP Server Filters in a Jar","author":"Jeffery Miller","date":"April 20, 2026","format":false,"excerpt":"We discussed the HTTP Server ContextHandler in a Jar\u00a0one of the methods was ContextHandlerInterface.getFilters() this method returns a list of Filters to apply to incoming HTTP Requests. \u00a0We subclass\u00a0com.sun.net.httpserver.Filter for these classes. These can be used to do processing on the request and add attributes to the HTTP Exchange. Filters\u2026","rel":"","context":"In &quot;Java HTTP&quot;","block_context":{"text":"Java HTTP","link":"https:\/\/www.mymiller.name\/wordpress\/category\/java_http\/"},"img":{"alt_text":"filters","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2016\/09\/http-875180_640.jpg?fit=640%2C452&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2016\/09\/http-875180_640.jpg?fit=640%2C452&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2016\/09\/http-875180_640.jpg?fit=640%2C452&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":2391,"url":"https:\/\/www.mymiller.name\/wordpress\/java_tips\/java-development-tips\/","url_meta":{"origin":2790,"position":5},"title":"Java Development Tips","author":"Jeffery Miller","date":"December 23, 2025","format":false,"excerpt":"Java-based servers or applications often have to deal with large amounts of data.\u00a0 Whether the data is from a database, or from a local file, processing this data in an efficient manner is a priority for maintainability. In this article, we will discuss the types of Java Data Objects and\u2026","rel":"","context":"In &quot;Java Tips&quot;","block_context":{"text":"Java Tips","link":"https:\/\/www.mymiller.name\/wordpress\/category\/java_tips\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2018\/10\/archive-1850170_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\/2018\/10\/archive-1850170_640.jpg?fit=640%2C426&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2018\/10\/archive-1850170_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\/2790","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=2790"}],"version-history":[{"count":4,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/2790\/revisions"}],"predecessor-version":[{"id":2795,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/2790\/revisions\/2795"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/media\/2793"}],"wp:attachment":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/media?parent=2790"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/categories?post=2790"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/tags?post=2790"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/series?post=2790"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}