Example usage for java.util Set toArray

List of usage examples for java.util Set toArray

Introduction

In this page you can find the example usage for java.util Set toArray.

Prototype

Object[] toArray();

Source Link

Document

Returns an array containing all of the elements in this set.

Usage

From source file:com.gson.oauth.Pay.java

/**
 * ??//from   www.  ja  v a2  s  .c om
 * @param params
 * @param encode
 * @return
 * @throws UnsupportedEncodingException 
 */
public static String createSign(Map<String, String> params, boolean encode)
        throws UnsupportedEncodingException {
    Set<String> keysSet = params.keySet();
    Object[] keys = keysSet.toArray();
    Arrays.sort(keys);
    StringBuffer temp = new StringBuffer();
    boolean first = true;
    for (Object key : keys) {
        if (first) {
            first = false;
        } else {
            temp.append("&");
        }
        temp.append(key).append("=");
        Object value = params.get(key);
        String valueString = "";
        if (null != value) {
            valueString = value.toString();
        }
        if (encode) {
            temp.append(URLEncoder.encode(valueString, "UTF-8"));
        } else {
            temp.append(valueString);
        }
    }
    return temp.toString();
}

From source file:com.inclouds.hbase.utils.RegionServerPoker.java

/**
  * Selects random region/*w  w  w .  ja v a 2 s .c o m*/
  * @param set
  * @return
  */
private static HRegionInfo select(Set<HRegionInfo> set) {
    Random r = new Random();
    int i = r.nextInt(set.size());
    return (HRegionInfo) set.toArray()[i];

}

From source file:de.tudarmstadt.ukp.wikipedia.util.GraphUtilities.java

/** Get a random subset (of size pSize) of the page set passed to the method.
 * @param pPageIDs The pages.//from w w w  .  jav a 2  s. co m
 * @param pResultSetSize The size of the result set.
 * @return A random subset of the original page set of the given size or null, if the requested subset size is larger than the original page set.
 */
public static Set<Integer> getRandomPageSubset(Set<Integer> pPageIDs, int pResultSetSize) {

    Set<Integer> uniqueRandomSet = new HashSet<Integer>();

    if (pPageIDs.size() < pResultSetSize) {
        logger.error("Requested subset size is larger than the original page set size.");
        return null;
    }

    Random rand = new Random();

    Object[] pageIdArray = pPageIDs.toArray();

    // If pSize is quite close to the size of the original pageSet the probability of generating the offset of the last missing pageIDs is quite low, with the consequence of unpredictable run-time.
    // => if more than the half of pages should be included in the result set, better remove random numbers than adding them
    if (pResultSetSize > (pPageIDs.size() / 2)) {
        uniqueRandomSet.addAll(pPageIDs);
        while (uniqueRandomSet.size() > pResultSetSize) {
            int randomOffset = rand.nextInt(pPageIDs.size());
            if (uniqueRandomSet.contains(pageIdArray[randomOffset])) {
                uniqueRandomSet.remove(pageIdArray[randomOffset]);
            }
        }
    } else {
        while (uniqueRandomSet.size() < pResultSetSize) {
            int randomOffset = rand.nextInt(pPageIDs.size());
            if (!uniqueRandomSet.contains(pageIdArray[randomOffset])) {
                uniqueRandomSet.add((Integer) pageIdArray[randomOffset]);
            }
        }
    }

    return uniqueRandomSet;
}

From source file:bayesGame.ui.painter.OrNodePainter.java

