Example usage for java.util TreeMap containsKey

List of usage examples for java.util TreeMap containsKey

Introduction

In this page you can find the example usage for java.util TreeMap containsKey.

Prototype

public boolean containsKey(Object key) 

Source Link

Document

Returns true if this map contains a mapping for the specified key.

Usage

From source file:cooccurrence.Omer_Levy.java

/**
 * Method that will extract top D singular values from CoVariance Matrix 
 * It will then identify the corresponding columns from U and V and add it to new matrices 
 * @param U/*from w  ww .  j  a v a 2s .  co  m*/
 * @param V
 * @param coVariance
 * @param matrixUd
 * @param matrixVd
 * @param coVarD
 * @param indicesD 
 */
private static void getTopD(RealMatrix U, RealMatrix V, RealMatrix coVariance, RealMatrix matrixUd,
        RealMatrix matrixVd, RealMatrix coVarD, ArrayList<Integer> indicesD) {
    TreeMap<Double, Set<Integer>> tmap = new TreeMap<>();
    for (int i = 0; i < coVariance.getRowDimension(); i++) {
        double val = coVariance.getEntry(i, i);
        if (tmap.containsKey(val)) {
            Set<Integer> temp = tmap.get(val);
            temp.add(i);
        } else {
            Set<Integer> temp = new HashSet<>();
            temp.add(i);
            tmap.put(val, temp);
        }
    }
    Iterator iter = tmap.keySet().iterator();
    while (iter.hasNext()) {
        Double val = (Double) iter.next();
        Set<Integer> indices = tmap.get(val);
        for (int i = 0; i < indices.size(); i++) {
            Iterator iterIndices = indices.iterator();
            while (iterIndices.hasNext()) {
                int index = (Integer) iterIndices.next();
                indicesD.add(index);
                coVarD.addToEntry(index, index, val);
                matrixUd.setColumnVector(index, U.getColumnVector(index));
                matrixVd.setColumnVector(index, V.getColumnVector(index));
            }
        }
    }

}

From source file:Main.java

/**
 * Gather all the namespaces defined on a node
 *
 * @return// w w w.j a v a  2  s.  c o m
 */
public static Iterable<Entry<String, String>> getNamespaces(Element element) {
    TreeMap<String, String> map = new TreeMap<String, String>();
    do {
        NamedNodeMap attributes = element.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            Attr attr = (Attr) attributes.item(i);
            final String name = attr.getLocalName();

            if (attr.getPrefix() != null) {
                if ("xmlns".equals(attr.getPrefix()))
                    if (!map.containsKey(name))
                        map.put(name, attr.getValue());
            } else if ("xmlns".equals(name)) {
                if (!map.containsKey(""))
                    map.put("", attr.getValue());
            }
        }
        if (element.getParentNode() == null || element.getParentNode().getNodeType() != Node.ELEMENT_NODE)
            break;
        element = (Element) element.getParentNode();
    } while (true);
    return map.entrySet();
}

From source file:cfappserver.Bot.java

public static TreeMap<String, Integer> addToTree(String a, TreeMap<String, Integer> tm){
    if(tm.containsKey(a))tm.put(a, 1+tm.get(a));
    else tm.put(a, 1);
    return tm;// ww w  .j  a  v a  2s.co m
}

From source file:org.apache.hadoop.hbase.master.procedure.MasterDDLOperationHelper.java

/**
 * Reopen all regions from a table after a schema change operation.
 **//*from   www .  j  a  v a  2  s .c  om*/
