Example usage for java.util TreeMap size

List of usage examples for java.util TreeMap size

Introduction

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

Prototype

int size

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

Click Source Link

Document

The number of entries in the tree

Usage

From source file:cc.slda.DisplayTopic.java

@SuppressWarnings("unchecked")
public int run(String[] args) throws Exception {
    Options options = new Options();

    options.addOption(Settings.HELP_OPTION, false, "print the help message");
    options.addOption(OptionBuilder.withArgName(Settings.PATH_INDICATOR).hasArg()
            .withDescription("input beta file").create(Settings.INPUT_OPTION));
    options.addOption(OptionBuilder.withArgName(Settings.PATH_INDICATOR).hasArg()
            .withDescription("term index file").create(ParseCorpus.INDEX));
    options.addOption(OptionBuilder.withArgName(Settings.INTEGER_INDICATOR).hasArg()
            .withDescription("display top terms only (default - 10)").create(TOP_DISPLAY_OPTION));

    String betaString = null;//ww  w .  j  av  a 2s.  co  m
    String indexString = null;
    int topDisplay = TOP_DISPLAY;

    CommandLineParser parser = new GnuParser();
    HelpFormatter formatter = new HelpFormatter();
    try {
        CommandLine line = parser.parse(options, args);

        if (line.hasOption(Settings.HELP_OPTION)) {
            formatter.printHelp(ParseCorpus.class.getName(), options);
            System.exit(0);
        }

        if (line.hasOption(Settings.INPUT_OPTION)) {
            betaString = line.getOptionValue(Settings.INPUT_OPTION);
        } else {
            throw new ParseException("Parsing failed due to " + Settings.INPUT_OPTION + " not initialized...");
        }

        if (line.hasOption(ParseCorpus.INDEX)) {
            indexString = line.getOptionValue(ParseCorpus.INDEX);
        } else {
            throw new ParseException("Parsing failed due to " + ParseCorpus.INDEX + " not initialized...");
        }

        if (line.hasOption(TOP_DISPLAY_OPTION)) {
            topDisplay = Integer.parseInt(line.getOptionValue(TOP_DISPLAY_OPTION));
        }
    } catch (ParseException pe) {
        System.err.println(pe.getMessage());
        formatter.printHelp(ParseCorpus.class.getName(), options);
        System.exit(0);
    } catch (NumberFormatException nfe) {
        System.err.println(nfe.getMessage());
        System.exit(0);
    }

    JobConf conf = new JobConf(DisplayTopic.class);
    FileSystem fs = FileSystem.get(conf);

    Path indexPath = new Path(indexString);
    Preconditions.checkArgument(fs.exists(indexPath) && fs.isFile(indexPath), "Invalid index path...");

    Path betaPath = new Path(betaString);
    Preconditions.checkArgument(fs.exists(betaPath) && fs.isFile(betaPath), "Invalid beta path...");

    SequenceFile.Reader sequenceFileReader = null;
    try {
        IntWritable intWritable = new IntWritable();
        Text text = new Text();
        Map<Integer, String> termIndex = new HashMap<Integer, String>();
        sequenceFileReader = new SequenceFile.Reader(fs, indexPath, conf);
        while (sequenceFileReader.next(intWritable, text)) {
            termIndex.put(intWritable.get(), text.toString());
        }

        PairOfIntFloat pairOfIntFloat = new PairOfIntFloat();
        // HMapIFW hmap = new HMapIFW();
        HMapIDW hmap = new HMapIDW();
        TreeMap<Double, Integer> treeMap = new TreeMap<Double, Integer>();
        sequenceFileReader = new SequenceFile.Reader(fs, betaPath, conf);
        while (sequenceFileReader.next(pairOfIntFloat, hmap)) {
            treeMap.clear();

            System.out.println("==============================");
            System.out.println(
                    "Top ranked " + topDisplay + " terms for Topic " + pairOfIntFloat.getLeftElement());
            System.out.println("==============================");

            Iterator<Integer> itr1 = hmap.keySet().iterator();
            int temp1 = 0;
            while (itr1.hasNext()) {
                temp1 = itr1.next();
                treeMap.put(-hmap.get(temp1), temp1);
                if (treeMap.size() > topDisplay) {
                    treeMap.remove(treeMap.lastKey());
                }
            }

            Iterator<Double> itr2 = treeMap.keySet().iterator();
            double temp2 = 0;
            while (itr2.hasNext()) {
                temp2 = itr2.next();
                if (termIndex.containsKey(treeMap.get(temp2))) {
                    System.out.println(termIndex.get(treeMap.get(temp2)) + "\t\t" + -temp2);
                } else {
                    System.out.println("How embarrassing! Term index not found...");
                }
            }
        }
    } finally {
        IOUtils.closeStream(sequenceFileReader);
    }

    return 0;
}

