Example usage for java.util TreeMap entrySet

List of usage examples for java.util TreeMap entrySet

Introduction

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

Prototype

EntrySet entrySet

To view the source code for java.util TreeMap entrySet.

Click Source Link

Document

Fields initialized to contain an instance of the entry set view the first time this view is requested.

Usage

From source file:com.rackspacecloud.blueflood.io.serializers.HistogramSerializationTest.java

private boolean areHistogramsEqual(HistogramRollup first, HistogramRollup second) {
    final TreeMap<Double, Double> firstBinsAsOrderedMap = getNonZeroBinsAsMap(first);
    final TreeMap<Double, Double> secondBinsAsOrderedMap = getNonZeroBinsAsMap(second);

    if (firstBinsAsOrderedMap.size() != secondBinsAsOrderedMap.size()) {
        return false;
    }//from w  w w. j  a v  a  2 s. co m

    for (Map.Entry<Double, Double> firstBin : firstBinsAsOrderedMap.entrySet()) {
        Double val = secondBinsAsOrderedMap.get(firstBin.getKey());
        if (val == null || !firstBin.getValue().equals(val)) {
            return false;
        }
    }

    return true;
}

From source file:org.apache.lens.lib.query.FilePersistentFormatter.java

@Override
public void addRowsFromPersistedPath(Path persistedDir) throws IOException {
    FileSystem persistFs = persistedDir.getFileSystem(ctx.getConf());

    FileStatus[] partFiles = persistFs.listStatus(persistedDir, new PathFilter() {
        @Override// ww  w  .  j a v a  2  s.c  om
        public boolean accept(Path path) {
            if (path.getName().startsWith("_")) {
                return false;
            }
            return true;
        }
    });

    TreeMap<PartFile, FileStatus> partFileMap = new TreeMap<PartFile, FileStatus>();
    try {
        for (FileStatus file : partFiles) {
            partFileMap.put(new PartFile(file.getPath().getName()), file);
        }

        for (Map.Entry<PartFile, FileStatus> entry : partFileMap.entrySet()) {
            LOG.info("Processing file:" + entry.getValue().getPath());
            BufferedReader in = null;
            try {
                in = new BufferedReader(new InputStreamReader(persistFs.open(entry.getValue().getPath())));
                String row = in.readLine();
                while (row != null) {
                    writeRow(row);
                    row = in.readLine();
                }
            } finally {
                if (in != null) {
                    in.close();
                }
            }
        }
    } catch (ParseException e) {
        throw new IOException(e);
    }
}

From source file:ANNFileDetect.EncogTestClass.java

private void createReport(TreeMap<Double, Integer> ht, String file) throws IOException {
    TreeMap<Integer, ArrayList<Double>> tm = new TreeMap<Integer, ArrayList<Double>>();
    for (Map.Entry<Double, Integer> entry : ht.entrySet()) {
        if (tm.containsKey(entry.getValue())) {
            ArrayList<Double> al = (ArrayList<Double>) tm.get(entry.getValue());
            al.add(entry.getKey());/*from   w  ww . j av  a  2  s. c o m*/
            tm.put(entry.getValue(), al);
        } else {
            ArrayList<Double> al = new ArrayList<Double>();
            al.add(entry.getKey());
            tm.put(entry.getValue(), al);
        }
    }

    String[] tmpfl = file.split("/");
    if (tmpfl.length < 2)
        tmpfl = file.split("\\\\");

    String crp = tmpfl[tmpfl.length - 1];
    String[] actfl = crp.split("\\.");
    FileWriter fstream = new FileWriter("tempTrainingFiles/" + actfl[1].toUpperCase() + actfl[0] + ".txt");
    BufferedWriter fileto = new BufferedWriter(fstream);
    int size = tm.size();
    int cnt = 0;
    for (Map.Entry<Integer, ArrayList<Double>> entry : tm.entrySet()) {
        if (cnt > (size - 10) && entry.getKey() > 2 && entry.getValue().size() < 20) {
            double tmpval = ((double) entry.getKey()) / filebytes;
            fileto.write("Times: " + tmpval + " Values: ");
            for (Double dbl : entry.getValue()) {
                fileto.write(dbl + " ");
            }
            fileto.write("\n");
        }

        cnt++;
    }
    fileto.close();
}

