Example usage for java.util TreeMap higherKey

List of usage examples for java.util TreeMap higherKey

Introduction

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

Prototype

public K higherKey(K key) 

Source Link

Usage

From source file:Main.java

public static void main(String[] args) {

    TreeMap<Integer, String> treemap = new TreeMap<Integer, String>();

    // populating tree map
    treemap.put(2, "two");
    treemap.put(1, "one");
    treemap.put(3, "three");
    treemap.put(6, "six");
    treemap.put(5, "from java2s.com");

    // getting higher key for key 4          
    System.out.println("Checking values of the map");
    System.out.println("Value is: " + treemap.higherKey(3));
}

From source file:Main.java

public static void main(String[] args) {
    TreeMap<Integer, Product> db = new TreeMap<Integer, Product>();
    db.put(1000, new Product("D", 350));
    db.put(1011, new Product("p", 15.75));
    db.put(1102, new Product("M", 8.50));
    db.put(2023, new Product("A", 150));
    db.put(2034, new Product("T", 9.99));

    System.out.println(db.subMap(1000, 1999) + "\n");

    System.out.println(db.tailMap(1011) + "\n");

    System.out.println(db.headMap(2023));

    System.out.println("First key higher than 2034: " + db.higherKey(2034));
    System.out.println("First key lower than 2034: " + db.lowerKey(2034));
}

From source file:org.eclipse.dataset.Stats.java

public static double[] outlierValuesMap(final Dataset a, int nl, int nh) {
    final TreeMap<Double, Integer> lMap = new TreeMap<Double, Integer>();
    final TreeMap<Double, Integer> hMap = new TreeMap<Double, Integer>();

    int ml = 0;//w w w .  j av  a  2 s.  co m
    int mh = 0;
    IndexIterator it = a.getIterator();
    while (it.hasNext()) {
        Double x = a.getElementDoubleAbs(it.index);
        Integer i;
        if (ml == nl) {
            Double k = lMap.lastKey();
            if (x < k) {
                i = lMap.get(k) - 1;
                if (i == 0) {
                    lMap.remove(k);
                } else {
                    lMap.put(k, i);
                }
                i = lMap.get(x);
                if (i == null) {
                    lMap.put(x, 1);
                } else {
                    lMap.put(x, i + 1);
                }
            }
        } else {
            i = lMap.get(x);
            if (i == null) {
                lMap.put(x, 1);
            } else {
                lMap.put(x, i + 1);
            }
            ml++;
        }

        if (mh == nh) {
            Double k = hMap.firstKey();
            if (x > k) {
                i = hMap.get(k) - 1;
                if (i == 0) {
                    hMap.remove(k);
                } else {
                    hMap.put(k, i);
                }
                i = hMap.get(x);
                if (i == null) {
                    hMap.put(x, 1);
                } else {
                    hMap.put(x, i + 1);
                }
            }
        } else {
            i = hMap.get(x);
            if (i == null) {
                hMap.put(x, 1);
            } else {
                hMap.put(x, i + 1);
            }
            mh++;
        }
    }

    // Attempt to make values distinct
    double lx = lMap.lastKey();
    double hx = hMap.firstKey();
    if (lx >= hx) {
        Double h = hMap.higherKey(lx);
        if (h != null) {
            hx = h;
            mh--;
        } else {
            Double l = lMap.lowerKey(hx);
            if (l != null) {
                lx = l;
                ml--;
            }
        }

    }
    return new double[] { lMap.lastKey(), hMap.firstKey(), ml, mh };
}

From source file:org.eclipse.january.dataset.Stats.java

