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:MyComparator.java

public static void main(String[] args) {
    TreeMap tm = new TreeMap();
    tm.put(1, new Double(344.34));
    tm.put(0, new Double(123.22));
    tm.put(4, new Double(138.00));
    tm.put(2, new Double(919.22));
    tm.put(3, new Double(-119.08));

    List<Map.Entry> valueList = new ArrayList(tm.entrySet());
    Collections.sort(valueList, new MyComparator());

    Iterator<Map.Entry> iterator = valueList.iterator();
    while (iterator.hasNext()) {
        Map.Entry entry = iterator.next();
        System.out.println("Value: " + entry.getValue());
    }/* w  ww  . j  ava  2  s  .c  o m*/
}

From source file:TreeMapDemo.java

public static void main(String args[]) {
    TreeMap<String, Double> tm = new TreeMap<String, Double>();

    tm.put("A", new Double(3.34));
    tm.put("B", new Double(1.22));
    tm.put("C", new Double(1.00));
    tm.put("D", new Double(9.22));
    tm.put("E", new Double(-1.08));

    Set<Map.Entry<String, Double>> set = tm.entrySet();

    for (Map.Entry<String, Double> me : set) {
        System.out.print(me.getKey() + ": ");
        System.out.println(me.getValue());
    }/*  ww  w. j  ava2s  .c o m*/

    double balance = tm.get("A");
    tm.put("B", balance + 1000);

    System.out.println(tm.get("A"));
}

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

    // putting values in set
    Set mapset = treemap.entrySet();

    System.out.println("Checking value");
    System.out.println("Entry set values: " + mapset);
}

From source file:MainClass.java

public static void main(String args[]) {

    TreeMap<String, Double> tm = new TreeMap<String, Double>();

    tm.put("A", new Double(3434.34));
    tm.put("B", new Double(123.22));
    tm.put("C", new Double(1378.00));
    tm.put("D", new Double(99.22));
    tm.put("E", new Double(-19.08));

    Set<Map.Entry<String, Double>> set = tm.entrySet();

    for (Map.Entry<String, Double> me : set) {
        System.out.print(me.getKey() + ": ");
        System.out.println(me.getValue());
    }/*from   www  . ja v  a  2s  . co m*/
    System.out.println();

    double balance = tm.get("B");
    tm.put("B", balance + 1000);

    System.out.println("B's new balance: " + tm.get("B"));
}

From source file:TComp.java

public static void main(String args[]) {
    TreeMap<String, Double> tm = new TreeMap<String, Double>(new TComp());

    tm.put("J D", new Double(3434.34));
    tm.put("T S", new Double(123.22));
    tm.put("J B", new Double(1378.00));
    tm.put("T H", new Double(99.22));
    tm.put("R S", new Double(-19.08));

    Set<Map.Entry<String, Double>> set = tm.entrySet();

    for (Map.Entry<String, Double> me : set) {
        System.out.print(me.getKey() + ": ");
        System.out.println(me.getValue());
    }/*from   w  w  w . j  a v  a2 s  .  c  om*/
    System.out.println();

    double balance = tm.get("A");
    tm.put("A", balance + 1000);

    System.out.println("A's new balance: " + tm.get("A"));
}

From source file:com.game.ui.views.MapPanel.java

public static void main(String[] args) throws IOException {
    MapInformation map = new MapInformation();
    try {/*from  w  w w .j av a2  s.co  m*/
        map = GameUtils.fetchParticularMapData(Configuration.PATH_FOR_MAP, "Test1");
        Player player = new Player();
        player.setType("Barbarian");
        player.setMovement(1);
        int user = 0;
        LinkedHashMap<Integer, Integer> userLocation = new LinkedHashMap<>();
        TreeMap<Integer, TileInformation> tileInfo = map.getPathMap();
        for (Map.Entry<Integer, TileInformation> entry : tileInfo.entrySet()) {
            if (entry.getValue().isStartTile()) {
                userLocation.put(user, entry.getValue().getLocation());
                entry.getValue().setPlayer(player);
                user++;
            }
        }
        map.setUserLocation(userLocation);
    } catch (Exception ex) {
        Logger.getLogger(MapPanel.class.getName()).log(Level.SEVERE, null, ex);
    }
    new MapPanel(map);
}

From source file:com.git.ifly6.components.Census.java