From source file:com.sfs.whichdoctor.export.writer.AgedDebtorsAnalysisWriter.java

/**
 * Gets the formatted period breakdown field.
 *
 * @param periods the periods// ww  w .  j  a v  a2 s.  c  om
 * @param group the group
 * @param format the format
 * @return the formatted period breakdown field
 */
private String getFormattedPeriodBreakdownField(final TreeMap<Integer, AgedDebtorsPeriod> periods,
        final AgedDebtorsBreakdown breakdown, final String format) {

    StringBuffer field = new StringBuffer();

    int i = 1;
    for (int id : periods.keySet()) {
        AgedDebtorsPeriod period = periods.get(id);
        AgedDebtorsPeriod bPeriod = breakdown.getPeriodBreakdown(period);

        if (StringUtils.equalsIgnoreCase(format, "html")) {
            field.append("<div style=\"text-align: right\">");
        }
        field.append(Formatter.toCurrency(bPeriod.getTotal(), "$"));
        if (StringUtils.equalsIgnoreCase(format, "html")) {
            field.append("</div>");
        }
        if (i < periods.size()) {
            field.append(this.getKeys().getString("ITEM_SUFFIX"));
            field.append(this.getKeys().getString("ITEM_DIVIDER"));
            field.append(this.getKeys().getString("ITEM_PREFIX"));
        }
        i++;
    }
    return field.toString();
}

From source file:streaming.core.WindowOperation.java

@Override
public Object[][] process(Object[] event) {
    long day = (Long) event[0];
    String word = (String) event[1];
    long freqs = (Long) event[2];

    TreeMap<Long, Long> sortedFreq = map.get(word);
    if (sortedFreq == null) {
        sortedFreq = new TreeMap<Long, Long>();
        map.put(word, sortedFreq);/*from w  w w. j a v  a  2  s. com*/
    }

    Long t = sortedFreq.get(day);
    if (t != null) {
        freqs = freqs + t;
    }
    sortedFreq.put(day, freqs);

    Iterator<Entry<Long, Long>> iterator = sortedFreq.headMap(1 + day - numberOfDays).entrySet().iterator();
    while (iterator.hasNext()) {
        iterator.next();
        iterator.remove();
    }

    DescriptiveStatistics stats = new DescriptiveStatistics();
    long dayIndex = 1 + day - numberOfDays;
    for (Entry<Long, Long> e : sortedFreq.entrySet()) {
        while (e.getKey() > dayIndex) {
            dayIndex++;
            stats.addValue(0);
        }
        stats.addValue(e.getValue());
    }

    if (sortedFreq.size() > numberOfDays) {
        System.out.println(day + " size=" + sortedFreq.size() + " " + sortedFreq);
    }

    double mean = stats.getMean();
    double meadian = stats.getPercentile(50);
    mean = (mean == 0) ? 1 : mean;
    meadian = (meadian == 0) ? 1 : meadian;
    double stddev = stats.getStandardDeviation();
    stddev = (stddev == 0) ? 1 : stddev;
    double cov = stddev / mean;

    //double swna = Math.log(freqs)*freqs/stats.getMean();
    double swna1 = Math.log(meadian) * Math.abs(freqs - meadian) / stddev;
    if (Double.isNaN(swna1)) {
        System.out.println();
    }
    double swna2 = Math.abs(freqs - meadian) / stddev;
    double swna3 = freqs / (meadian * cov);

    Gaussian gaussian = new Gaussian(100, 50);

    double swna4 = (0.1 + 100 * gaussian.value(meadian)) * freqs / (meadian * cov);

    int percentageAvialableValues = Math.round(100 * sortedFreq.size() / numberOfDays);
    //System.out.println("#"+ word + " " + freqs + " "+ stats.getMean() + Arrays.toString(stats.getValues()));
    return new Object[][] { { day, word, swna1, freqs, stats.getMean(), meadian, stddev, swna2, swna3, swna4,
            cov, percentageAvialableValues } };

    //      if(freqs > 3 && swna> 5){
    //         return new Object[][]{{day, word, swna}};   
    //      }else{
    //         return null;
    //      }

}