public static Image paintPercentage(Color gridColor, Color falseColor, int size, int squaresize, BayesNode node,
        Fraction parentNode1Probability, Fraction parentNode2Probability) {

    BufferedImage img = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
    Graphics g = img.getGraphics();

    NodePainter.graphicBackgroundPainter(g, 0, 0, size, size);

    // get non-zero truth table entries from the node
    List<Map<Object, Boolean>> nonZeroEntries = node.getNonZeroProbabilities();

    // get the identities of its parents by taking the first map and querying it 
    // for KeySet, subtracting the object representing the node itself
    Set<Object> nodeParents = nonZeroEntries.get(0).keySet();
    nodeParents.remove(node.type);//from  ww  w . j av  a  2 s .c  om

    if (nodeParents.size() > 2) {
        throw new IllegalArgumentException("OR node with more than 2 parents not yet implemented");
    }

    Object[] nodeParentsArray = nodeParents.toArray();
    Object parent1 = nodeParentsArray[0];
    Object parent2 = nodeParentsArray[1];

    // for each map, check the truth table entry it corresponds to and color
    // those appropriately
    boolean p1true_p2true = false;
    boolean p1true_p2false = false;
    boolean p1false_p2true = false;
    boolean p1false_p2false = false;

    for (Map<Object, Boolean> map : nonZeroEntries) {
        Boolean parent1truth = map.get(parent1);
        Boolean parent2truth = map.get(parent2);

        if (parent1truth && parent2truth) {
            p1true_p2true = true;
        } else if (parent1truth && !parent2truth) {
            p1true_p2false = true;
        } else if (!parent1truth && parent2truth) {
            p1false_p2true = true;
        } else if (!parent1truth && !parent2truth) {
            p1false_p2false = true;
        }
    }

    int XSize = parentNode1Probability.multiply(size).intValue();
    int X_Size = size - XSize;
    int YSize = parentNode2Probability.multiply(size).intValue();
    int Y_Size = size - YSize;

    if (p1true_p2true) {
        NodePainter.squarePainter(g, 0, 0, XSize, YSize, gridColor, Color.BLACK);
    } else {
        NodePainter.squarePainter(g, 0, 0, XSize, YSize, NodePainter.RECTANGLE_BOX_BACKGROUND_COLOR,
                Color.BLACK);
    }

    NodePainter.squarePainter(g, XSize, 0, X_Size, YSize, gridColor, Color.BLACK);
    NodePainter.squarePainter(g, 0, YSize, XSize, Y_Size, gridColor, Color.BLACK);

    if (p1false_p2false) {
        NodePainter.squarePainter(g, XSize, YSize, X_Size, Y_Size, falseColor, Color.BLACK);
    } else {
        NodePainter.squarePainter(g, XSize, YSize, X_Size, Y_Size, NodePainter.RECTANGLE_BOX_BACKGROUND_COLOR,
                Color.BLACK);
    }

    return img;
}

From source file:com.aurel.track.fieldType.bulkSetters.ItemPickerBulkSetter.java

/**
 * Add the value to the existing ones//from www  .  j  a v  a 2 s .  c om
 * @param actualValue
 * @param addedValue
 * @return
 */
private static Object[] addItem(Object[] actualValue, Integer addedValue) {
    Set<Integer> set = new HashSet<Integer>();
    if (actualValue != null) {
        for (Object value : actualValue) {
            set.add((Integer) value);
        }
    }
    set.add(addedValue);
    return set.toArray();
}

From source file:ezbake.data.mongo.redact.RedactHelper.java

public static String createStringArray(Set<String> auths) {

    if (auths == null) {
        return "[ ]";
    }/*from   ww  w . j a v  a  2 s .c  om*/

    Set<String> newAuths = new HashSet<>();
    for (String auth : auths) {
        String newAuth = "'" + auth + "'";
        newAuths.add(newAuth);
    }
    return Arrays.toString(newAuths.toArray());
}

From source file:bayesGame.ui.painter.AndNodePainter.java

