add to Collection if Not Exists - Java java.util

Java examples for java.util:Collection Element

Description

add to Collection if Not Exists

Demo Code


//package com.book2s;

import java.util.Collection;

public class Main {
    public static void main(String[] argv) {
        Collection c = java.util.Arrays.asList("asdf", "book2s.com");
        Object obj = "book2s.com";
        addNotExists(c, obj);//from  w  ww  .j a  va  2  s  .  c  om
    }

    public static void addNotExists(Collection<Object> c, Object obj) {
        if (!c.contains(obj)) {
            c.add(obj);
        }
    }
}

Related Tutorials