From source file:com.irccloud.android.data.EventsDataSource.java

public void pruneEvents(int bid) {
    synchronized (events) {
        TreeMap<Long, Event> e = events.get(bid);
        while (e != null && e.size() > 50 && e.firstKey() != null) {
            e.remove(e.firstKey());/*  w  w w  .j  av a  2s . c o m*/
        }
    }
}

From source file:com.hichinaschool.flashcards.anki.Preferences.java

private void initializeLanguageDialog() {
    TreeMap<String, String> items = new TreeMap<String, String>();
    for (String localeCode : mAppLanguages) {
        Locale loc;//from  ww w . j  a v a  2s  .  co  m
        if (localeCode.length() > 2) {
            loc = new Locale(localeCode.substring(0, 2), localeCode.substring(3, 5));
        } else {
            loc = new Locale(localeCode);
        }
        items.put(loc.getDisplayName(), loc.toString());
    }
    mLanguageDialogLabels = new CharSequence[items.size() + 1];
    mLanguageDialogValues = new CharSequence[items.size() + 1];
    mLanguageDialogLabels[0] = getResources().getString(R.string.language_system);
    mLanguageDialogValues[0] = "";
    int i = 1;
    for (Map.Entry<String, String> e : items.entrySet()) {
        mLanguageDialogLabels[i] = e.getKey();
        mLanguageDialogValues[i] = e.getValue();
        i++;
    }
    mLanguageSelection = (ListPreference) getPreferenceScreen().findPreference("language");
    mLanguageSelection.setEntries(mLanguageDialogLabels);
    mLanguageSelection.setEntryValues(mLanguageDialogValues);
}

From source file:org.powertac.common.config.ConfiguratorTest.java

@Test
public void testBootstrapInstanceOrig() {
    final TreeMap<String, Object> map = new TreeMap<String, Object>();
    ConfigurationRecorder cr = new ConfigurationRecorder() {
        @Override//from   ww w.  j a v  a 2s  .  c  o  m
        public void recordItem(String key, Object value) {
            map.put(key, value);
        }
    };
    ConfigInstance ci1 = new ConfigInstance("a1");
    ci1.sequence = 3;
    ci1.simpleProp = 21;
    ci1.stateProp = -3;
    ConfigInstance ci2 = new ConfigInstance("b1");
    ci2.sequence = 4;
    ci2.simpleProp = 31;
    ci2.stateProp = -13;
    List<ConfigInstance> instances = Arrays.asList(ci1, ci2);
    Configurator uut = new Configurator();
    uut.gatherBootstrapState(instances, cr);
    assertEquals(6, map.size(), "six entries");
    assertEquals(-3, map.get("common.config.configInstance.a1.stateProp"), "a1.stateProp");
}

From source file:com.act.biointerpretation.mechanisminspection.MechanisticValidator.java

private Reaction runEROsOnReaction(Reaction rxn, Long newId) throws IOException {
    projector.clearInchiCache(); // Mew reaction probably doesn't have repeat chemicals, and we don't want a huge cache
    // Apply the EROs and save the results in the reaction object.
    TreeMap<Integer, List<Ero>> scoreToListOfRos;
    try {//  ww w . j  av  a  2 s  .c om
        /* api.writeToOutKnowledgeGraph doesn't update the id of the written reaction, so we have to pass it as a
         * separate parameter. :(  I would fix the MongoDB behavior, but don't know what that might break!!! */
        scoreToListOfRos = findBestRosThatCorrectlyComputeTheReaction(rxn, newId);
    } catch (IOException e) {
        // Log some information about the culprit when validation fails.
        LOGGER.error("Caught IOException when applying ROs to rxn %d): %s", newId, e.getMessage());
        throw e;
    }

    if (scoreToListOfRos != null && scoreToListOfRos.size() > 0) {
        JSONObject matchingEros = new JSONObject();
        for (Map.Entry<Integer, List<Ero>> entry : scoreToListOfRos.entrySet()) {
            for (Ero e : entry.getValue()) {
                matchingEros.put(e.getId().toString(), entry.getKey().toString());
            }
        }
        rxn.setMechanisticValidatorResult(matchingEros);
        eroHitCounter++;
    }

    return rxn;
}

