Java Utililty Methods Collection Element Get

List of utility methods to do Collection Element Get

Description

The list of methods to do Collection Element Get are organized into topic(s).

Method

StringgetCollectionClosingSymbol(Collection col)
get Collection Closing Symbol
if (col instanceof List) {
    return "]";
if (col instanceof Set) {
    return "}";
return ")";
StringgetCollectionPreview(final Collection collection, int previewSize)
Generates a string that enumerates just the first elements of a collection (a preview).
final Iterator<?> it = collection.iterator();
final StringBuilder buffer = new StringBuilder();
if (it.hasNext()) {
    buffer.append('[').append(it.next().toString());
    for (; previewSize > 0 && it.hasNext(); --previewSize)
        buffer.append(LIST_SEPARATOR).append(' ').append(it.next().toString());
    if (it.hasNext())
        buffer.append(" ... ");
...
intgetCollectionSize(Collection collection)
return collection size
return collection != null ? collection.size() : 0;
intgetCollectionSize(Collection collection)
Returns size of the incoming list
if (collection == null)
    return 0;
else
    return collection.size();
StringgetCommaDelimitedString(Collection elements)
Transforms a collection of Integers into a comma delimited String.
final StringBuilder builder = new StringBuilder();
if (elements != null && !elements.isEmpty()) {
    for (Object element : elements) {
        builder.append(element.toString()).append(DELIMITER);
    return builder.substring(0, builder.length() - DELIMITER.length());
return builder.toString();
...
StringgetCommaSeparatedString(Collection collection)
get Comma Separated String
StringBuffer buffer = new StringBuffer();
Iterator<? extends Object> iterator = collection.iterator();
while (iterator.hasNext()) {
    Object obj = iterator.next();
    buffer.append(obj.toString());
    if (iterator.hasNext()) {
        buffer.append(", ");
return buffer.toString();
StringgetCommaSeparatedString(Collection values)
get Comma Separated String
if (values == null || values.size() == 0) {
    return null;
StringBuilder builder = new StringBuilder();
for (String value : values) {
    builder.append(value);
    builder.append(COMMA);
return builder.substring(0, builder.length() - 1);
StringgetCommaSeparatedStringFromCollection(Collection collections)
get Comma Separated String From Collection
StringBuilder result = new StringBuilder();
if (collections != null) {
    Object[] array = collections.toArray();
    for (int i = 0; i < array.length; i++) {
        result.append(array[i]);
        if (i < array.length - 1) {
            result.append(',');
return result.toString();
CollectiongetCommon(Collection c1, Collection c2)
get Common objects between two collections
Iterator iterator = c1.iterator();
while (iterator.hasNext()) {
    Object o1 = iterator.next();
    if (!c2.contains(o1))
        iterator.remove();
return c1;
ListgetCommonElements(Collection from, Collection to)
get Common Elements
List<E> res = new ArrayList<E>();
for (E f : from) {
    for (E t : to) {
        if (f.equals(t)) {
            res.add(f);
return res;