From source file:org.commoncrawl.util.NodeAffinityMaskBuilder.java

public static String buildNodeAffinityMask(FileSystem fileSystem, Path partFileDirectory,
        Map<Integer, String> optionalRootMapHint, Set<String> excludedNodeList, int maxReducersPerNode,
        boolean skipBalance) throws IOException {

    TreeMap<Integer, String> partitionToNodeMap = new TreeMap<Integer, String>();
    FileStatus paths[] = fileSystem.globStatus(new Path(partFileDirectory, "part-*"));

    if (paths.length == 0) {
        throw new IOException("Invalid source Path:" + partFileDirectory);
    }/*from w  w w  .  j a v a2s  .  c o m*/

    Multimap<String, Integer> inverseMap = TreeMultimap.create();
    Map<Integer, List<String>> paritionToDesiredCandidateList = new TreeMap<Integer, List<String>>();

    // iterate paths 
    for (FileStatus path : paths) {

        String currentFile = path.getPath().getName();
        int partitionNumber;
        try {
            if (currentFile.startsWith("part-r")) {
                partitionNumber = NUMBER_FORMAT.parse(currentFile.substring("part-r-".length())).intValue();
            } else {
                partitionNumber = NUMBER_FORMAT.parse(currentFile.substring("part-".length())).intValue();
            }
        } catch (ParseException e) {
            throw new IOException("Invalid Part Name Encountered:" + currentFile);
        }

        // get block locations 
        BlockLocation locations[] = fileSystem.getFileBlockLocations(path, 0, path.getLen());

        // if passed in root map is not null, then validate that all blocks for the current file reside on the desired node 
        if (optionalRootMapHint != null) {
            // the host all blocks should reside on 
            String desiredHost = optionalRootMapHint.get(partitionNumber);

            ArrayList<String> misplacedBlocks = new ArrayList<String>();
            // ok walk all blocks 
            for (BlockLocation location : locations) {
                boolean found = false;
                for (String host : location.getHosts()) {
                    if (host.compareTo(desiredHost) == 0) {
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    misplacedBlocks.add("Block At:" + location.getOffset() + " for File:" + path.getPath()
                            + " did not contain desired location:" + desiredHost);
                }

            }
            // ok pass test at a certain threshold 
            if (misplacedBlocks.size() != 0
                    && ((float) misplacedBlocks.size() / (float) locations.length) > .50f) {
                LOG.error("Misplaced Blocks Exceed Threshold");
                for (String misplacedBlock : misplacedBlocks) {
                    LOG.error(misplacedBlock);
                }
                // TODO: SKIP THIS STEP FOR NOW ??? 
                //throw new IOException("Misplaced Blocks Exceed Threshold!");
            }
            partitionToNodeMap.put(partitionNumber, desiredHost);
        } else {
            if (excludedNodeList != null) {
                // LOG.info("Exclued Node List is:" + Lists.newArrayList(excludedNodeList).toString());
            }
            // ok ask file system for block locations
            TreeMap<String, Integer> nodeToBlockCount = new TreeMap<String, Integer>();

            for (BlockLocation location : locations) {
                for (String host : location.getHosts()) {
                    if (excludedNodeList == null || !excludedNodeList.contains(host)) {
                        Integer nodeHitCount = nodeToBlockCount.get(host);
                        if (nodeHitCount == null) {
                            nodeToBlockCount.put(host, 1);
                        } else {
                            nodeToBlockCount.put(host, nodeHitCount.intValue() + 1);
                        }
                    }
                }
            }

            if (nodeToBlockCount.size() == 0) {
                throw new IOException("No valid nodes found for partition number:" + path);
            }

            Map.Entry<String, Integer> entries[] = nodeToBlockCount.entrySet().toArray(new Map.Entry[0]);
            Arrays.sort(entries, new Comparator<Map.Entry<String, Integer>>() {

                @Override
                public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
                    return o1.getValue().intValue() < o2.getValue().intValue() ? 1
                            : o1.getValue().intValue() == o2.getValue().intValue() ? 0 : -1;
                }
            });

            // build a list of nodes by priority ... 
            List<String> nodesByPriority = Lists.transform(Lists.newArrayList(entries),
                    new Function<Map.Entry<String, Integer>, String>() {

                        @Override
                        public String apply(Entry<String, Integer> entry) {
                            return entry.getKey();
                        }
                    });

            // stash it away ... 
            paritionToDesiredCandidateList.put(partitionNumber, nodesByPriority);
            //LOG.info("Mapping Partition:" + partitionNumber + " To Node:" + entries[0].getKey() + " BlockCount" + entries[0].getValue().intValue());
            partitionToNodeMap.put(partitionNumber, entries[0].getKey());
            // store the inverse mapping ... 
            inverseMap.put(entries[0].getKey(), partitionNumber);
        }
    }

    if (skipBalance) {
        // walk partition map to make sure everything is assigned ...
        /*
        for (String node : inverseMap.keys()) { 
          if (inverseMap.get(node).size() > maxReducersPerNode) { 
            throw new IOException("Node:" + node + " has too many partitions! ("+inverseMap.get(node).size());
          }
        }
        */
    }

    // now if optional root map hint is null 
    if (optionalRootMapHint == null && !skipBalance) {
        // figure out if there is an imbalance
        int avgRegionsPerNode = (int) Math.floor((float) paths.length / (float) inverseMap.keySet().size());
        int maxRegionsPerNode = (int) Math.ceil((float) paths.length / (float) inverseMap.keySet().size());
        LOG.info("Attempting to ideally balance nodes. Avg paritions per node:" + avgRegionsPerNode);

        // two passes .. 
        for (int pass = 0; pass < 2; ++pass) {
            LOG.info("Pass:" + pass);
            // iterate nodes ... 
            for (String node : ImmutableSet.copyOf(inverseMap.keySet())) {
                // get paritions in map  
                Collection<Integer> paritions = ImmutableList.copyOf(inverseMap.get(node));
                // if parition count exceeds desired average ... 
                if (paritions.size() > maxRegionsPerNode) {
                    // first pass, assign based on preference 
                    if (pass == 0) {
                        LOG.info("Node:" + node + " parition count:" + paritions.size() + " exceeds avg:"
                                + avgRegionsPerNode);
                        // walk partitions trying to find a node to discrard the parition to 
                        for (int partition : paritions) {
                            for (String candidate : paritionToDesiredCandidateList.get(partition)) {
                                if (!candidate.equals(node)) {
                                    // see if this candidate has room ..
                                    if (inverseMap.get(candidate).size() < avgRegionsPerNode) {
                                        LOG.info("REASSIGNING parition:" + partition + " from Node:" + node
                                                + " to Node:" + candidate);
                                        // found match reassign it ... 
                                        inverseMap.remove(node, partition);
                                        inverseMap.put(candidate, partition);
                                        break;
                                    }
                                }
                            }
                            // break out if reach our desired number of paritions for this node 
                            if (inverseMap.get(node).size() == avgRegionsPerNode)
                                break;
                        }
                    }
                    // second pass ... assign based on least loaded node ... 
                    else {
                        int desiredRelocations = paritions.size() - maxRegionsPerNode;
                        LOG.info("Desired Relocation for node:" + node + ":" + desiredRelocations
                                + " partitions:" + paritions.size());
                        for (int i = 0; i < desiredRelocations; ++i) {
                            String leastLoadedNode = null;
                            int leastLoadedNodePartitionCount = 0;

                            for (String candidateNode : inverseMap.keySet()) {
                                if (leastLoadedNode == null || inverseMap.get(candidateNode)
                                        .size() < leastLoadedNodePartitionCount) {
                                    leastLoadedNode = candidateNode;
                                    leastLoadedNodePartitionCount = inverseMap.get(candidateNode).size();
                                }
                            }
                            int bestPartition = -1;
                            int bestParitionOffset = -1;

                            for (int candidateParition : inverseMap.get(node)) {
                                int offset = 0;
                                for (String nodeCandidate : paritionToDesiredCandidateList
                                        .get(candidateParition)) {
                                    if (nodeCandidate.equals(leastLoadedNode)) {
                                        if (bestPartition == -1 || bestParitionOffset > offset) {
                                            bestPartition = candidateParition;
                                            bestParitionOffset = offset;
                                        }
                                        break;
                                    }
                                    offset++;
                                }
                            }
                            if (bestPartition == -1) {
                                bestPartition = Iterables.get(inverseMap.get(node), 0);
                            }
                            LOG.info("REASSIGNING parition:" + bestPartition + " from Node:" + node
                                    + " to Node:" + leastLoadedNode);
                            // found match reassign it ... 
                            inverseMap.remove(node, bestPartition);
                            inverseMap.put(leastLoadedNode, bestPartition);
                        }
                    }
                }
            }
        }
        LOG.info("Rebuilding parition to node map based on ideal balance");
        for (String node : inverseMap.keySet()) {
            LOG.info("Node:" + node + " has:" + inverseMap.get(node).size() + " partitions:"
                    + inverseMap.get(node).toString());
        }

        partitionToNodeMap.clear();
        for (Map.Entry<String, Integer> entry : inverseMap.entries()) {
            partitionToNodeMap.put(entry.getValue(), entry.getKey());
        }
    }

    StringBuilder builder = new StringBuilder();
    int itemCount = 0;
    for (Map.Entry<Integer, String> entry : partitionToNodeMap.entrySet()) {
        if (itemCount++ != 0)
            builder.append("\t");
        builder.append(entry.getKey().intValue() + "," + entry.getValue());
    }

    return builder.toString();
}

