Skip to content

Tag: collections

Why list.contains(null) throwing null pointer exception?

I have List<Long> countriesList which contains Long values which are country ids. Now I am iterating over some List<UserRequests> list by using streams. I tried to debug by evaluating individual statements. I have made sure countriesList have some data First part CollectionUtils.isNotEmpty(countriesList) is returning true. userRequest is also not null but userRequest.getCountryId() is null. And when I evaluated countriesList.contains(userRequest.getCountryId()), I

remove (and count) duplicates from a list

Is it possible to iterate between two iterators of a same list and remove an item within the nested iterator? Version 1 (does not work): } I get a java.util.ConcurrentModificationException, because I modify an element within the same iterator.. I can solve this issue by using another list removableItems and put those items in it: Version 2 (works): Is there

Create a list with only base class elements

In Java I have a class and subclass Now let’s say I have List<Bar> bars. How can I create from this a List of Foo (and I only want the a field to appear, not b) Answer What you want is impossible; let’s say you have your bars list which contains 5 Bar instances. It is not possible in java

Comparing the contents of two Treemaps

I have two TreeMaps that I want to compare. I currently have it written down like below but I feel like this could be written more efficiently. I tried looking in to comparators, but I don’t think that’s something I can use for my use-case. The maps are Treemaps because the key must be case-insensitive. Thanks in advance Answer If

Merge an List of Object and an Object to a new array in Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 10 months ago. Improve this question I have List<BigInteger> sample1 = Lists.newArrayList(BIGINTEGER1, BIGINTEGER2,BIGINTEGER3); I have to create new List sample2 with one more value BIGINTEGER4 along with sample1 list: How can

add element to limited size list

I have the following method which adds an element to a size limited ArrayList. If the size of the ArrayList exceeds, previous elements are removed (like FIFO = “first in first out”) (version 1): The “version 1”-method works. However, I wanted to improve this method by using subList (version 2): Both methods works. However, I want to know if “version

Combine two lists by ID

I have two lists of objects with contents that look like this: List A id = 1, name = “alex”, height = null, weight = 60 id = 2, name = “sara”, height = null, weight = 50 List B id …