public static boolean reOpenAllRegions(final MasterProcedureEnv env, final TableName tableName,
        final List<HRegionInfo> regionInfoList) throws IOException {
    boolean done = false;
    LOG.info("Bucketing regions by region server...");
    List<HRegionLocation> regionLocations = null;
    Connection connection = env.getMasterServices().getConnection();
    try (RegionLocator locator = connection.getRegionLocator(tableName)) {
        regionLocations = locator.getAllRegionLocations();
    }
    // Convert List<HRegionLocation> to Map<HRegionInfo, ServerName>.
    NavigableMap<HRegionInfo, ServerName> hri2Sn = new TreeMap<HRegionInfo, ServerName>();
    for (HRegionLocation location : regionLocations) {
        hri2Sn.put(location.getRegionInfo(), location.getServerName());
    }
    TreeMap<ServerName, List<HRegionInfo>> serverToRegions = Maps.newTreeMap();
    List<HRegionInfo> reRegions = new ArrayList<HRegionInfo>();
    for (HRegionInfo hri : regionInfoList) {
        ServerName sn = hri2Sn.get(hri);
        // Skip the offlined split parent region
        // See HBASE-4578 for more information.
        if (null == sn) {
            LOG.info("Skip " + hri);
            continue;
        }
        if (!serverToRegions.containsKey(sn)) {
            LinkedList<HRegionInfo> hriList = Lists.newLinkedList();
            serverToRegions.put(sn, hriList);
        }
        reRegions.add(hri);
        serverToRegions.get(sn).add(hri);
    }

    LOG.info("Reopening " + reRegions.size() + " regions on " + serverToRegions.size() + " region servers.");
    AssignmentManager am = env.getMasterServices().getAssignmentManager();
    am.setRegionsToReopen(reRegions);
    BulkReOpen bulkReopen = new BulkReOpen(env.getMasterServices(), serverToRegions, am);
    while (true) {
        try {
            if (bulkReopen.bulkReOpen()) {
                done = true;
                break;
            } else {
                LOG.warn("Timeout before reopening all regions");
            }
        } catch (InterruptedException e) {
            LOG.warn("Reopen was interrupted");
            // Preserve the interrupt.
            Thread.currentThread().interrupt();
            break;
        }
    }
    return done;
}

From source file:com.niki.normalizer.Metaphone.java

public static int DamerauLevenshteinDistance(String source, String target) {
    int m = source.length();
    int n = target.length();
    int[][] H = new int[m + 2][n + 2];

    int INF = m + n;
    H[0][0] = INF;/*from w w w  .  jav  a 2 s . c  o  m*/
    for (int i = 0; i <= m; i++) {
        H[i + 1][1] = i;
        H[i + 1][0] = INF;
    }
    for (int j = 0; j <= n; j++) {
        H[1][j + 1] = j;
        H[0][j + 1] = INF;
    }
    TreeMap<Character, Integer> sd = new TreeMap<Character, Integer>();
    for (int iter = 0; iter < (source + target).length(); iter++) {
        if (!sd.containsKey((source + target).charAt(iter))) {
            sd.put((source + target).charAt(iter), 0);
        }
    }

    for (int i = 1; i <= m; i++) {
        int DB = 0;
        for (int j = 1; j <= n; j++) {
            int i1 = sd.get(target.charAt(j - 1));
            int j1 = DB;

            if (source.charAt(i - 1) == target.charAt(j - 1)) {
                H[i + 1][j + 1] = H[i][j];
                DB = j;
            } else {
                H[i + 1][j + 1] = Math.min(H[i][j], Math.min(H[i + 1][j], H[i][j + 1])) + 1;
            }

            H[i + 1][j + 1] = Math.min(H[i + 1][j + 1], H[i1][j1] + (i - i1 - 1) + 1 + (j - j1 - 1));
        }

        sd.put(source.charAt(i - 1), i);
    }
    return H[m + 1][n + 1];
}

From source file:com.sshtools.common.vomanagementtool.common.VOHelper.java

public static boolean addVOToFavourites(String voName, String fromGroup) {
    boolean isAdded = false;
    //First check if VO is already in Favourites
    TreeMap allFavVOs = getVOGroup(FAVOURITES);
    if (allFavVOs.containsKey(voName)) {
        JOptionPane.showMessageDialog(null, "VO '" + voName + "' is already in " + FAVOURITES + ".",
                "Error: Add VO to " + FAVOURITES, JOptionPane.ERROR_MESSAGE);
    } else {/*from   w w  w.j a v  a  2s.c  om*/
        TreeMap groupVOs = getVOGroup(fromGroup);
        if (groupVOs.containsKey(voName)) {
            List voDetails = (List) groupVOs.get(voName);
            allFavVOs.put(voName, voDetails);
            String newContent = createStringFromTreeMap(allFavVOs);
            try {
                writeToFile(favouritesFile, newContent);
                isAdded = true;
            } catch (IOException e) {
                JOptionPane.showMessageDialog(
                        null, "Cannot add VO '" + voName + "' to " + FAVOURITES + ". Write to file '"
                                + favouritesFile + "' failed",
                        "Error: Add VO to " + FAVOURITES, JOptionPane.ERROR_MESSAGE);
            }

        }
    }
    return isAdded;
}

