Example usage for java.util SortedMap firstKey

List of usage examples for java.util SortedMap firstKey

Introduction

In this page you can find the example usage for java.util SortedMap firstKey.

Prototype

K firstKey();

Source Link

Document

Returns the first (lowest) key currently in this map.

Usage

From source file:Main.java

public static void main(String args[]) {
    Calendar now = Calendar.getInstance();
    Locale locale = Locale.getDefault();

    Map<String, Integer> names = now.getDisplayNames(Calendar.DAY_OF_WEEK, Calendar.LONG, locale);
    SortedMap<String, Integer> nav = new TreeMap<String, Integer>(names);
    System.out.printf("Whole list:%n%s%n", nav);
    System.out.printf("First key: %s\tFirst entry: %s%n", nav.firstKey(), nav.firstEntry());
}

From source file:Main.java

public static void main(String[] args) {
    SortedMap<String, Integer> sortedMap = new TreeMap<String, Integer>();
    sortedMap.put("A", 1);
    sortedMap.put("B", 2);
    sortedMap.put("C", 3);
    sortedMap.put("D", 4);
    sortedMap.put("E", 5);
    sortedMap.put("java2s", 6);

    System.out.println(sortedMap.firstKey());

}

From source file:Main.java

public static void main(String[] args) {
    SortedMap<String, String> sMap = new TreeMap<>();
    sMap.put("CSS", "style");
    sMap.put("HTML", "mark up");
    sMap.put("Oracle", "database");
    sMap.put("XML", "data");

    SortedMap<String, String> subMap = sMap.subMap("CSS", "XML");
    System.out.println(subMap);//w  ww.j av  a  2s .c om

    // Get the first and last keys
    String firstKey = sMap.firstKey();
    String lastKey = sMap.lastKey();
    System.out.println("First Key:  " + firstKey);
    System.out.println("Last key:   " + lastKey);
}

From source file:Main.java

public static void main(String[] args) {
    Comparator<String> keyComparator = Comparator.comparing(String::length)
            .thenComparing(String::compareToIgnoreCase);

    SortedMap<String, String> sMap = new TreeMap<>(keyComparator);
    sMap.put("CSS", "style");
    sMap.put("HTML", "mark up");
    sMap.put("Oracle", "database");
    sMap.put("XML", "data");

    SortedMap<String, String> subMap = sMap.subMap("CSS", "XML");
    System.out.println(subMap);//from   w w w .j  a v a  2 s  .c  o  m

    // Get the first and last keys
    String firstKey = sMap.firstKey();
    String lastKey = sMap.lastKey();
    System.out.println("First Key:  " + firstKey);
    System.out.println("Last key:   " + lastKey);
}

From source file:Main.java

public static <T, U> U firstElement(SortedMap<T, U> m) {
    if (m == null || m.isEmpty()) {
        return null;
    }/*from  w w w .  ja v  a 2  s.c  om*/
    return m.get(m.firstKey());
}

From source file:com.cloudera.oryx.kmeans.common.Weighted.java

/**
 * Sample items from a {@code List<Weighted>} where items with higher weights
 * have a higher probability of being included in the sample.
 * //from w  w  w  .  j a  v  a2  s  .c  om
 * @param things The iterable to sample from
 * @param size The number of items to sample
 * @return A list containing the sampled items
 */
public static <T extends Weighted<?>> List<T> sample(Iterable<T> things, int size, RandomGenerator random) {
    if (random == null) {
        random = RandomManager.getRandom();
    }
    SortedMap<Double, T> sampled = Maps.newTreeMap();
    for (T thing : things) {
        if (thing.weight() > 0) {
            double score = Math.log(random.nextDouble()) / thing.weight();
            if (sampled.size() < size || score > sampled.firstKey()) {
                sampled.put(score, thing);
            }
            if (sampled.size() > size) {
                sampled.remove(sampled.firstKey());
            }
        }
    }
    return Lists.newArrayList(sampled.values());
}

From source file:org.kalypso.model.wspm.tuhh.schema.simulation.PolynomeProcessor.java