public static Image paintPercentage(Color gridColor, Color falseColor, int size, int squaresize, BayesNode node,
        Fraction parentNode1Probability, Fraction parentNode2Probability) {

    BufferedImage img = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
    Graphics g = img.getGraphics();

    NodePainter.graphicBackgroundPainter(g, 0, 0, size, size);

    // get non-zero truth table entries from the node
    List<Map<Object, Boolean>> nonZeroEntries = node.getNonZeroProbabilities();

    // get the identities of its parents by taking the first map and querying it 
    // for KeySet, subtracting the object representing the node itself
    Set<Object> nodeParents = nonZeroEntries.get(0).keySet();
    nodeParents.remove(node.type);/*from   w ww  .j a v  a  2  s. c  o  m*/

    if (nodeParents.size() > 2) {
        throw new IllegalArgumentException("AND node with more than 2 parents not yet implemented");
    }

    Object[] nodeParentsArray = nodeParents.toArray();
    Object parent1 = nodeParentsArray[0];
    Object parent2 = nodeParentsArray[1];

    // for each map, check the truth table entry it corresponds to and color
    // those appropriately
    boolean p1true_p2true = false;
    boolean p1true_p2false = false;
    boolean p1false_p2true = false;
    boolean p1false_p2false = false;

    for (Map<Object, Boolean> map : nonZeroEntries) {
        Boolean parent1truth = map.get(parent1);
        Boolean parent2truth = map.get(parent2);

        if (parent1truth && parent2truth) {
            p1true_p2true = true;
        } else if (parent1truth && !parent2truth) {
            p1true_p2false = true;
        } else if (!parent1truth && parent2truth) {
            p1false_p2true = true;
        } else if (!parent1truth && !parent2truth) {
            p1false_p2false = true;
        }
    }

    Color whiteColor = Color.WHITE;

    int XSize = parentNode1Probability.multiply(size).intValue();
    int X_Size = size - XSize;
    int YSize = parentNode2Probability.multiply(size).intValue();
    int Y_Size = size - YSize;

    if (p1true_p2true) {
        NodePainter.squarePainter(g, 0, 0, XSize, YSize, gridColor, Color.BLACK);
    } else {
        NodePainter.squarePainter(g, 0, 0, XSize, YSize, NodePainter.RECTANGLE_BOX_BACKGROUND_COLOR,
                Color.BLACK);
    }

    NodePainter.squarePainter(g, XSize, 0, X_Size, YSize, falseColor, Color.BLACK);
    NodePainter.squarePainter(g, 0, YSize, XSize, Y_Size, falseColor, Color.BLACK);

    if (p1false_p2false) {
        NodePainter.squarePainter(g, XSize, YSize, X_Size, Y_Size, falseColor, Color.BLACK);
    } else {
        NodePainter.squarePainter(g, XSize, YSize, X_Size, Y_Size, NodePainter.RECTANGLE_BOX_BACKGROUND_COLOR,
                Color.BLACK);
    }

    return img;
}

From source file:org.eel.kitchen.jsonschema.validator.ObjectValidatorTest.java

private static void checkNodeAndPaths(final Set<JsonNode> actual, final JsonNode expected) {
    assertEquals(actual.size(), expected.size());

    final Set<JsonNode> expectedSet = ImmutableSet.copyOf(expected);
    final Set<JsonNode> actualSet = Sets.newHashSet();

    Map<String, JsonNode> map;
    ObjectNode node;/*from  w  ww . ja va  2s  .c o  m*/

    for (final JsonNode element : actual) {
        node = JsonNodeFactory.instance.objectNode();
        map = JacksonUtils.nodeToMap(element);
        node.putAll(map);
        actualSet.add(node);
    }

    assertEqualsNoOrder(actualSet.toArray(), expectedSet.toArray());
}

From source file:com.strider.datadefender.DataDefender.java

/**
 * Returns the list of unparsed arguments as a list of table names by
 * transforming the strings to lower case.
 *
 * This guarantees table names to be in lower case, so functions comparing
 * can use contains() with a lower case name.
 *
 * If tables names are not supplied via command line, then will search the property file
 * for space separated list of table names.
 *
 * @param tableNames/*w  w w  . j  a  v  a 2  s  .  c o m*/
 * @param appProperties application property file
 * @param dbProperties database property file
 * @return The list of table names
 */
public static Set<String> getTableNames(final List<String> tableNames, final Properties dbProperties) {
    List<String> tableNameList = new ArrayList<String>(Arrays.asList(new String[tableNames.size()]));

    Collections.copy(tableNameList, tableNames);

    if (tableNameList.isEmpty()) {
        final String tableStr = dbProperties.getProperty("include-tables");

        if (tableStr != null) {
            tableNameList = Arrays.asList(tableStr.split(","));
            LOG.debug("Adding tables from property file.");
        }
    }

    final Set<String> tables = tableNameList.stream().map(s -> s.toLowerCase(Locale.ENGLISH))
            .collect(Collectors.toSet());

    LOG.info("Tables: " + Arrays.toString(tables.toArray()));

    return tables;
}

From source file:com.aurel.track.fieldType.bulkSetters.ItemPickerBulkSetter.java

/**
 * Add the value to the existing ones//from w w w. j a v  a2 s.c  o  m
 * @param actualValue
 * @param addedValue
 * @return
 */
private static Object[] removeItem(Object[] actualValue, Integer removeValue) {
    Set<Integer> set = new HashSet<Integer>();
    if (actualValue != null) {
        for (Object value : actualValue) {
            if (!removeValue.equals(value)) {
                set.add((Integer) value);
            }
        }
    }
    return set.toArray();
}