From source file:com.gsma.rcs.ri.sharing.SharingListView.java

/**
 * Sets the providers//w  w w  .  j  a  v a  2  s  .  c  om
 *
 * @param providers ordered map of providers IDs associated with their name
 */
private void setProviders(TreeMap<Integer, String> providers) {
    mProviderIds = new ArrayList<>();
    mFilterMenuItems = new ArrayList<>();
    for (Entry<Integer, String> entry : providers.entrySet()) {
        mFilterMenuItems.add(entry.getValue());
        mProviderIds.add(entry.getKey());
    }
    /* Upon setting, all providers are checked True */
    mCheckedProviders = new boolean[providers.size()];
    for (int i = 0; i < mCheckedProviders.length; i++) {
        mCheckedProviders[i] = true;
    }
}

From source file:org.apache.hadoop.hbase.extended.loadbalance.strategies.hotspot.HotSpotLoadBalancer.java

private boolean isHotSpot(TreeMap<HotSpotRegionLoad, HRegionInfo> regionLoadMap,
        List<HotSpotRegionLoad> exisitingRegionLoadList, double totalLoad) {
    // iterate in order on HotSpotRegionLoad
    boolean isHotSpot = false;
    double loadTillNow = 0.0;
    HotSpotRegionLoad load = null;/*from ww  w .  jav a 2 s.  c  o  m*/
    List<HotSpotRegionLoad> listToMarkHotSpot = new ArrayList<HotSpotRegionLoad>();
    int counter = 0;
    double hotspotLoad = totalLoad * hotspotLoadPercentThreshold;
    LOG.debug("#################isHotSpot: hotspotLoad=" + hotspotLoad + " totalLoad= " + totalLoad
            + " hotspotLoadPercentThreshold = " + hotspotLoadPercentThreshold);
    int hotspotNumber = (int) Math.ceil(regionLoadMap.size() * hotspotLoadNumberRegionsThreshold);

    LOG.debug("#################isHotSpot: hotspotNumber=" + hotspotNumber + " regionLoadMap.size()= "
            + regionLoadMap.size() + " hotspotLoadNumberRegionsThreshold = "
            + hotspotLoadNumberRegionsThreshold);
    for (Map.Entry<HotSpotRegionLoad, HRegionInfo> regionLoadItem : regionLoadMap.entrySet()) {
        load = regionLoadItem.getKey();
        loadTillNow += load.getLoad();
        counter++;
        LOG.debug(String.format(
                "#################isHotSpot: load = %s;loadTillNow=%s; hotspotLoad=%s; counter=%s ", load,
                loadTillNow, hotspotLoad, counter));
        if (loadTillNow >= hotspotLoad) {
            LOG.debug(String.format("#################isHotSpot: counter = %s;hotspotNumber=%s; ", counter,
                    hotspotNumber));

            if (counter <= hotspotNumber) {
                // hotspot reached
                listToMarkHotSpot.add(load);
                isHotSpot = true;
                break;
            } else {
                break;
            }
        } else {
            LOG.debug(String.format("#################isHotSpot: Adding load = %s into potential hotspot list",
                    load));
            // potentially hotspot
            listToMarkHotSpot.add(load);
        }
    }
    LOG.debug(String.format(
            "#################isHotSpot: isHotSpot =%s ; ;;listToMarkHotSpot = %s;exisitingRegionLoadList=%s ",
            isHotSpot, listToMarkHotSpot, exisitingRegionLoadList));

    if (isHotSpot) {
        // need to mark the list as true.
        for (HotSpotRegionLoad item : listToMarkHotSpot) {
            int itemIndexIfExists = exisitingRegionLoadList.indexOf(item);
            LOG.debug(String.format(
                    "#################isHotSpot: Item =%s is in the exitisting list at index =%s ", item,
                    itemIndexIfExists));
            if (itemIndexIfExists >= 0) {
                exisitingRegionLoadList.get(itemIndexIfExists).setRegionHotspot(true);
            }
        }
    }

    LOG.debug("#################isHotSpot: listToMarkHotSpot List=" + listToMarkHotSpot);
    return isHotSpot;
}

