Example usage for java.util Map keySet

List of usage examples for java.util Map keySet

Introduction

In this page you can find the example usage for java.util Map keySet.

Prototype

Set<K> keySet();

Source Link

Document

Returns a Set view of the keys contained in this map.

Usage

From source file:Main.java

public static void main(String[] a) {
    Map<String, String> yourMap = new HashMap<String, String>();
    yourMap.put("1", "one");
    yourMap.put("2", "two");
    yourMap.put("3", "three");

    Map<String, Object> map = new LinkedHashMap<String, Object>();

    List<String> keyList = new ArrayList<String>(yourMap.keySet());
    List<String> valueList = new ArrayList<String>(yourMap.values());
    Set<String> sortedSet = new TreeSet<String>(valueList);

    Object[] sortedArray = sortedSet.toArray();
    int size = sortedArray.length;

    for (int i = 0; i < size; i++) {
        map.put(keyList.get(valueList.indexOf(sortedArray[i])), sortedArray[i]);
    }//from  w  w  w  . j  a v  a 2  s. co m

    Set ref = map.keySet();
    Iterator it = ref.iterator();

    while (it.hasNext()) {
        String i = (String) it.next();
        System.out.println(i);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Map map = new HashMap<String, String>();
    map.put("cluster", "10.200.111.111");
    map.put("cluster1", "10.200.121.111");

    Product xml = new Product();
    List<Top> top1 = new ArrayList<Top>();
    Set<String> keys = map.keySet();
    for (String key : keys) {
        Top top = new Top();
        top.setMode(key);/* ww  w .  j  a va2 s.  c om*/
        top.setAddress((String) map.get(key));
        top1.add(top);
    }
    xml.setTop(top1);
    File file = new File("C:\\kar\\file.xml");
    JAXBContext jaxbContext = JAXBContext.newInstance(Product.class);
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

    // output pretty printed
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    jaxbMarshaller.marshal(xml, file);
    jaxbMarshaller.marshal(xml, System.out);
}

From source file:Main.java

public static void main(String[] args) {
    Map<String, String> m1 = new LinkedHashMap<String, String>();
    m1.put("1", "One");
    m1.put("3", "Three");

    Map<String, String> m2 = new LinkedHashMap<String, String>();
    m2.put("2", "Two");
    m2.put("4", "Four");

    List<String> list = new ArrayList<String>();
    list.addAll(m1.keySet());
    list.addAll(m2.keySet());//from w  w w. ja  v  a2s.  co m
    for (String s : list) {
        System.out.println(s);
    }
}

From source file:HashMapExample.java

public static void main(String[] args) {
    Map<Integer, String> map = new HashMap<Integer, String>();

    map.put(new Integer(1), "One");
    map.put(new Integer(2), "Two");
    map.put(new Integer(3), "Three");
    map.put(new Integer(4), "Four");
    map.put(new Integer(5), "Five");

    System.out.println("Map Values Before: ");
    Set keys = map.keySet();
    for (Iterator i = keys.iterator(); i.hasNext();) {
        Integer key = (Integer) i.next();
        String value = (String) map.get(key);
        System.out.println(key + " = " + value);
    }/*from  www.j a  v a  2  s  . com*/

    System.out.println("\nRemove element with key 6");
    map.remove(new Integer(6));

    System.out.println("\nMap Values After: ");
    keys = map.keySet();
    for (Iterator i = keys.iterator(); i.hasNext();) {
        Integer key = (Integer) i.next();
        String value = (String) map.get(key);
        System.out.println(key + " = " + value);
    }
}

From source file:net.wedjaa.elasticparser.tester.ESSearchTester.java

public static void main(String[] args) {

    System.out.println("ES Search Testing started.");

    System.out.println("Starting local node...");

    Settings nodeSettings = ImmutableSettings.settingsBuilder().put("transport.tcp.port", "9600-9700")
            .put("http.port", "9500").put("http.max_content_length", "104857600").build();

    Node node = NodeBuilder.nodeBuilder().settings(nodeSettings).clusterName("elasticparser.unittest").node();

    node.start();//  w w w  .ja  v  a 2 s . c om

    // Populate our test index
    System.out.println("Preparing Unit Test Index - this may take a while...");
    populateTest();

    try {
        System.out.println("...OK - Executing query!");
        // Try our searches
        ESSearch search = new ESSearch(null, null, ESSearch.ES_MODE_AGGS, "localhost", 9600,
                "elasticparser.unittest");
        search.search(getQuery("test-aggs.json"));
        Map<String, Object> hit = null;
        while ((hit = search.next()) != null) {
            System.out.println("Hit: {");
            for (String key : hit.keySet()) {
                System.out.println("  " + key + ": " + hit.get(key));
            }
            System.out.println("};");
        }
        search.close();
        Map<String, Class<?>> fields = search.getFields(getQuery("test-aggs.json"));
        List<String> sortedKeys = new ArrayList<String>(fields.keySet());
        Collections.sort(sortedKeys);
        Iterator<String> sortedKeyIter = sortedKeys.iterator();
        while (sortedKeyIter.hasNext()) {
            String fieldname = sortedKeyIter.next();
            System.out.println(" --> " + fieldname + "[" + fields.get(fieldname).getCanonicalName() + "]");
        }

    } catch (Exception ex) {
        System.out.println("Exception: " + ex);
    } finally {
        System.out.println("Stopping Test Node");
        node.stop();
    }
}

From source file:at.newmedialab.ldpath.backend.linkeddata.LDQuery.java

public static void main(String[] args) {
    Options options = buildOptions();/*  w w w.  j  a va 2s.  c o m*/

    CommandLineParser parser = new PosixParser();
    try {
        CommandLine cmd = parser.parse(options, args);

        Level logLevel = Level.WARN;

        if (cmd.hasOption("loglevel")) {
            String logLevelName = cmd.getOptionValue("loglevel");
            if ("DEBUG".equals(logLevelName.toUpperCase())) {
                logLevel = Level.DEBUG;
            } else if ("INFO".equals(logLevelName.toUpperCase())) {
                logLevel = Level.INFO;
            } else if ("WARN".equals(logLevelName.toUpperCase())) {
                logLevel = Level.WARN;
            } else if ("ERROR".equals(logLevelName.toUpperCase())) {
                logLevel = Level.ERROR;
            } else {
                log.error("unsupported log level: {}", logLevelName);
            }
        }

        if (logLevel != null) {
            for (String logname : new String[] { "at", "org", "net", "com" }) {

                ch.qos.logback.classic.Logger logger = (ch.qos.logback.classic.Logger) LoggerFactory
                        .getLogger(logname);
                logger.setLevel(logLevel);
            }
        }

        String format = null;
        if (cmd.hasOption("format")) {
            format = cmd.getOptionValue("format");
        }

        GenericSesameBackend backend;
        if (cmd.hasOption("store")) {
            backend = new LDPersistentBackend(new File(cmd.getOptionValue("store")));
        } else {
            backend = new LDMemoryBackend();
        }

        Resource context = null;
        if (cmd.hasOption("context")) {
            context = backend.getRepository().getValueFactory().createURI(cmd.getOptionValue("context"));
        }

        if (backend != null && context != null) {
            LDPath<Value> ldpath = new LDPath<Value>(backend);

            if (cmd.hasOption("path")) {
                String path = cmd.getOptionValue("path");

                for (Value v : ldpath.pathQuery(context, path, null)) {
                    System.out.println(v.stringValue());
                }
            } else if (cmd.hasOption("program")) {
                File file = new File(cmd.getOptionValue("program"));

                Map<String, Collection<?>> result = ldpath.programQuery(context, new FileReader(file));

                for (String field : result.keySet()) {
                    StringBuilder line = new StringBuilder();
                    line.append(field);
                    line.append(" = ");
                    line.append("{");
                    for (Iterator it = result.get(field).iterator(); it.hasNext();) {
                        line.append(it.next().toString());
                        if (it.hasNext()) {
                            line.append(", ");
                        }
                    }
                    line.append("}");
                    System.out.println(line);

                }
            }
        }

        if (backend instanceof LDPersistentBackend) {
            ((LDPersistentBackend) backend).shutdown();
        }

    } catch (ParseException e) {
        System.err.println("invalid arguments");
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("LDQuery", options, true);
    } catch (LDPathParseException e) {
        System.err.println("path or program could not be parsed");
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        System.err.println("file or program could not be found");
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("LDQuery", options, true);
    } catch (IOException e) {
        System.err.println("could not access cache data directory");
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("LDQuery", options, true);
    }

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String bubba = "this is a test this is a test";
    Map<Integer, Integer> occurrences = new HashMap<Integer, Integer>();

    for (String currentWord : bubba.split(" ")) {
        Integer current = occurrences.get(currentWord.length());
        if (current == null) {
            current = 0;/*  w  w  w. j a  v  a  2  s .  c  o m*/
        }
        occurrences.put(currentWord.length(), current + 1);
    }
    for (Integer currentKey : occurrences.keySet()) {
        System.out.println("There are " + occurrences.get(currentKey) + " " + currentKey + " letter words");
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Map<String, Integer> map = new HashMap<String, Integer>();
    map = new TreeMap();

    map.put("a", new Integer(1));
    map.put("b", new Integer(2));
    map.put("c", new Integer(3));

    int size = map.size(); // 2

    Object oldValue = map.put("a", new Integer(9)); // 1

    oldValue = map.remove("c"); // 3

    Iterator it = map.keySet().iterator();
    while (it.hasNext()) {
        Object key = it.next();// w w  w . ja v a 2  s  .  c om
    }

    it = map.values().iterator();
    while (it.hasNext()) {
        Object value = it.next();
    }
}

From source file:Main.java

public static void main(String[] args) {

    String sentence = "is this a sentence this is a test";
    String[] myStringArray = sentence.split("\\s"); // Split the sentence by
                                                    // space.

    Map<String, Integer> wordOccurrences = new HashMap<String, Integer>(myStringArray.length);

    for (String word : myStringArray) {
        if (wordOccurrences.containsKey(word)) {
            wordOccurrences.put(word, wordOccurrences.get(word) + 1);
        } else {/*ww  w. ja v  a2 s  . co m*/
            wordOccurrences.put(word, 1);
        }
    }
    for (String word : wordOccurrences.keySet()) {
        if (wordOccurrences.get(word) > 1) {
            System.out.println("1b. - Tokens that occurs more than once: " + word + "\n");
        }
    }
}

From source file:Main.java

public static final void main(String[] ignored) {
    Map<Integer, List<String>> mapOfIntStrs = new HashMap<Integer, List<String>>();

    add(mapOfIntStrs, 1, "one");
    add(mapOfIntStrs, 1, "two");
    add(mapOfIntStrs, 1, "three");
    add(mapOfIntStrs, 2, "four");
    add(mapOfIntStrs, 2, "five");
    add(mapOfIntStrs, 3, "six");
    add(mapOfIntStrs, 3, "seven");

    Set<Integer> keySet = mapOfIntStrs.keySet();

    for (int i : keySet) {
        List<String> strList = mapOfIntStrs.get(i);
        System.out.println(i);//from w ww  .  j a  v a  2s .c  om
        for (String s : strList) {
            System.out.println("  " + s);
        }
    }
}