Returns an java.util.LinkedHashSet of the android.database.Cursor and closes it. - Android Database

Android examples for Database:Cursor

Description

Returns an java.util.LinkedHashSet of the android.database.Cursor and closes it.

Demo Code


import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.LinkedList;

public class Main{
    /**//from   ww w .  j  a  va  2s .  c  om
     * Returns an {@link java.util.LinkedHashSet} of the {@link android.database.Cursor} and closes
     * it.
     */
    public static <T> LinkedHashSet<T> consumeToLinkedHashSet(
            IterableCursor<T> cursor) {
        return consumeToCollection(cursor,
                new LinkedHashSet<T>(cursor.getCount()));
    }
    /**
     * Add each item of this {@link android.database.Cursor} to the {@code collection} parameter.
     * Closes the cursor once completed.
     *
     * @return the same collection as the parameter.
     * @see #consumeToArrayList(com.venmo.cursor.IterableCursor)
     * @see #consumeToLinkedList(com.venmo.cursor.IterableCursor)
     * @see #consumeToLinkedHashSet(com.venmo.cursor.IterableCursor)
     */
    public static <T, C extends Collection<T>> C consumeToCollection(
            IterableCursor<T> cursor, C collection) {
        try {
            for (T t : cursor) {
                collection.add(t);
            }
        } finally {
            cursor.close();
        }
        return collection;
    }
}

Related Tutorials