From source file:org.modeshape.web.jcr.rest.handler.RestItemHandlerImpl.java

private List<RestItem> updateMultipleNodes(Request request, Session session,
        TreeMap<String, JsonNode> nodesByPath) throws RepositoryException {
    List<RestItem> result = new ArrayList<RestItem>();
    for (Map.Entry<String, JsonNode> nodePath : nodesByPath.entrySet()) {
        Item item = session.getItem(nodePath.getKey());
        item = updateItem(item, nodePath.getValue());
        result.add(createRestItem(request, 0, session, item));
    }//from w  w  w  .  jav a 2  s .c o  m
    session.save();
    return result;
}

From source file:com.xpn.xwiki.objects.classes.GroupsClass.java

@Override
public void displayEdit(StringBuffer buffer, String name, String prefix, BaseCollection object,
        XWikiContext context) {//  w  ww  .j a  va  2s. c om
    select select = new select(prefix + name, 1);
    select.setMultiple(isMultiSelect());
    select.setSize(getSize());
    select.setName(prefix + name);
    select.setID(prefix + name);
    select.setDisabled(isDisabled());

    List<String> list;
    if (isUsesList()) {
        list = getList(context);
    } else {
        list = new ArrayList<String>();
    }

    List<String> selectlist;

    BaseProperty prop = (BaseProperty) object.safeget(name);
    if (prop == null) {
        selectlist = new ArrayList<String>();
    } else {
        selectlist = getListFromString((String) prop.getValue());
    }

    list.remove("XWiki.XWikiAllGroup");
    list.add(0, "XWiki.XWikiAllGroup");
    if (!context.isMainWiki()) {
        list.remove("xwiki:XWiki.XWikiAllGroup");
        list.add(1, "xwiki:XWiki.XWikiAllGroup");
    }

    // Add options from Set

    for (String value : selectlist) {
        if (!list.contains(value)) {
            list.add(value);
        }
    }

    // Sort the group list
    TreeMap<String, String> map = new TreeMap<String, String>();
    for (String value : list) {
        map.put(getText(value, context), value);
    }

    for (Map.Entry<String, String> entry : map.entrySet()) {
        String display = entry.getKey();
        String value = entry.getValue();

        option option = new option(display, value);
        option.addElement(display);
        if (selectlist.contains(value)) {
            option.setSelected(true);
        }
        select.addElement(option);
    }

    buffer.append(select.toString());

    if (!isUsesList()) {
        input in = new input();
        in.setName(prefix + "newgroup");
        in.setID(prefix + "newgroup");
        in.setSize(15);
        in.setDisabled(isDisabled());
        buffer.append("<br />");
        buffer.append(in.toString());

        if (!isDisabled()) {
            button button = new button();
            button.setTagText("Add");

            button.setOnClick("addGroup(this.form,'" + prefix + "'); return false;");
            buffer.append(button.toString());
        }
    }

    input in = new input();
    in.setType("hidden");
    in.setName(prefix + name);
    in.setDisabled(isDisabled());
    buffer.append(in.toString());
}

