{"id":1420,"date":"2011-09-09T09:01:06","date_gmt":"2011-09-09T16:01:06","guid":{"rendered":"http:\/\/juixe.com\/techknow\/?p=1420"},"modified":"2011-09-07T21:44:18","modified_gmt":"2011-09-08T04:44:18","slug":"remove-multiple-null-values-from-a-list-in-java","status":"publish","type":"post","link":"http:\/\/juixe.com\/techknow\/index.php\/2011\/09\/09\/remove-multiple-null-values-from-a-list-in-java\/","title":{"rendered":"Remove Multiple Null Values From A List in Java"},"content":{"rendered":"<p>I&#8217;ve had situations where I&#8217;ve needed a list of foreign keys (fks) that I get from a result set and from that list make additional queries.  Sometimes for whatever reason there are null values in the list and I have to remove them. You might had a similar problem where you needed to remove multiple occurring value from a lists in java.  There are a few ways you can approach this problem.  You you can remove each occurrence of a list element one at a time.  The example below removes any null element in the list one at a time.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nwhile(ids.contains(null)) {\r\n\tids.remove(null);\r\n}\r\n<\/pre>\n<p>There is another approach you can use to remove all instances of a given value from a list in one go.  Instead of the remove method you can use the removeAll as in the following code sample.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nids.removeAll(Collections.singleton(null));\r\n<\/pre>\n<p>Of course, instead of null values you can remove all instances of any given value from a list based on your business needs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve had situations where I&#8217;ve needed a list of foreign keys (fks) that I get from a result set and from that list make additional queries. Sometimes for whatever reason there are null values in the list and I have to remove them. You might had a similar problem where you needed to remove multiple [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[15,19],"tags":[547,545,802,546,548,549],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p902K-mU","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/1420"}],"collection":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/comments?post=1420"}],"version-history":[{"count":1,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/1420\/revisions"}],"predecessor-version":[{"id":1421,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/1420\/revisions\/1421"}],"wp:attachment":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/media?parent=1420"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/categories?post=1420"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/tags?post=1420"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}