Java Collection Combine combineAndRemoveDuplicates(Collection collection1, Collection collection2)

Here you can find the source of combineAndRemoveDuplicates(Collection collection1, Collection collection2)

Description

combine And Remove Duplicates

License

Open Source License

Declaration

public static <T> Collection<T> combineAndRemoveDuplicates(Collection<T> collection1,
            Collection<T> collection2) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;
import java.util.Collection;

public class Main {
    public static <T> Collection<T> combineAndRemoveDuplicates(Collection<T> collection1,
            Collection<T> collection2) {
        Collection<T> merged;

        try {//from  w  ww .  j av  a 2s. c o  m
            merged = collection1.getClass().newInstance();
        } catch (Exception ex) {
            merged = new ArrayList<T>();
        }

        merged.addAll(collection1);
        merged.addAll(collection2);

        Collection<T> output;

        try {
            output = collection1.getClass().newInstance();
        } catch (Exception ex) {
            output = new ArrayList<T>();
        }

        for (T val : merged) {
            if (!output.contains(val)) {
                output.add(val);
            }
        }

        return output;
    }
}

Related

  1. combine(Collection strings)
  2. combine(final Collection[] c)
  3. combine(String separator, Collection parts)
  4. combine(String separator, Collection stringCollection)
  5. combine(String separator, Collection objs)
  6. combineCollections(Collection sessionCollection, Collection reqCollection)
  7. CombineCollections(final Collection... collections)
  8. combineNames(Collection items)
  9. combineProblemMessages(final Collection problems)

  10. HOME | Copyright © www.java2s.com 2016