From source file:edu.utexas.cs.tactex.utilityestimation.UtilityArchitectureTariffRevoker.java

/**
 * Could be modified into a template method 
 *//*from  w  ww .  ja  va2 s.c om*/
@Override
public List<TariffSpecification> selectTariffsToRevoke(boolean useCanUse,
        HashMap<TariffSpecification, HashMap<CustomerInfo, Integer>> tariffSubscriptions,
        List<TariffSpecification> competingTariffs, MarketManager marketManager, ContextManager contextManager,
        CostCurvesPredictor costCurvesPredictor, int currentTimeslot, Broker me) {

    boolean customerPerspective = true;
    HashMap<CustomerInfo, ArrayRealVector> customer2estimatedEnergy = energyPredictionManager
            .getAbout7dayPredictionForAllCustomers(customerPerspective, currentTimeslot, /*false*/ true); // false: don't use fixed-rate only

    TreeMap<Double, TariffSpecification> sortedTariffs = tariffOptimizerRevoke.optimizeTariffs(
            tariffSubscriptions, customer2estimatedEnergy, competingTariffs, marketManager, contextManager,
            costCurvesPredictor, currentTimeslot, me);

    log.info("(Revoke) Estimated Utilities: ");
    for (Entry<Double, TariffSpecification> e : sortedTariffs.entrySet()) {
        log.info("u: " + e.getKey() + " spec: " + e.getValue());
    }

    List<TariffSpecification> specsToAdd = selectTariffsToRevoke(currentTimeslot, sortedTariffs,
            tariffSubscriptions.keySet());

    return specsToAdd;
}

