Null safe creation of a LinkedHashSet out of a given collection. - Java java.util

Java examples for java.util:LinkedHashSet

Description

Null safe creation of a LinkedHashSet out of a given collection.

Demo Code

/*******************************************************************************
 * Copyright (c) 2010 BSI Business Systems Integration AG.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from   w  w w .j  av a2  s .c o  m*/
 *     BSI Business Systems Integration AG - initial API and implementation
 ******************************************************************************/
//package com.book2s;

import java.util.Collection;

import java.util.LinkedHashSet;

public class Main {
    public static void main(String[] argv) {
        Collection c = java.util.Arrays.asList("asdf", "book2s.com");
        System.out.println(orderedHashSet(c));
    }

    /**
     * Null safe creation of a {@link LinkedHashSet} out of a given collection. The returned {@link LinkedHashSet} is
     * modifiable and
     * never null.
     *
     * @param c
     * @return an {@link LinkedHashSet} containing the given collection's elements. Never null.
     */
    public static <T> LinkedHashSet<T> orderedHashSet(
            Collection<? extends T> c) {
        if (c != null) {
            return new LinkedHashSet<T>(c);
        }
        return new LinkedHashSet<T>(0);
    }
}

Related Tutorials