From source file:io.apiman.tools.i18n.TemplateScanner.java

/**
 * Scan the given html template using jsoup and find all strings that require translation.  This is
 * done by finding all elements with a "apiman-i18n-key" attribute.
 * @param file/* w  ww .ja v  a 2  s  .  co m*/
 * @param strings
 * @throws IOException
 */
private static void scanFile(File file, TreeMap<String, String> strings) throws IOException {
    Document doc = Jsoup.parse(file, "UTF-8");

    // First, scan for elements with the 'apiman-i18n-key' attribute.  These require translating.
    Elements elements = doc.select("*[apiman-i18n-key]");
    for (Element element : elements) {
        String i18nKey = element.attr("apiman-i18n-key");
        boolean isNecessary = false;

        // Process the element text (if the element has no children)
        if (strings.containsKey(i18nKey)) {
            if (hasNoChildren(element)) {
                isNecessary = true;
                String elementVal = element.text();
                if (elementVal.trim().length() > 0 && !elementVal.contains("{{")) {
                    String currentValue = strings.get(i18nKey);
                    if (!currentValue.equals(elementVal)) {
                        throw new IOException("Duplicate i18n key found with different default values.  Key="
                                + i18nKey + "  Value1=" + elementVal + "  Value2=" + currentValue);
                    }
                }
            }
        } else {
            if (hasNoChildren(element)) {
                String elementVal = element.text();
                if (elementVal.trim().length() > 0 && !elementVal.contains("{{")) {
                    isNecessary = true;
                    strings.put(i18nKey, elementVal);
                }
            }
        }

        // Process the translatable attributes
        for (String tattr : TRANSLATABLE_ATTRIBUTES) {
            if (element.hasAttr(tattr)) {
                String attrValue = element.attr(tattr);
                if (attrValue.contains("{{")) {
                    continue;
                }
                String attrI18nKey = i18nKey + '.' + tattr;
                String currentAttrValue = strings.get(attrI18nKey);
                if (currentAttrValue == null) {
                    isNecessary = true;
                    strings.put(attrI18nKey, attrValue);
                } else if (!currentAttrValue.equals(attrValue)) {
                    throw new IOException(
                            "Duplicate i18n key found with different default values (for attribute '" + tattr
                                    + "').  Key=" + attrI18nKey + "  Value1=" + attrValue + "  Value2="
                                    + currentAttrValue);
                } else {
                    isNecessary = true;
                }
            }
        }

        if (!isNecessary) {
            throw new IOException("Detected an unnecessary apiman-i18n-key attribute in file '" + file.getName()
                    + "' on element: " + element);
        }
    }

    // Next, scan all elements to see if the element *should* be marked for translation
    elements = doc.select("*");
    for (Element element : elements) {
        if (element.hasAttr("apiman-i18n-key") || element.hasAttr("apiman-i18n-skip")) {
            continue;
        }
        if (hasNoChildren(element)) {
            String value = element.text();
            if (value != null && value.trim().length() > 0) {
                if (!value.contains("{{")) {
                    throw new IOException("Found an element in '" + file.getName()
                            + "' that should be translated:  " + element);
                }
            }
        }
    }

    // Next scan elements with a translatable attribute and fail if any of those elements
    // are missing the apiman-i18n-key attribute.
    for (String tattr : TRANSLATABLE_ATTRIBUTES) {
        elements = doc.select("*[" + tattr + "]");
        for (Element element : elements) {
            if (element.hasAttr("apiman-i18n-key") || element.hasAttr("apiman-i18n-skip")
                    || element.attr(tattr).contains("{{")) {
                continue;
            } else {
                throw new IOException("In template '" + file.getName() + "', found an element with a '" + tattr
                        + "' attribute but missing 'apiman-i18n-key': " + element);
            }
        }
    }

}

From source file:com.sshtools.common.vomanagementtool.common.VOHelper.java

