Example usage for java.util TreeMap firstKey

List of usage examples for java.util TreeMap firstKey

Introduction

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

Prototype

public K firstKey() 

Source Link

Usage

From source file:Main.java

public static void main(String[] args) {
    TreeMap<String, String> treeMap = new TreeMap<String, String>();
    treeMap.put("1", "One");
    treeMap.put("2", "Two");
    treeMap.put("3", "Three");
    treeMap.put("4", "Four");
    treeMap.put("5", "Five");

    System.out.println("Lowest key: " + treeMap.firstKey());
    System.out.println("Highest key: " + treeMap.lastKey());
}

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");

    System.out.println("Checking first key");
    System.out.println("First key is: " + treemap.firstKey());
}

From source file:SortedMapDemo.java

public static void main(String[] args) {
    TreeMap sortedMap = new TreeMap();
    sortedMap.put("Adobe", "Mountain View, CA");
    sortedMap.put("IBM", "White Plains, NY");
    sortedMap.put("Learning Tree", "Los Angeles, CA");
    sortedMap.put("Microsoft", "Redmond, WA");
    sortedMap.put("Netscape", "Mountain View, CA");
    sortedMap.put("O'Reilly", "Sebastopol, CA");
    sortedMap.put("Sun", "Mountain View, CA");
    System.out.println(sortedMap);
    Object low = sortedMap.firstKey(), high = sortedMap.lastKey();
    System.out.println(low);//from w w w.j  ava 2  s.c o m
    System.out.println(high);
    Iterator it = sortedMap.keySet().iterator();
    for (int i = 0; i <= 6; i++) {
        if (i == 3)
            low = it.next();
        if (i == 6)
            high = it.next();
        else
            it.next();
    }
    System.out.println(low);
    System.out.println(high);
    System.out.println(sortedMap.subMap(low, high));
    System.out.println(sortedMap.headMap(high));
    System.out.println(sortedMap.tailMap(low));
}

From source file:Main.java

public static void main(String[] a) {

    TreeMap map = new TreeMap();
    map.put("key1", "value1");
    map.put("key2", "value2");
    map.put("key3", "value3");

    if (!map.isEmpty()) {
        Object last = map.lastKey();
        boolean first = true;
        do {/*  w ww.  j a  va  2  s  .co m*/
            if (!first) {
                System.out.print(", ");
            }
            System.out.print(last);
            last = map.headMap(last).lastKey();
            first = false;
        } while (last != map.firstKey());
        System.out.println();
    }
}

From source file:Main.java

public static void main(String[] a) {
    TreeMap<String, String> map = new TreeMap<String, String>();
    map.put("key1", "value1");
    map.put("key2", "value2");
    map.put("key3", "value3");

    if (!map.isEmpty()) {
        String last = map.lastKey();
        boolean first = true;
        do {//ww  w .  j av a 2  s .  co m
            if (!first) {
                System.out.print(", ");
            }
            System.out.print(last);
            last = map.headMap(last, true).lastKey();
            first = false;
        } while (last != map.firstKey());
        System.out.println();
    }
}

From source file:MainClass.java

public static void main(String args[]) {
    TreeMap map = new TreeMap();
    map.put("Virginia", "Richmond");
    map.put("Massachusetts", "Boston");
    map.put("New York", "Albany");
    map.put("Maryland", "Annapolis");

    if (!map.isEmpty()) {
        Object last = map.lastKey();
        boolean first = true;
        do {//from   w  w w . j a v  a2s  .  c om
            if (!first) {
                System.out.print(", ");
            }
            System.out.print(last);
            last = map.headMap(last).lastKey();
            first = false;
        } while (last != map.firstKey());
        System.out.println();
    }
}

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

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

    setOptions();//from   w w  w.  ja  v  a2s .  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.IRVTally.IRVTally.java

/**
 * Fills in default strings if contest information isn't provided
 * @param p_results//from   w ww  . ja  va  2  s  .c om
 * @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

/**
 * Fills in default strings if contest information isn't provided
 * @param p_results/*  w  w w .  j a va  2  s  .  c o  m*/
 * @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.scantegrity.IRVTally.IRVTally.java

/**
 * Creates a Results File.//ww w  .  j a  va 2 s  . 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();
}