/** * Adds the given object to the set but saves memory space by allocating only the required amount for small sets. The idea is to use the specialized empty * set and singleton set implementations (which are immutable) for the sets of size 0 and 1. If the set is empty a new singleton set is created, if set has * one element we create a new set with two elements, otherwise we simply add the element to the given set.This technique is most useful if the expected set * size is 0 or 1. * * @param o * @param set * @return merge of set */ public static <T> Set<T> add(final T o, final Set<T> set) { switch (set.size()) { case 0: return Collections.singleton(o); case 1: { final T existing = set.iterator().next(); if (existing.equals(o)) return set; return binary(existing, o); } default: { set.add(o); return set; } } }
/** * Adds the given object to the set but saves memory space by allocating only the required amount for small sets. The idea is to use the specialized empty * set and singleton set implementations (which are immutable) for the sets of size 0 and 1. If the set is empty a new singleton set is created, if set has * one element we create a new set with two elements, otherwise we simply add the element to the given set.This technique is most useful if the expected set * size is 0 or 1. * * @param o * @param set * @return merge of set */ public static <T> Set<T> add(final T o, final Set<T> set) { switch (set.size()) { case 0: return Collections.singleton(o); case 1: { final T existing = set.iterator().next(); if (existing.equals(o)) return set; return binary(existing, o); } default: { set.add(o); return set; } } }