public static boolean removeVOFromFavourites(String voName) {
    boolean isRemoved = false;
    TreeMap allFavVOs = getVOGroup(FAVOURITES);
    Iterator<Map.Entry<String, TreeMap>> entries = allFavVOs.entrySet().iterator();
    while (entries.hasNext()) {
        Map.Entry<String, TreeMap> entry = entries.next();
        String vonameinMap = entry.getKey();
        if (voName.equals(vonameinMap)) {
            //Check also if it is a self-VO, if yes, need to remove folders too
            TreeMap allEGIVOs = getVOGroup(EGI);
            TreeMap allOTHERSVO = getVOGroup(OTHERS);
            if (!allEGIVOs.containsKey(voName) && !allOTHERSVO.containsKey(voName)) {
                try {
                    delete(new File(vomsdir + File.separator + voName));
                } catch (IOException e) {
                    JOptionPane.showMessageDialog(null,
                            "Cannot remove dirctory '" + vomsdir + File.separator + voName + "'.",
                            "Error: Remove VO from " + FAVOURITES, JOptionPane.ERROR_MESSAGE);
                }// w ww  .j  av  a 2  s . c o m
            }
            allFavVOs.remove(vonameinMap);
            isRemoved = true;
            break;
        }
    }
    if (isRemoved) {
        String newContent = createStringFromTreeMap(allFavVOs);
        try {
            writeToFile(favouritesFile, newContent);
        } catch (IOException e) {
            isRemoved = false;
            JOptionPane.showMessageDialog(null,
                    "Failed to remove VO from Favourites. Cannot write to file '" + favouritesFile + "'",
                    "Error: Remove VO", JOptionPane.ERROR_MESSAGE);
        }
    }

    return isRemoved;
}

From source file:Main.java

public static Window getOwnerForChildWindow() {
    Window w = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow();
    if (w != null) {
        return w;
    }/*from   w ww.j a  v a2s.co m*/
    w = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();
    if (w != null) {
        return w;
    }
    /*
     * Priority level1
     * modal dialog: +200
     * non-modal dialog: +100
     * frame: +0
     *
     * Priority level2
     * no owned windows: +10
     */
    TreeMap<Integer, Window> prioMap = new TreeMap<Integer, Window>();
    for (Window cand : Window.getWindows()) {
        if (cand == null) {
            continue;
        }
        if (!cand.isVisible()) {
            continue;
        }
        if (!cand.isShowing()) {
            continue;
        }
        int prio = 0;
        Window[] children = cand.getOwnedWindows();
        if (children == null || children.length == 0) {
            prio += 10;
        }
        if (cand instanceof Dialog) {
            Dialog dlg = (Dialog) cand;
            if (dlg.isModal()) {
                prio += 200;
            } else {
                prio += 100;
            }
            prioMap.put(prio, cand);
        } else if (cand instanceof Frame) {
            if (!prioMap.containsKey(prio)) {
                prioMap.put(prio, cand);
            }
        }
    }
    if (prioMap.size() > 0) {
        return prioMap.get(prioMap.lastKey());
    }
    //last line of defense
    if (prioMap.size() == 0) {
        for (Window cand : Window.getWindows()) {
            if (cand == null) {
                continue;
            }
            if (cand.isVisible()) {
                return cand;
            }
        }
    }
    return null;
}

From source file:com.sfs.whichdoctor.xml.writer.helper.AccreditationXmlHelper.java

/**
 * Output the training summary as an XML string.
 *
 * @param trainingSummary the training summary
 * @param type the type/*from   ww  w  .  j  a  v a 2  s.  co  m*/
 *
 * @return the xml string
 */
