List of usage examples for java.util Set add
boolean add(E e);
From source file:Main.java
/** * Returns true if every element in a collection are unique by {@link Object#equals(Object)}. *///from ww w .j av a 2 s . co m public static boolean elementsAreUnique(Collection<?> items) { final Set<Object> testSet = new HashSet<>(); for (Object item : items) { final boolean itemAlreadyExists = !testSet.add(item); // see Set documentation if (itemAlreadyExists) { return false; } } return true; }
From source file:Main.java
/** * Get the elements who's tags match those passed in. * @param nodeList - the node list in which to look for elements * @param tags - the tags to match (empty returns all elements). * @return The elements that matched./*from ww w. j av a2s . c o m*/ */ public static List<Element> getElements(NodeList nodeList, String... tags) { int nNodes = nodeList.getLength(); List<Element> elements = new ArrayList<Element>(nNodes); Set<String> tagSet = new HashSet<String>(tags.length); for (String tag : tags) { tagSet.add(tag); } for (int i = 0; i < nNodes; ++i) { Node node = nodeList.item(i); if (node instanceof Element) { Element element = (Element) node; String tagName = element.getTagName(); if (tagSet.isEmpty() || tagSet.contains(tagName)) { elements.add(element); } } } return elements; }
From source file:Main.java
public static Set<Integer> getNumsFromStr(String text) { text = null == text ? "" : text; String[] ary = text.replaceAll("[^\\d]", " ").split("\\s+"); Set<Integer> set = new TreeSet<Integer>(); for (String num : ary) { if (!num.trim().equals("")) { set.add(Integer.valueOf(num.trim())); }//from w ww . j a v a 2 s .c o m } return set; }
From source file:Main.java
public static <E> Collection<E> unique(Collection<? extends E> src, Collection<E> dest) { Set<E> set = new HashSet<>(); for (E element : src) { if (!set.contains(element)) { set.add(element); dest.add(element);//from w ww . j a va2s . c o m } } set.clear(); return dest; }
From source file:Main.java
public static Set<Node> populateNodes(Node node, Set<Node> nodes) { if (node != null) { if (node.getNodeType() == Node.ELEMENT_NODE) { nodes.add(node); }// w ww . j av a 2 s .c o m populateNodes(node.getNextSibling(), nodes); } return nodes; }
From source file:Main.java
private static Set<String> getBlueSet(Set<Integer> numSet) { Set<String> blueSet = new HashSet<String>(); for (Integer i : numSet) { if (i < 10 && i > 0) { blueSet.add("" + i); }//from ww w . j a va 2 s .c o m if (i < 7 && i > 0) { blueSet.add("1" + i); } } return blueSet; }
From source file:Main.java
public static Set<Class<?>> findSuperTypes(Class<?> targetClass) { Set<Class<?>> classes = new HashSet<Class<?>>(); Class<?> clazz = targetClass; while (clazz != null) { classes.add(clazz); addInterfaces(classes, clazz.getInterfaces()); clazz = clazz.getSuperclass();/*from w w w . j av a2 s . c o m*/ } return classes; }
From source file:Main.java
/** Find all values of a named field in a nested Map containing fields, Maps, and Collections of Maps (Lists, etc) */ public static void findAllFieldsNestedMap(String key, Map theMap, Set<Object> valueSet) { Object localValue = theMap.get(key); if (localValue != null) valueSet.add(localValue); for (Object value : theMap.values()) { if (value instanceof Map) { findAllFieldsNestedMap(key, (Map) value, valueSet); } else if (value instanceof Collection) { // only look in Collections of Maps for (Object colValue : (Collection) value) { if (colValue instanceof Map) findAllFieldsNestedMap(key, (Map) colValue, valueSet); }/*from w w w.j ava 2 s .com*/ } } }
From source file:Main.java
/** * Converts the specified {@link JSONArray JSON array} to a * {@link List list}.// w w w . ja v a 2 s . c om * * @param <T> the type of elements maintained by the specified json array * @param jsonArray the specified json array * @return an {@link ArrayList array list} */ @SuppressWarnings("unchecked") public static <T> Set<T> jsonArrayToSet(final JSONArray jsonArray) { if (null == jsonArray) { return Collections.emptySet(); } final Set<T> ret = new HashSet<T>(); for (int i = 0; i < jsonArray.length(); i++) { ret.add((T) jsonArray.opt(i)); } return ret; }
From source file:edu.uci.ics.hyracks.algebricks.rewriter.util.PhysicalOptimizationsUtil.java
private static void computeFDsAndEqClassesWithVisitorRec(AbstractLogicalOperator op, IOptimizationContext ctx, FDsAndEquivClassesVisitor visitor, Set<ILogicalOperator> visitSet) throws AlgebricksException { visitSet.add(op); for (Mutable<ILogicalOperator> i : op.getInputs()) { computeFDsAndEqClassesWithVisitorRec((AbstractLogicalOperator) i.getValue(), ctx, visitor, visitSet); }//from ww w . j a v a2s. c o m if (op.hasNestedPlans()) { for (ILogicalPlan p : ((AbstractOperatorWithNestedPlans) op).getNestedPlans()) { for (Mutable<ILogicalOperator> r : p.getRoots()) { AbstractLogicalOperator rootOp = (AbstractLogicalOperator) r.getValue(); computeFDsAndEqClassesWithVisitorRec(rootOp, ctx, visitor, visitSet); } } } if (op.getOperatorTag() == LogicalOperatorTag.NESTEDTUPLESOURCE) { NestedTupleSourceOperator nts = (NestedTupleSourceOperator) op; ILogicalOperator source = nts.getDataSourceReference().getValue().getInputs().get(0).getValue(); if (!visitSet.contains(source)) { computeFDsAndEqClassesWithVisitorRec((AbstractLogicalOperator) source, ctx, visitor, visitSet); } } op.accept(visitor, ctx); if (AlgebricksConfig.DEBUG) { AlgebricksConfig.ALGEBRICKS_LOGGER .fine("--> op. type = " + op.getOperatorTag() + "\n" + " equiv. classes = " + ctx.getEquivalenceClassMap(op) + "\n" + " FDs = " + ctx.getFDList(op) + "\n"); } }