static double[] outlierValuesMap(final Dataset a, int nl, int nh) {
    final TreeMap<Double, Integer> lMap = new TreeMap<Double, Integer>();
    final TreeMap<Double, Integer> hMap = new TreeMap<Double, Integer>();

    int ml = 0;/*from w  w w. jav a 2 s  .c  om*/
    int mh = 0;
    IndexIterator it = a.getIterator();
    while (it.hasNext()) {
        Double x = a.getElementDoubleAbs(it.index);
        Integer i;
        if (ml == nl) {
            Double k = lMap.lastKey();
            if (x < k) {
                i = lMap.get(k) - 1;
                if (i == 0) {
                    lMap.remove(k);
                } else {
                    lMap.put(k, i);
                }
                i = lMap.get(x);
                if (i == null) {
                    lMap.put(x, 1);
                } else {
                    lMap.put(x, i + 1);
                }
            }
        } else {
            i = lMap.get(x);
            if (i == null) {
                lMap.put(x, 1);
            } else {
                lMap.put(x, i + 1);
            }
            ml++;
        }

        if (mh == nh) {
            Double k = hMap.firstKey();
            if (x > k) {
                i = hMap.get(k) - 1;
                if (i == 0) {
                    hMap.remove(k);
                } else {
                    hMap.put(k, i);
                }
                i = hMap.get(x);
                if (i == null) {
                    hMap.put(x, 1);
                } else {
                    hMap.put(x, i + 1);
                }
            }
        } else {
            i = hMap.get(x);
            if (i == null) {
                hMap.put(x, 1);
            } else {
                hMap.put(x, i + 1);
            }
            mh++;
        }
    }

    // Attempt to make values distinct
    double lx = lMap.lastKey();
    double hx = hMap.firstKey();
    if (lx >= hx) {
        Double h = hMap.higherKey(lx);
        if (h != null) {
            hx = h;
            mh--;
        } else {
            Double l = lMap.lowerKey(hx);
            if (l != null) {
                lx = l;
                ml--;
            }
        }

    }
    return new double[] { lMap.lastKey(), hMap.firstKey(), ml, mh };
}

From source file:org.scantegrity.IRVTally.IRVTally.java

/**
 * Creates a Results File.//from   ww  w. ja  v  a2s. c  o m
 * @param p_contest the contest to use
 * @param p_ballots the ballot store
 * @throws IOException
 */
public static void createResults(Vector<Contest> p_contest, TreeMap<Integer, Vector<ContestChoice>> p_ballots,
        String p_outFile) throws IOException {

    FileWriter l_file = new FileWriter(p_outFile);
    BufferedWriter l_out = new BufferedWriter(l_file);

    Integer l_key = p_ballots.firstKey();
    while (l_key != null) {
        System.out.println("Saving Contest " + l_key + "...");

        //Get the contest
        Contest l_c = p_contest.get(0);
        for (Contest l_con : p_contest) {
            if (l_con.getId() == l_key) {
                l_c = l_con;
                break;
            }
        }

        l_out.write("CONTEST - " + l_c.getContestName());
        l_out.newLine();
        Vector<ContestChoice> l_choices = p_ballots.get(l_key);
        InstantRunoffTally l_tally = new InstantRunoffTally();
        IRVContestResult l_x = (IRVContestResult) l_tally.tally(l_c, l_choices);
        l_out.write(l_x.toString());

        l_out.newLine();
        l_key = p_ballots.higherKey(l_key);
    }

    l_out.close();
}

From source file:org.scantegrity.IRVTally.IRVTally.java

/**
 * Fills in default strings if contest information isn't provided
 * @param p_results//from  w ww.j a va2s  .c o m
 * @return
 */
public static Vector<Contest> getContestDefaults(TreeMap<Integer, Vector<ContestChoice>> p_results) {
    Vector<Contest> l_res = new Vector<Contest>();
    Integer l_key = p_results.firstKey();
    while (l_key != null) {
        Vector<Contestant> l_cons = new Vector<Contestant>();
        int l_maxd = 0;
        for (ContestChoice l_b : p_results.get(l_key)) {
            int[][] l_d = l_b.getChoices();
            for (int i = 0; i < l_d.length; i++) {
                for (int l_j : l_d[i])
                    l_maxd = Math.max(l_j, l_maxd);
            }
        }
        for (int l_i = 0; l_i <= l_maxd; l_i++) {
            //System.err.println("Adding Contestant " + l_i);
            l_cons.add(new Contestant(l_i, "Contestant " + l_i));
        }

        Contest l_c = new Contest();
        l_c.setId(l_key);
        l_c.setContestName("Contest " + l_key);
        l_c.setContestants(l_cons);
        l_res.add(l_c);
        l_key = p_results.higherKey(l_key);
    }

    return l_res;
}

