My usecase is like in which I need to maintain a collection of unique items. I may need to frequently add or remove items from the collection at an index(which I have as member of the item currently, but I am open to modification), and while doing that I need to update index of items. I am not able to
Tag: collections
Spring SPEL collection projection
A simple question on SPEL collection selection. Look at section 10.5.17 Collection Selection on this page https://docs.spring.io/spring/docs/4.3.10.RELEASE/spring-framework-reference/html/expressions.html What i need is the selection ‘Serbian’ to come from outside and not be a fixed hard coded String. Just for arguments sake consider that, we could get it as “selectedNationality” from the same society class from the same page in the link.
Java 8 Remove 1 List from Other
I have two list of different Objects. I want to remove the list of School objects from List of World objects based on schoolName and location. I cannot use equals and hashCode methods on those two fields as it is creating some other problem. Please help me how it can be done using streams. Answer You can use filter:
Is it possible that TreeSet equals HashSet but not HashSet equals TreeSet
I had a interview today and the person taking my interview puzzled me with his statement asking if it possible that TreeSet equals HashSet but not HashSet equals TreeSet. I said “no” but according to him the answer is “yes”. How is it even possible? Answer Your interviewer is right, they do not hold equivalence relation for some specific cases.
Calculate the percentage of value using Collection framework
I have List of TrainingRequest where each and every element has List of Feedback. I need to get all given result of Q1,Q2 and calculate percentage of each value. To flat all the feedback To calculate each value of Q1 and Q2, I’m grouping it and getting the count. I need to get the percentage of each Q1, Q2 value
Sort List of Strings by Characters In Java
I am trying to write a program to order a list of strings by most least frequent characters within the list. For example, if the list was [apple, orange, banana] the letter frequency within the list would be a – 5, n – 3, p – 2, e – 2, l- 1, o – 1, r – 1, g –
JUnit: assert collection contains an element with a certain property
I want to assert (using JUnit and not any other dependencies like hamcrest) if a collection contains an element with a certain property value). I found this question How do I assert an Iterable contains elements with a certain property? using harmcrest. One option is to override the equals method to check only the property that I want to compare,
Removing elements while iterating. removeIf result in ConcurrentModificationException
I’m trying to remove elements from a set (someObjectSet) while looping through it. As I googled, using removeIf should avoid ConcurrentModificationException in this case. However this doesn’t work for me. Did google lie to me (or I misundertood it), or I’m not using removeIf correctly? The reason I want to do removeif inside the loop is that, in each loop,
Java Error: java.lang.IllegalArgumentException: Comparison method violates its general contract
I am working on a old application which was originally written in Java 6 and which was upgraded to Java 7 a couple of years ago. In this application i am using a Collection.Sort to sort a list with a custom compare method by implementing Comparator interface. Type of objects in the list are CompanySchedule which have 3 properties companyName,
Creating a map from nested lists
Suppose there are 3 classes: Suppose there is a List of Level1 objects called initial state I want to create a map from initialList where: I am able to achieve this using for loops, but I want to achieve this in a more elegant way using Java 8 features (streams and the functions). I tried using Collectors.toMap() also tried grouping