From source file:org.decojer.cavaj.utils.SwitchTypes.java

/**
 * Rewrite string-switches from hash to value: Apply previously extracted case value maps to
 * bytecode case edges.//from ww  w.  j  av a2  s. co m
 *
 * This works differently to {@link #rewriteCaseValues(BB, Map)} because strings could yield to
 * same hashes and cases have to be restructered. It is more reasonable to add new case edges
 * and delete all previous case edges.
 *
 * @param switchHead
 *            switch head
 * @param string2bb
 *            case value map: string to BB
 * @param defaultCase
 *            default case
 */
public static void rewriteCaseStrings(@Nonnull final BB switchHead, @Nonnull final Map<String, BB> string2bb,
        @Nonnull final BB defaultCase) {
    // remember old switch case edges, for final deletion
    final List<E> outs = switchHead.getOuts();
    int i = outs.size();

    // build sorted map: unique case pc -> matching case keys
    final TreeMap<Integer, List<String>> casePc2values = Maps.newTreeMap();
    // add case branches
    for (final Map.Entry<String, BB> string2bbEntry : string2bb.entrySet()) {
        final int casePc = string2bbEntry.getValue().getPc();
        List<String> caseValues = casePc2values.get(casePc);
        if (caseValues == null) {
            caseValues = Lists.newArrayList();
            casePc2values.put(casePc, caseValues); // pc-sorted
        }
        caseValues.add(string2bbEntry.getKey());
    }
    // add default branch
    final int defaultPc = defaultCase.getPc();
    List<String> defaultCaseValues = casePc2values.get(defaultPc);
    if (defaultCaseValues == null) {
        defaultCaseValues = Lists.newArrayList();
        casePc2values.put(defaultPc, defaultCaseValues);
    }
    defaultCaseValues.add(null);

    // now add successors, preserve pc-order as edge-order
    for (final Map.Entry<Integer, List<String>> casePc2valuesEntry : casePc2values.entrySet()) {
        final List<String> caseValues = casePc2valuesEntry.getValue();

        // get BB from first value via previous string2bb map
        final String caseValue = caseValues.get(0);
        final BB caseBb = caseValue == null ? defaultCase : string2bb.get(caseValue);
        assert caseBb != null;

        final Object[] values = caseValues.toArray(new Object[caseValues.size()]);
        assert values != null;
        switchHead.addSwitchCase(caseBb, values);
    }
    // delete all previous outgoing switch cases
    for (; i-- > 0;) {
        final E out = outs.get(i);
        if (out.isSwitchCase()) {
            out.remove();
        }
    }
}