public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);

    try {//from w ww  .  j  a  v  a 2s  .  c o  m
        region = new NSRegion(args[0]);
    } catch (ArrayIndexOutOfBoundsException e) {
        System.out.print("Please input the name of your region: \t");
        region = new NSRegion(scan.nextLine());
    }

    try {

        HashMap<String, Integer> endoMap = new HashMap<String, Integer>();
        String[] waMembers = region.getWAMembers();
        int[] valueCount = new int[waMembers.length];

        System.out.println(
                "[INFO] This census will take: " + time((int) Math.round(waitTime * waMembers.length)));

        for (int i = 0; i < waMembers.length; i++) {
            NSNation nation = new NSNation(waMembers[i]);
            valueCount[i] = nation.getEndoCount();
            endoMap.put(waMembers[i], new Integer(valueCount[i]));

            System.out.println("[LOG] Fetched information for: " + waMembers[i] + ", " + (i + 1) + " of "
                    + waMembers.length);
        }

        TreeMap<String, Integer> sortedMap = sortByValue(endoMap);

        int current = 0;
        int previous = sortedMap.firstEntry().getValue();

        System.out.printf("%-35s %12s %12s%n", "Nations", "Endorsements", "Difference");
        System.out.println("-------------------------------------------------------------");

        for (Map.Entry<String, Integer> entry : sortedMap.entrySet()) {

            String nationName = StringUtils.capitalize(entry.getKey().replace('_', ' '));
            current = entry.getValue();

            if ((previous - current) != 0) {
                System.out.printf("%-35s %12s %12s%n", nationName, entry.getValue(), (previous - current));
            } else {
                System.out.printf("%-35s %12s %12s%n", nationName, entry.getValue(), "-");
            }

            previous = entry.getValue();
        }

        System.out.println("-------------------------------------------------------------");
        System.out.printf("%-35s %12s %12s%n", "Delegate", "Endorsements", "Proportion");
        System.out.printf("%-35s %12s %12s%n",
                StringUtils.capitalize(sortedMap.firstEntry().getKey().replace('_', ' ')),
                sortedMap.firstEntry().getValue(),
                (double) (sortedMap.firstEntry().getValue() / waMembers.length));

    } catch (IOException e) {
        printError("Failed to fetch WA members or get endorsements in this region. "
                + "Check your internet connection or the state of the API.");
    }

    scan.close();
}

From source file:BitLottoVerify.java

public static void main(String args[]) throws Exception {
    if (args.length < 3) {
        System.out.println("Args: blockhash megamillions addrs");
        System.out.println(// ww w . ja v  a  2s  .co  m
                "Example: 14avxyPW5PgA68kGkDkY1mCGPP8zqkywEx 000000000000042c91c9de46f802524ab1c2296923a72b55fac2d2c6fd7f4741 113538415240");
        System.exit(1);
    }

    String blockhash = args[0];
    String megamillions = args[1];

    String mixer = blockhash + megamillions;
    String mixer_hash = sha256(blockhash + megamillions);
    System.out.println("Mixer: " + mixer);
    System.out.println("Mixer hash: " + mixer_hash);
    TreeMap<String, String> draw_map = new TreeMap<String, String>();

    for (int i = 2; i < args.length; i++) {
        String addr = args[i];
        System.out.println("Draw address: " + addr);

        draw_map.putAll(getDrawTxSet(addr, mixer_hash));

    }

    System.out.println();
    for (Map.Entry<String, String> me : draw_map.entrySet()) {
        System.out.println(me.getKey().substring(0, 8) + ": " + me.getValue());
    }

}

From source file:cc.twittertools.util.VerifySubcollection.java

@SuppressWarnings("static-access")
public static void main(String[] args) throws Exception {
    Options options = new Options();

    options.addOption(OptionBuilder.withArgName("dir").hasArg().withDescription("source collection directory")
            .create(COLLECTION_OPTION));
    options.addOption(//from ww  w .  jav  a  2  s.co m
            OptionBuilder.withArgName("file").hasArg().withDescription("list of tweetids").create(ID_OPTION));

    CommandLine cmdline = null;
    CommandLineParser parser = new GnuParser();
    try {
        cmdline = parser.parse(options, args);
    } catch (ParseException exp) {
        System.err.println("Error parsing command line: " + exp.getMessage());
        System.exit(-1);
    }

    if (!cmdline.hasOption(COLLECTION_OPTION) || !cmdline.hasOption(ID_OPTION)) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(ExtractSubcollection.class.getName(), options);
        System.exit(-1);
    }

    String collectionPath = cmdline.getOptionValue(COLLECTION_OPTION);

    LongOpenHashSet tweetids = new LongOpenHashSet();
    File tweetidsFile = new File(cmdline.getOptionValue(ID_OPTION));
    if (!tweetidsFile.exists()) {
        System.err.println("Error: " + tweetidsFile + " does not exist!");
        System.exit(-1);
    }
    LOG.info("Reading tweetids from " + tweetidsFile);

    FileInputStream fin = new FileInputStream(tweetidsFile);
    BufferedReader br = new BufferedReader(new InputStreamReader(fin));

    String s;
    while ((s = br.readLine()) != null) {
        tweetids.add(Long.parseLong(s));
    }
    br.close();
    fin.close();
    LOG.info("Read " + tweetids.size() + " tweetids.");

    File file = new File(collectionPath);
    if (!file.exists()) {
        System.err.println("Error: " + file + " does not exist!");
        System.exit(-1);
    }

    LongOpenHashSet seen = new LongOpenHashSet();
    TreeMap<Long, String> tweets = Maps.newTreeMap();

    PrintStream out = new PrintStream(System.out, true, "UTF-8");
    StatusStream stream = new JsonStatusCorpusReader(file);
    Status status;
    int cnt = 0;
    while ((status = stream.next()) != null) {
        if (!tweetids.contains(status.getId())) {
            LOG.error("tweetid " + status.getId() + " doesn't belong in collection");
            continue;
        }
        if (seen.contains(status.getId())) {
            LOG.error("tweetid " + status.getId() + " already seen!");
            continue;
        }

        tweets.put(status.getId(), status.getJsonObject().toString());
        seen.add(status.getId());
        cnt++;
    }
    LOG.info("total of " + cnt + " tweets in subcollection.");

    for (Map.Entry<Long, String> entry : tweets.entrySet()) {
        out.println(entry.getValue());
    }

    stream.close();
    out.close();
}

