Skip to content
Advertisement

Tag: collections

How to search in a List of Java object

I have a List of object and the list is very big. The object is Now I have to search for a specific value of an object in the list. Say if value3==’three’ I have to return those objects (My search is not always based on value3) The list is What is the efficient way of doing it? Thanks. Answer

Change priorityQueue to max priorityqueue

I have priority queue in Java of Integers: When I call pq.poll() I get the minimum element. Question: how to change the code to get the maximum element? Answer How about like this: The Collections.reverseOrder() provides a Comparator that would sort the elements in the PriorityQueue in a the oposite order to their natural order in this case.

Library method to partition a collection by a predicate

I have a collection of objects that I would like to partition into two collections, one of which passes a predicate and one of which fails a predicate. I was hoping there would be a Guava method to do this, but the closest they come is filter, which doesn’t give me the other collection. I would image the signature of

Why is this ArrayList throwing a ConcurrentModificationException when I try to remove an element?

I’m trying to remove a particular element from Arraylist, it throws an ConcurrentModificationException any comments, what am I doing wrong? Answer Only remove an element from the array while iterating by using Iterator.remove(). The line for(String st: ar) { is a bit misleading. You’re actually creating an iterator behind the scenes which is being used for this iteration. If you

difference between natural ordering and total ordering

I happen to come across many statements like comparable is used when natural ordering is required while sorting an array or collection and comparator for total ordering. The version you may have heard could be same or different with the same meaning but ultimately its one of the distinguishing factors between the two(comparator and comparable interfaces). But, I couldn’t find

Why is there no SortedList in Java?

In Java there are the SortedSet and SortedMap interfaces. Both belong to the Java Collections framework and provide a sorted way to access the elements. However, in my understanding there is no SortedList in Java. You can use java.util.Collections.sort() to sort a list. Any idea why it is designed like that? Answer List iterators guarantee first and foremost that you

Java : list that contains unique elements in order

Is there a list type in java that stores objects in ascending order and not adds if this object is previously added. I know java maps can do that but I wonder if there is a list type that does what I want. Otherwise I have to override contains, equalsTo and add methods,right? Answer So you need a list containing

How can I cascade delete a collection which is part of a JPA entity?

In one of my jobs I have the following code: This always fails to delete the entity with the following error: The DELETE statement conflicted with the REFERENCE constraint “FK966F0D9A66DB1E54”. The conflict occurred in database “TFADB”, table “dbo.MonthlyReport_categories”, column ‘MonthlyReport_id’. How can I specify the mapping so the elements from the categories collection get deleted when the report is deleted?

How to convert comma-separated String to List?

Is there any built-in method in Java which allows us to convert comma separated String to some container (e.g array, List or Vector)? Or do I need to write custom code for that? Answer Convert comma separated String to List The above code splits the string on a delimiter defined as: zero or more whitespace, a literal comma, zero or

Advertisement