public static <S> S forStationAdjacent(final SortedMap<BigDecimal, S> stationIndex, final BigDecimal station,
        final boolean upstream) {
    final BigDecimal pred = NumberUtils.decrement(station);
    final BigDecimal succ = NumberUtils.increment(station);

    if (upstream) {
        final SortedMap<BigDecimal, ? extends Object> successors = stationIndex.tailMap(succ);
        if (!successors.isEmpty())
            return stationIndex.get(successors.firstKey());
    } else {// w w  w  . j  a v a 2s .com

        final SortedMap<BigDecimal, ? extends Object> predecessors = stationIndex.headMap(pred);
        if (!predecessors.isEmpty())
            return stationIndex.get(predecessors.lastKey());
    }

    return null;
}

From source file:org.orekit.models.earth.GeoMagneticFieldFactory.java

/** Gets a geomagnetic field model for the given year. In case the specified
 * year does not match an existing model epoch, the resulting field is
 * generated by either time-transforming an existing model using its secular
 * variation coefficients, or by linear interpolating two existing models.
 * @param type the type of the field (e.g. WMM or IGRF)
 * @param models all loaded field models, sorted by their epoch
 * @param year the epoch of the resulting field model
 * @return a {@link GeoMagneticField} model for the given year
 * @throws OrekitException if the specified year is out of range of the available models
 *///from w ww  .j a v a 2 s.  co m
private static GeoMagneticField getModel(final FieldModel type, final TreeMap<Integer, GeoMagneticField> models,
        final double year) throws OrekitException {

    final int epochKey = (int) (year * 100d);
    final SortedMap<Integer, GeoMagneticField> head = models.headMap(epochKey, true);

    if (head.isEmpty()) {
        throw new OrekitException(OrekitMessages.NON_EXISTENT_GEOMAGNETIC_MODEL, type.name(), year);
    }

    GeoMagneticField model = models.get(head.lastKey());
    if (model.getEpoch() < year) {
        if (model.supportsTimeTransform()) {
            model = model.transformModel(year);
        } else {
            final SortedMap<Integer, GeoMagneticField> tail = models.tailMap(epochKey, false);
            if (tail.isEmpty()) {
                throw new OrekitException(OrekitMessages.NON_EXISTENT_GEOMAGNETIC_MODEL, type.name(), year);
            }
            final GeoMagneticField secondModel = models.get(tail.firstKey());
            if (secondModel != model) {
                model = model.transformModel(secondModel, year);
            }
        }
    }
    return model;
}

From source file:arun.com.chromer.util.ColorUtil.java

@ColorInt
public static int getClosestAccentColor(@ColorInt int color) {
    final SortedMap<Double, Integer> set = new TreeMap<>();
    color = (0xFFFFFF - color) | 0xFF000000;
    for (int i = 0; i < ACCENT_COLORS_700.length; i++) {
        set.put(colorDifference(color, ACCENT_COLORS_700[i]), i);
    }//from w  w w . j av a 2 s. c  o  m
    return ACCENT_COLORS_700[set.get(set.firstKey())];
}

From source file:org.fede.util.Util.java

private static MoneyAmountSeries read(String name) {

    try (InputStream is = Util.class.getResourceAsStream("/" + name)) {

        final JSONSeries series = CONSULTATIO_SERIES.contains(name) ? readConsultatioSeries(is, OM)
                : OM.readValue(is, JSONSeries.class);

        final SortedMap<YearMonth, MoneyAmount> interpolatedData = new TreeMap<>();
        final String currency = series.getCurrency();
        for (JSONDataPoint dp : series.getData()) {
            if (interpolatedData.put(new YearMonth(dp.getYear(), dp.getMonth()),
                    new MoneyAmount(dp.getValue(), currency)) != null) {
                throw new IllegalArgumentException("Series " + name + " has two values for year " + dp.getYear()
                        + " and month " + dp.getMonth());
            }/*from w  w w. j  a  va  2s  .  co  m*/
        }

        final InterpolationStrategy strategy = InterpolationStrategy.valueOf(series.getInterpolation());

        YearMonth ym = interpolatedData.firstKey();
        final YearMonth last = interpolatedData.lastKey();
        while (ym.monthsUntil(last) > 0) {
            YearMonth next = ym.next();
            if (!interpolatedData.containsKey(next)) {
                interpolatedData.put(next, strategy.interpolate(interpolatedData.get(ym), ym, currency));
            }
            ym = ym.next();
        }
        return new SortedMapMoneyAmountSeries(currency, interpolatedData);

    } catch (IOException ioEx) {
        throw new IllegalArgumentException("Could not read series named " + name, ioEx);
    }

}