From source file:com.cloud.test.utils.SubmitCert.java

public static void main(String[] args) {
    // Parameters
    List<String> argsList = Arrays.asList(args);
    Iterator<String> iter = argsList.iterator();
    while (iter.hasNext()) {
        String arg = iter.next();

        if (arg.equals("-c")) {
            certFileName = iter.next();/*w ww  . j a v  a 2  s . com*/
        }

        if (arg.equals("-s")) {
            secretKey = iter.next();
        }

        if (arg.equals("-a")) {
            apiKey = iter.next();
        }

        if (arg.equals("-action")) {
            url = "Action=" + iter.next();
        }
    }

    Properties prop = new Properties();
    try {
        prop.load(new FileInputStream("conf/tool.properties"));
    } catch (IOException ex) {
        s_logger.error("Error reading from conf/tool.properties", ex);
        System.exit(2);
    }

    host = prop.getProperty("host");
    port = prop.getProperty("port");

    if (url.equals("Action=SetCertificate") && certFileName == null) {
        s_logger.error("Please set path to certificate (including file name) with -c option");
        System.exit(1);
    }

    if (secretKey == null) {
        s_logger.error("Please set secretkey  with -s option");
        System.exit(1);
    }

    if (apiKey == null) {
        s_logger.error("Please set apikey with -a option");
        System.exit(1);
    }

    if (host == null) {
        s_logger.error("Please set host in tool.properties file");
        System.exit(1);
    }

    if (port == null) {
        s_logger.error("Please set port in tool.properties file");
        System.exit(1);
    }

    TreeMap<String, String> param = new TreeMap<String, String>();

    String req = "GET\n" + host + ":" + prop.getProperty("port") + "\n/" + prop.getProperty("accesspoint")
            + "\n";
    String temp = "";

    if (certFileName != null) {
        cert = readCert(certFileName);
        param.put("cert", cert);
    }

    param.put("AWSAccessKeyId", apiKey);
    param.put("Expires", prop.getProperty("expires"));
    param.put("SignatureMethod", prop.getProperty("signaturemethod"));
    param.put("SignatureVersion", "2");
    param.put("Version", prop.getProperty("version"));

    StringTokenizer str1 = new StringTokenizer(url, "&");
    while (str1.hasMoreTokens()) {
        String newEl = str1.nextToken();
        StringTokenizer str2 = new StringTokenizer(newEl, "=");
        String name = str2.nextToken();
        String value = str2.nextToken();
        param.put(name, value);
    }

    //sort url hash map by key
    Set c = param.entrySet();
    Iterator it = c.iterator();
    while (it.hasNext()) {
        Map.Entry me = (Map.Entry) it.next();
        String key = (String) me.getKey();
        String value = (String) me.getValue();
        try {
            temp = temp + key + "=" + URLEncoder.encode(value, "UTF-8") + "&";
        } catch (Exception ex) {
            s_logger.error("Unable to set parameter " + value + " for the command " + param.get("command"), ex);
        }

    }
    temp = temp.substring(0, temp.length() - 1);
    String requestToSign = req + temp;
    String signature = UtilsForTest.signRequest(requestToSign, secretKey);
    String encodedSignature = "";
    try {
        encodedSignature = URLEncoder.encode(signature, "UTF-8");
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    String url = "http://" + host + ":" + prop.getProperty("port") + "/" + prop.getProperty("accesspoint") + "?"
            + temp + "&Signature=" + encodedSignature;
    s_logger.info("Sending request with url:  " + url + "\n");
    sendRequest(url);
}