From source file:nl.uva.contextualsuggestion.Ranker_2014.java

public void refineTests() throws FileNotFoundException, IOException {
    String inputProfiles = "test2014.txt";
    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("test2014.qrel", true)));
    String line = null;//from w  w w  . j  a v a 2  s .  co m
    BufferedReader br = new BufferedReader(new FileReader(inputProfiles));
    TreeMap<String, String> lines = new TreeMap<>();
    while ((line = br.readLine()) != null) {
        String[] arr = line.split("\\s+");
        String key = arr[0] + " " + arr[1] + " " + arr[2];
        String val = arr[3];
        lines.put(key, val);
    }
    for (Entry<String, String> e : lines.entrySet()) {
        out.println(e.getKey() + " " + e.getValue());
    }
    out.close();
}

From source file:com.xpn.xwiki.objects.classes.UsersClass.java

@Override
public void displayEdit(StringBuffer buffer, String name, String prefix, BaseCollection object,
        XWikiContext context) {// w  w  w  .  j  a va2 s.c  o  m
    select select = new select(prefix + name, 1);
    select.setMultiple(isMultiSelect());
    select.setSize(getSize());
    select.setName(prefix + name);
    select.setID(prefix + name);
    select.setDisabled(isDisabled());

    List<String> list;
    if (isUsesList()) {
        list = getList(context);
    } else {
        list = new ArrayList<String>();
    }

    List<String> selectlist;
    BaseProperty prop = (BaseProperty) object.safeget(name);
    if (prop == null) {
        selectlist = new ArrayList<String>();
    } else {
        selectlist = getListFromString((String) prop.getValue());
    }

    // Add options from Set

    for (String value : selectlist) {
        if (!list.contains(value)) {
            list.add(value);
        }
    }

    // Make sure we have guest
    list.remove(XWikiRightService.GUEST_USER_FULLNAME);
    list.add(0, XWikiRightService.GUEST_USER_FULLNAME);

    // Sort the user list
    TreeMap<String, String> map = new TreeMap<String, String>();
    for (String value : list) {
        map.put(getText(value, context), value);
    }

    // Add options from Set
    for (Map.Entry<String, String> entry : map.entrySet()) {
        String display = entry.getKey();
        String value = entry.getValue();

        option option = new option(display, value);
        option.addElement(display);
        if (selectlist.contains(value)) {
            option.setSelected(true);
        }
        select.addElement(option);
    }

    buffer.append(select.toString());

    if (!isUsesList()) {
        input in = new input();
        in.setName(prefix + "newuser");
        in.setSize(15);
        in.setDisabled(isDisabled());
        buffer.append("<br />");
        buffer.append(in.toString());

        if (!isDisabled()) {
            button button = new button();
            button.setTagText("Add");

            button.setOnClick("addUser(this.form,'" + prefix + "'); return false;");
            buffer.append(button.toString());
        }
    }

    input in = new input();
    in.setType("hidden");
    in.setName(prefix + name);
    in.setDisabled(isDisabled());
    buffer.append(in.toString());
}