From source file:org.apache.hadoop.hbase.util.RegionSplitter.java

static void rollingSplit(String tableName, SplitAlgorithm splitAlgo, Configuration conf)
        throws IOException, InterruptedException {
    final int minOS = conf.getInt("split.outstanding", 2);

    HTable table = new HTable(conf, tableName);

    // max outstanding splits. default == 50% of servers
    final int MAX_OUTSTANDING = Math.max(table.getConnection().getCurrentNrHRS() / 2, minOS);

    Path hbDir = FSUtils.getRootDir(conf);
    Path tableDir = FSUtils.getTableDir(hbDir, table.getName());
    Path splitFile = new Path(tableDir, "_balancedSplit");
    FileSystem fs = FileSystem.get(conf);

    // get a list of daughter regions to create
    LinkedList<Pair<byte[], byte[]>> tmpRegionSet = getSplits(table, splitAlgo);
    LinkedList<Pair<byte[], byte[]>> outstanding = Lists.newLinkedList();
    int splitCount = 0;
    final int origCount = tmpRegionSet.size();

    // all splits must compact & we have 1 compact thread, so 2 split
    // requests to the same RS can stall the outstanding split queue.
    // To fix, group the regions into an RS pool and round-robin through it
    LOG.debug("Bucketing regions by regionserver...");
    TreeMap<String, LinkedList<Pair<byte[], byte[]>>> daughterRegions = Maps.newTreeMap();
    for (Pair<byte[], byte[]> dr : tmpRegionSet) {
        String rsLocation = table.getRegionLocation(dr.getSecond()).getHostnamePort();
        if (!daughterRegions.containsKey(rsLocation)) {
            LinkedList<Pair<byte[], byte[]>> entry = Lists.newLinkedList();
            daughterRegions.put(rsLocation, entry);
        }//from   w w  w .j  a v  a  2 s. com
        daughterRegions.get(rsLocation).add(dr);
    }
    LOG.debug("Done with bucketing.  Split time!");
    long startTime = System.currentTimeMillis();

    // open the split file and modify it as splits finish
    FSDataInputStream tmpIn = fs.open(splitFile);
    byte[] rawData = new byte[tmpIn.available()];
    tmpIn.readFully(rawData);
    tmpIn.close();
    FSDataOutputStream splitOut = fs.create(splitFile);
    splitOut.write(rawData);

    try {
        // *** split code ***
        while (!daughterRegions.isEmpty()) {
            LOG.debug(daughterRegions.size() + " RS have regions to splt.");

            // Get RegionServer : region count mapping
            final TreeMap<ServerName, Integer> rsSizes = Maps.newTreeMap();
            Map<HRegionInfo, ServerName> regionsInfo = table.getRegionLocations();
            for (ServerName rs : regionsInfo.values()) {
                if (rsSizes.containsKey(rs)) {
                    rsSizes.put(rs, rsSizes.get(rs) + 1);
                } else {
                    rsSizes.put(rs, 1);
                }
            }

            // sort the RS by the number of regions they have
            List<String> serversLeft = Lists.newArrayList(daughterRegions.keySet());
            Collections.sort(serversLeft, new Comparator<String>() {
                public int compare(String o1, String o2) {
                    return rsSizes.get(o1).compareTo(rsSizes.get(o2));
                }
            });

            // round-robin through the RS list. Choose the lightest-loaded servers
            // first to keep the master from load-balancing regions as we split.
            for (String rsLoc : serversLeft) {
                Pair<byte[], byte[]> dr = null;

                // find a region in the RS list that hasn't been moved
                LOG.debug("Finding a region on " + rsLoc);
                LinkedList<Pair<byte[], byte[]>> regionList = daughterRegions.get(rsLoc);
                while (!regionList.isEmpty()) {
                    dr = regionList.pop();

                    // get current region info
                    byte[] split = dr.getSecond();
                    HRegionLocation regionLoc = table.getRegionLocation(split);

                    // if this region moved locations
                    String newRs = regionLoc.getHostnamePort();
                    if (newRs.compareTo(rsLoc) != 0) {
                        LOG.debug("Region with " + splitAlgo.rowToStr(split) + " moved to " + newRs
                                + ". Relocating...");
                        // relocate it, don't use it right now
                        if (!daughterRegions.containsKey(newRs)) {
                            LinkedList<Pair<byte[], byte[]>> entry = Lists.newLinkedList();
                            daughterRegions.put(newRs, entry);
                        }
                        daughterRegions.get(newRs).add(dr);
                        dr = null;
                        continue;
                    }

                    // make sure this region wasn't already split
                    byte[] sk = regionLoc.getRegionInfo().getStartKey();
                    if (sk.length != 0) {
                        if (Bytes.equals(split, sk)) {
                            LOG.debug("Region already split on " + splitAlgo.rowToStr(split)
                                    + ".  Skipping this region...");
                            ++splitCount;
                            dr = null;
                            continue;
                        }
                        byte[] start = dr.getFirst();
                        Preconditions.checkArgument(Bytes.equals(start, sk),
                                splitAlgo.rowToStr(start) + " != " + splitAlgo.rowToStr(sk));
                    }

                    // passed all checks! found a good region
                    break;
                }
                if (regionList.isEmpty()) {
                    daughterRegions.remove(rsLoc);
                }
                if (dr == null)
                    continue;

                // we have a good region, time to split!
                byte[] split = dr.getSecond();
                LOG.debug("Splitting at " + splitAlgo.rowToStr(split));
                HBaseAdmin admin = new HBaseAdmin(table.getConfiguration());
                admin.split(table.getTableName(), split);

                LinkedList<Pair<byte[], byte[]>> finished = Lists.newLinkedList();
                if (conf.getBoolean("split.verify", true)) {
                    // we need to verify and rate-limit our splits
                    outstanding.addLast(dr);
                    // with too many outstanding splits, wait for some to finish
                    while (outstanding.size() >= MAX_OUTSTANDING) {
                        finished = splitScan(outstanding, table, splitAlgo);
                        if (finished.isEmpty()) {
                            Thread.sleep(30 * 1000);
                        } else {
                            outstanding.removeAll(finished);
                        }
                    }
                } else {
                    finished.add(dr);
                }

                // mark each finished region as successfully split.
                for (Pair<byte[], byte[]> region : finished) {
                    splitOut.writeChars("- " + splitAlgo.rowToStr(region.getFirst()) + " "
                            + splitAlgo.rowToStr(region.getSecond()) + "\n");
                    splitCount++;
                    if (splitCount % 10 == 0) {
                        long tDiff = (System.currentTimeMillis() - startTime) / splitCount;
                        LOG.debug("STATUS UPDATE: " + splitCount + " / " + origCount + ". Avg Time / Split = "
                                + org.apache.hadoop.util.StringUtils.formatTime(tDiff));
                    }
                }
            }
        }
        if (conf.getBoolean("split.verify", true)) {
            while (!outstanding.isEmpty()) {
                LinkedList<Pair<byte[], byte[]>> finished = splitScan(outstanding, table, splitAlgo);
                if (finished.isEmpty()) {
                    Thread.sleep(30 * 1000);
                } else {
                    outstanding.removeAll(finished);
                    for (Pair<byte[], byte[]> region : finished) {
                        splitOut.writeChars("- " + splitAlgo.rowToStr(region.getFirst()) + " "
                                + splitAlgo.rowToStr(region.getSecond()) + "\n");
                    }
                }
            }
        }
        LOG.debug("All regions have been successfully split!");
    } finally {
        long tDiff = System.currentTimeMillis() - startTime;
        LOG.debug("TOTAL TIME = " + org.apache.hadoop.util.StringUtils.formatTime(tDiff));
        LOG.debug("Splits = " + splitCount);
        LOG.debug("Avg Time / Split = " + org.apache.hadoop.util.StringUtils.formatTime(tDiff / splitCount));

        splitOut.close();
        if (table != null) {
            table.close();
        }
    }
    fs.delete(splitFile, false);
}