public static String getSummaryXml(final TreeMap<String, AccreditationBean[]> trainingSummary,
        final String type) {

    final XmlWriter xmlwriter = new XmlWriter();

    if (trainingSummary.size() > 0) {

        int totalCore = 0;
        int totalNonCore = 0;

        TreeMap<String, ArrayList<AccreditationBean[]>> summaryTreemap = new TreeMap<String, ArrayList<AccreditationBean[]>>();

        for (String summaryKey : trainingSummary.keySet()) {

            AccreditationBean[] details = trainingSummary.get(summaryKey);
            AccreditationBean core = details[0];
            AccreditationBean nonCore = details[1];

            totalCore += core.getWeeksCertified();
            totalNonCore += nonCore.getWeeksCertified();

            if (StringUtils.isNotBlank(core.getSpecialtyType())) {

                ArrayList<AccreditationBean[]> summaries = new ArrayList<AccreditationBean[]>();

                if (!summaryTreemap.containsKey(core.getSpecialtyType())) {
                    /* New type of specialty */
                    summaries.add(details);
                } else {
                    /* Existing specialty */
                    summaries = summaryTreemap.get(core.getSpecialtyType());
                    summaries.add(details);
                }
                summaryTreemap.put(core.getSpecialtyType(), summaries);
            }
        }

        xmlwriter.writeEntity("trainingSummary");
        xmlwriter.writeAttribute("type", type);
        xmlwriter.writeAttribute("totalCore", Formatter.getWholeMonths(totalCore));
        xmlwriter.writeAttribute("totalNonCore", Formatter.getWholeMonths(totalNonCore));

        xmlwriter.writeEntity("specialtyTraining");

        for (String specialtyType : summaryTreemap.keySet()) {

            ArrayList<AccreditationBean[]> summaries = summaryTreemap.get(specialtyType);
            int typeCoreWeeks = 0;
            int typeNCWeeks = 0;

            if (summaries != null) {
                // For each accredited specialty create an element
                xmlwriter.writeEntity("specialty");
                xmlwriter.writeEntity("subtypes");
                String division = "";
                String abbreviation = "";
                String specialtytype = "";
                String typeAbbreviation = "";
                for (Object[] summary : summaries) {
                    boolean blSubType = false;
                    AccreditationBean core = (AccreditationBean) summary[0];
                    AccreditationBean nonCore = (AccreditationBean) summary[1];

                    division = core.getAccreditationClass();
                    abbreviation = core.getAbbreviation();
                    specialtytype = core.getSpecialtyType();
                    typeAbbreviation = core.getSpecialtyTypeAbbreviation();

                    if (StringUtils.isNotBlank(core.getSpecialtySubType())) {
                        blSubType = true;
                        xmlwriter.writeEntity("subtype");
                        xmlwriter.writeEntity("name").writeText(core.getSpecialtySubType()).endEntity();
                        xmlwriter.writeEntity("coreMonths")
                                .writeText(Formatter.getWholeMonths(core.getWeeksCertified())).endEntity();
                        xmlwriter.writeEntity("nonCoreMonths")
                                .writeText(Formatter.getWholeMonths(nonCore.getWeeksCertified())).endEntity();

                        xmlwriter.endEntity();

                        typeCoreWeeks += core.getWeeksCertified();
                        typeNCWeeks += nonCore.getWeeksCertified();
                    }
                    if (!blSubType) {
                        xmlwriter.writeEntity("subtype");
                        xmlwriter.writeEntity("coreMonths")
                                .writeText(Formatter.getWholeMonths(core.getWeeksCertified())).endEntity();
                        xmlwriter.writeEntity("nonCoreMonths")
                                .writeText(Formatter.getWholeMonths(nonCore.getWeeksCertified())).endEntity();

                        xmlwriter.endEntity();

                        typeCoreWeeks += core.getWeeksCertified();
                        typeNCWeeks += nonCore.getWeeksCertified();
                    }
                }
                xmlwriter.endEntity();

                xmlwriter.writeEntity("division").writeText(division).endEntity();
                xmlwriter.writeEntity("abbreviation").writeText(abbreviation).endEntity();
                xmlwriter.writeEntity("type").writeText(specialtytype).endEntity();
                xmlwriter.writeEntity("typeAbbreviation").writeText(typeAbbreviation).endEntity();
                xmlwriter.writeEntity("coreMonths").writeText(Formatter.getWholeMonths(typeCoreWeeks))
                        .endEntity();
                xmlwriter.writeEntity("nonCoreMonths").writeText(Formatter.getWholeMonths(typeNCWeeks))
                        .endEntity();

                xmlwriter.endEntity();
            }
        }
        xmlwriter.endEntity();

        xmlwriter.endEntity();
    }
    return xmlwriter.getXml();
}