From source file:org.scantegrity.openstv.m32ostv.java

public static void main(String args[]) {
    TreeMap<Integer, Vector<Ballot>> l_results = null;

    setOptions();/*from w  ww .j a v a  2 s  .  c o  m*/

    String l_args[] = null;
    CommandLine l_cmdLine = null;
    try {
        CommandLineParser l_parser = new PosixParser();
        l_cmdLine = l_parser.parse(c_opts, args);
        l_args = l_cmdLine.getArgs();
    } catch (ParseException l_e) {
        l_e.printStackTrace();
        return;
    }

    if (l_cmdLine == null || l_cmdLine.hasOption("help") || l_args == null || l_args.length < 1
            || l_args.length > 2) {
        printUsage();
        return;
    }

    //Looks like we have valid arguments, try to read M3
    try {
        l_results = ParseMeetingThree(l_args[0]);
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
        return;
    } catch (SAXException e) {
        e.printStackTrace();
        return;
    } catch (IOException e) {
        System.out.println("Could not read '" + l_args[0] + "'");
        return;
    }

    //Get contest information, if possible.
    Vector<Contest> l_c = null;
    if (l_cmdLine.hasOption("info")) {
        System.out.println("Contest Information");
        try {
            l_c = loadContest(l_cmdLine.getOptionValue("info"));
            //validateContest(l_results, l_c);
        } catch (Exception l_e) {
            l_e.printStackTrace();
            l_c = null;
        }
    }

    if (l_c == null) {
        //Load defaults
        System.out.print("Contest information is missing! ");
        System.out.print("Generating default contest information...");
        l_c = getContestDefaults(l_results);
        System.out.println("done");
    }

    //Convert results to BLT format.
    Integer l_key = l_results.firstKey();
    while (l_key != null) {
        /*
        for (Ballot l_b : l_results.get(l_key))
        {
           System.out.println(l_b.getId());
           System.out.println(java.util.Arrays.deepToString(l_b.getBallotData().get(0)));
                   
        }*/
        //Find the corresponding contest
        Contest l_contest = null;
        for (Contest l_con : l_c)
            if (l_con.getId().equals(l_key)) {
                l_contest = l_con;
                break;
            }
        if (l_contest == null) {
            System.out.println("Contest " + l_key + " could not be found...");
            continue;
        }
        //Get the ballots and Print
        try {
            createBLT(l_contest, l_results.get(l_key));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        l_key = l_results.higherKey(l_key);
    }

}

From source file:org.scantegrity.openstv.m32ostv.java

/**
 * Fills in default strings if contest information isn't provided
 * @param p_results/*from  w  w w  .  j  ava2  s .c om*/
 * @return
 */
public static Vector<Contest> getContestDefaults(TreeMap<Integer, Vector<Ballot>> p_results) {
    Vector<Contest> l_res = new Vector<Contest>();
    Integer l_key = p_results.firstKey();
    while (l_key != null) {
        Ballot l_b = p_results.get(l_key).get(0);
        Integer[][] l_d = l_b.getContestData(l_key);
        Vector<Contestant> l_cons = new Vector<Contestant>();
        for (int i = 0; i < l_d.length; i++) {
            l_cons.add(new Contestant(i, "Contestant " + i));
        }

        Contest l_c = new Contest();
        l_c.setId(l_key);
        l_c.setContestName("Contest " + l_key);
        l_c.setContestants(l_cons);
        l_res.add(l_c);
        l_key = p_results.higherKey(l_key);
    }

    return l_res;
}

From source file:org.wso2.carbon.governance.registry.extensions.executors.ServiceVersionExecutor.java

private String reformatPath(String path, String currentExpression, String targetExpression,
        String newResourceVersion) throws RegistryException {
    TreeMap<Integer, String> indexMap = new TreeMap<Integer, String>();

    String returnPath = targetExpression;
    String prefix;/*from w  ww.ja va  2s  . c om*/

    if (currentExpression.equals(targetExpression)) {
        return path;
    }
    indexMap.put(currentExpression.indexOf(RESOURCE_NAME), RESOURCE_NAME);
    indexMap.put(currentExpression.indexOf(RESOURCE_PATH), RESOURCE_PATH);
    indexMap.put(currentExpression.indexOf(RESOURCE_VERSION), RESOURCE_VERSION);

    String tempExpression = currentExpression;

    while (indexMap.lastKey() < tempExpression.lastIndexOf(RegistryConstants.PATH_SEPARATOR)) {
        tempExpression = tempExpression.substring(0,
                tempExpression.lastIndexOf(RegistryConstants.PATH_SEPARATOR));
        path = path.substring(0, path.lastIndexOf(RegistryConstants.PATH_SEPARATOR));
    }

    prefix = currentExpression.substring(0, currentExpression.indexOf(indexMap.get(indexMap.higherKey(-1))));

    if (!path.startsWith(prefix)) {
        return path;
    }
    path = path.replace(prefix, "");

    while (true) {
        if (indexMap.firstKey() < 0) {
            indexMap.pollFirstEntry();
        } else {
            break;
        }
    }

    while (true) {
        if (indexMap.size() == 0) {
            break;
        }
        Map.Entry lastEntry = indexMap.pollLastEntry();
        if (lastEntry.getValue().equals(RESOURCE_PATH)) {
            String pathValue = path;

            for (int i = 0; i < indexMap.size(); i++) {
                //                    pathValue = formatPath(pathValue.substring(path.indexOf(RegistryConstants.PATH_SEPARATOR)));
                pathValue = formatPath(
                        pathValue.substring(pathValue.indexOf(RegistryConstants.PATH_SEPARATOR)));
            }

            if (!pathValue.equals("")) {
                returnPath = returnPath.replace(RESOURCE_PATH, formatPath(pathValue));
                path = path.replace(pathValue, "");
            } else {
                returnPath = returnPath.replace("/" + lastEntry.getValue(), "");
            }

            continue;
        }
        if (lastEntry.getValue().equals(RESOURCE_VERSION)) {
            returnPath = returnPath.replace(RESOURCE_VERSION, newResourceVersion);
            if (path.contains("/")) {
                path = path.substring(0, path.lastIndexOf(RegistryConstants.PATH_SEPARATOR));
            } else {
                path = "";
            }
            continue;
        }

        String tempPath;
        if (path.contains("/")) {
            tempPath = path.substring(path.lastIndexOf(RegistryConstants.PATH_SEPARATOR) + 1);
        } else {
            tempPath = path;
        }
        if (!tempPath.equals("")) {
            returnPath = returnPath.replace((String) lastEntry.getValue(), formatPath(tempPath));
            if (path.contains("/")) {
                path = path.substring(0, path.lastIndexOf(RegistryConstants.PATH_SEPARATOR));
            } else {
                path = "";
            }
        } else {
            returnPath = returnPath.replace("/" + lastEntry.getValue(), "");
            if (path.contains("/")) {
                path = path.substring(0, path.lastIndexOf(RegistryConstants.PATH_SEPARATOR));
            }
        }

    }

    //        Adding the version validation here.
    if (!newResourceVersion.matches("^\\d+[.]\\d+[.]\\d+(-[a-zA-Z0-9]+)?$")) {
        String message = "Invalid version found for " + RegistryUtils.getResourceName(path);
        log.error(message);
        throw new RegistryException(message);
    }
    if (returnPath.contains(RESOURCE_VERSION)) {
        return returnPath.replace(RESOURCE_VERSION, newResourceVersion);
    }
    return returnPath;
}