Questions tagged [apache-commons-collection]

Commons-Collections builds upon the Java collection classes by providing new interfaces, implementations and utilities.

Features listed from home page

  • Bag interface for collections that have a number of copies of each object
  • Buffer interface for collections that have a well defined removal order, like FIFOs
  • BidiMap interface for maps that can be looked up from value to key as well and key to value
  • MapIterator interface to provide simple and quick iteration over maps
  • Type checking decorators to ensure that only instances of a certain type can be added
  • Transforming decorators that alter each object as it is added to the collection
  • Composite collections that make multiple collections look like one
  • Ordered maps and sets that retain the order elements are added in, including an LRU based map
  • Identity map that compares objects based on their identity (==) instead of the equals method
  • Reference map that allows keys and/or values to be garbage collected under close control
  • Many comparator implementations
  • Many iterator implementations
  • Adapter classes from array and enumerations to collections
  • Utilities to test or create typical set-theory properties of collections such as union, intersection, and closure
74 questions
347
votes
11 answers

How to convert a Collection to List?

I am using TreeBidiMap from the Apache Collections library. I want to sort this on the values which are doubles. My method is to retrieve a Collection of the values using: Collection coll = themap.values(); Which naturally works fine. Main…
Ankur
  • 50,282
  • 110
  • 242
  • 312
33
votes
1 answer

Java Commons Collections removeAll

CollectionUtils::removeAll() Commons Collections 3.2.1 I must be going crazy, becuase it seems like this method is doing the inverse of what the docs state: Removes the elements in remove from collection. That is, this method returns a collection…
markdsievers
  • 7,151
  • 11
  • 51
  • 83
16
votes
2 answers

Is there a viable generic alternative to apache.commons.collections.CollectionUtils?

Is there a viable generic version of org.apache.commons.collections.CollectionUtils? If not, why not? It seems like an obvious need. Or has the Java community just given up on functional coding until closures are added to Java 17?
kevin cline
  • 2,608
  • 2
  • 25
  • 38
13
votes
2 answers

null-safe Collection contains method

What's the best way to do null-safe contains on a Java collection? in other words - if (collection != null && collection.contains(x)) ? I was hoping Apache commons-collections had something like CollectionUtils.contains(collection, x) that…
wrschneider
  • 17,913
  • 16
  • 96
  • 176
12
votes
3 answers

CollectionUtils in java using predicate

I have a List and I want to return the first value that it finds true which matches a predicate. I found that I can use CollectionUtils.find(collection,predicate) (Apache commons). Assuming that the Object contains a integer variable called…
Rory Lester
  • 2,858
  • 11
  • 49
  • 66
11
votes
0 answers

common-collections 4.1 is causing compilation issue when comparing BeanPredicate with EqualPredicate

After upgrading commons-collection from 3.2.2 to 4.1, I am having issues with comparing collections for BeanPredicate and EqualsPredicate. BeanPredicate is from commons-beanutils jar and EqualsPredicate is from commons-collection.jar. BeanPredicate…
Pradeep
  • 1,947
  • 3
  • 22
  • 45
10
votes
2 answers

How to initialize a public static final read-only LinkedMap (bidirectionnal map)

I would like to create a public static final LinkedMap myMap; Somewhere I found something similar for Maps: public class Test { private static final Map MY_MAP = createMap(); private static Map
Antje Janosch
  • 1,154
  • 5
  • 19
  • 37
8
votes
2 answers

Convert a list of Person object into a separated String by getName() property of Person object

Is there a XXXUtils where I can do String s = XXXUtils.join(aList, "name", ","); where "name" is a JavaBeans property from the object in the aList. I found only StringUtils having join method, but it only transforms a List into a separated…
6
votes
1 answer

Break MultiKey returned by MapIterator into the individual keys

I am using Apache Commons Collections to create a MultiKeyMap that will store two keys with one corresponding value and then using MapIterator to walk through the map. The problem I've got is I need to break the keys returned by the MapIterator back…
Paul H
  • 2,104
  • 7
  • 39
  • 53
5
votes
3 answers

How do I learn to use Java commons-collections?

Weird title, I know, let me explain. I am a developer most familiar with C# and Javascript. I am completely sunk into those semi-functional worlds to the point that most of my code is about mapping/reducing/filtering collections. In C# that means I…
George Mauer
  • 117,483
  • 131
  • 382
  • 612
5
votes
3 answers

How to iterate over MultiKeyMap?

I'm using the MultiKeyMap from the commons-collections which provide multikey-value pairs. I have 3 keys which are Strings. I have two problems which I don't see how to solve. How can I iterate over all multikey-value pairs? With a simple HashMap I…
machinery
  • 5,972
  • 12
  • 67
  • 118
5
votes
1 answer

Is there a generic equivalent to ArrayIterator from Apache Commons Collections?

ArrayIterator is handy (although I don't need the reset functionality), but like the rest of the Commons Collections stuff, it doesn't use generics. I checked Google Collections, but I didn't see a close equivalent. Did I miss it? Is there another…
Hank Gay
  • 70,339
  • 36
  • 160
  • 222
5
votes
1 answer

Collection intermediate operations library in Java7

I Like the idea of intermediate operations of Java8, Where all operations will applied once when a terminal operation is reached. I am asking is there library that I can use with Java 7 that allow me to achieve such behaviour. Note: I am using…
Muhammad Hewedy
  • 29,102
  • 44
  • 127
  • 219
5
votes
3 answers

spot the difference between two lists

In java suppose I have 2 lists List list1 List list2 object1.getName(); returns a String object2.getName(); return a String is there any way to compare the names and get a difference of the two list those 2 objects are defined…
Junchen Liu
  • 5,435
  • 10
  • 51
  • 62
5
votes
2 answers

Creating a Commons Collections MultiValueMap with a custom value collection type

The 4.0 release of the Apache Commons Collections library has added generics support. I am having trouble converting my code to take advantage of it: I would like a MultiValueMap which takes a String as a key, and a collection of Strings as the…
Ryan Bennetts
  • 1,143
  • 2
  • 16
  • 29
1
2 3 4 5