Class CollectionUtilities

java.lang.Object
com.cedarsoftware.util.CollectionUtilities

public class CollectionUtilities extends Object
A utility class providing enhanced operations for working with Java collections.

CollectionUtilities simplifies tasks such as null-safe checks, retrieving collection sizes, creating immutable collections, and wrapping collections in checked, synchronized, or unmodifiable views. It includes functionality compatible with JDK 8, providing alternatives to methods introduced in later versions of Java, such as List.of(Object...) and Set.of(Object...).

Key Features

Usage Examples


 // Null-safe checks
 boolean isEmpty = CollectionUtilities.isEmpty(myCollection);
 boolean hasContent = CollectionUtilities.hasContent(myCollection);
 int size = CollectionUtilities.size(myCollection);

 // Immutable collections
 List<String> list = CollectionUtilities.listOf("A", "B", "C");
 Set<String> set = CollectionUtilities.setOf("X", "Y", "Z");

 // Collection wrappers
 Collection<?> unmodifiable = CollectionUtilities.getUnmodifiableCollection(myCollection);
 Collection<?> checked = CollectionUtilities.getCheckedCollection(myCollection, String.class);
 Collection<?> synchronizedCollection = CollectionUtilities.getSynchronizedCollection(myCollection);
 Collection<?> empty = CollectionUtilities.getEmptyCollection(myCollection);
 

Design Notes

  • This class is designed as a static utility class and should not be instantiated.
  • It uses unmodifiable empty collections as constants to optimize memory usage and prevent unnecessary object creation.
  • The collection wrappers apply type-specific operations based on the runtime type of the provided collection.
Author:
John DeRegnaucourt ([email protected])
Copyright (c) Cedar Software LLC

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

License

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
See Also: