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
Tag: collections
How can I use merge method in Java to append elements to the list value in map<Character,List>?
I am trying to understand the merge method. I have a map of <Character, List<Integer>>. I want to add the indices to the list of the appropriate key. I want to use the merge method to do so. I tried the following method. I am getting UnsupportedOperationException. What is the best way to do so using the merge method? Answer
Why clear() on one ArrayList is clearing elements in another ArrayList?
My desired output should be: [[1], [1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 5]] By creating new ArrayList object every time I am getting the desired output as [[1], [1, 2], [1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 5]] But, not getting same result when trying to use clear() (to
I made my own Collection based on TreeSet but I have troubles with existing test: it doesn’t see elements
Here is my collection. Here I try to make my own implemetation with special Comparator that sorts Integer elements by its absolute values. (and other methods) Main class. Everything seems to work correct except toString() method. When I overwrite this method without lambdas it works. But! This method is in tests and I mustn’t change it. I just copied it
How to collect data to List from Map using java Stream API?
I have map Map<Nominal, Integer> with objects and their counts: And I need to get such a List<Nominal> from it: How can I do this using the Stream API? Answer We can use Collections::nCopies to achieve the desired result: Ideone demo Remark In the demo, I changed the key-type of the Map from Nominal to Object since the definition of
iterate the List and set the value with data available in another list [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 1 year ago. Improve this question I have the below two List objects, i want to iterate the addrMappingList and set the Person to the field by getting the person object
Map.ofEntries gives Null Pointer Exception on checking NULL key using containsKey()
I was fetching key from a constant map earlier using HashMap. On passing a NULL key at containsKey(), I used to get FALSE. To make the code look fancy, I tried java-8 over it. So, instead of HashMap, I started using Map.ofEntries to build my map Surprisingly, I got Null Pointer Exception when a Null key was passed to containsKey()
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
Updating value of particular keys in MultiKeyMap in Java
I was trying to update the values of set of particular keys in MultiKeyMap in Java but not able to get how to do that. Suppose I want to update (key1, key2, value1) same set of key with different value (key1, key2, value2). If I simply perform put in MultiKeyMap then it create new row and doesn’t update the value.
Iterating over and reducing values of a Java map via Stream API
Java 8 here. I have an array of Strings: Then I have a map where the keys are Strings (specifically, the same literal values as some of the animals in the array above), and the values are a count (representing the number of those animals): I am trying to figure out how to use the Stream API to iterate over