Example usage for java.util HashMap HashMap

List of usage examples for java.util HashMap HashMap

Introduction

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

Prototype

public HashMap(Map<? extends K, ? extends V> m) 

Source Link

Document

Constructs a new HashMap with the same mappings as the specified Map .

Usage

From source file:Main.java

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

    System.out.println(map);// w  w w .jav a  2s .co  m
}

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 {/*w w  w . j  ava2 s  .c o 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 void main(String[] args) {
    Properties properties = new Properties();

    properties.setProperty("name", "Designer");
    properties.setProperty("version", "1.0");
    properties.setProperty("vendor", "Inc");

    Map<String, String> map = new HashMap<String, String>((Map) properties);

    Set propertySet = map.entrySet();
    for (Object o : propertySet) {
        Map.Entry entry = (Map.Entry) o;
        System.out.printf("%s = %s%n", entry.getKey(), entry.getValue());
    }/* w  w w.j  a v  a  2  s .  c  o  m*/
}

From source file:Main.java

public static void main(String args[]) {

    Properties prop = new Properties();

    prop.setProperty("A", "t@h.com");
    prop.setProperty("B", "k@h.com");
    prop.setProperty("C", "R@h.com");
    prop.setProperty("D", "S@h.com");

    HashMap<String, String> propMap = new HashMap<String, String>((Map) prop);

    Set<Map.Entry<String, String>> propSet;
    propSet = propMap.entrySet();//from w  ww  .j a  va2 s. co m

    System.out.println("Contents of map: ");
    for (Map.Entry<String, String> me : propSet) {
        System.out.print(me.getKey() + ": ");
        System.out.println(me.getValue());
    }
}

From source file:example.Main.java

public static void main(String[] args) throws Exception {
    Class[] classes = new Class[3];
    classes[0] = A.class;
    classes[1] = B.class;
    classes[2] = C.class;

    JAXBContext jc = JAXBContext.newInstance(classes);

    JAXBIntrospector ji = jc.createJAXBIntrospector();
    Map<QName, Class> classByQName = new HashMap<QName, Class>(classes.length);
    for (Class clazz : classes) {
        QName qName = ji.getElementName(clazz.newInstance());
        if (null != qName) {
            classByQName.put(qName, clazz);
        }/*from   www . j  a  v  a2  s  .co m*/
    }

    QName qName = new QName("http://www.example.com", "EH");
    System.out.println(classByQName.get(qName));
}

From source file:MainClass.java

public static void main(String args[]) {
    Set simpsons = new HashSet();
    simpsons.add("B");
    simpsons.add("H");
    simpsons.add("L");
    simpsons = Collections.synchronizedSet(simpsons);
    synchronized (simpsons) {
        Iterator iter = simpsons.iterator();
        while (iter.hasNext()) {
            System.out.println(iter.next());
        }/*from www  .j av a2 s.co  m*/
    }
    Map map = Collections.synchronizedMap(new HashMap(89));
    Set set = map.entrySet();
    synchronized (map) {
        Iterator iter = set.iterator();
        while (iter.hasNext()) {
            System.out.println(iter.next());
        }
    }
}

From source file:SyncTest.java

public static void main(String args[]) {
    Set simpsons = new HashSet();
    simpsons.add("Bart");
    simpsons.add("Hugo");
    simpsons.add("Lisa");
    simpsons.add("Marge");
    simpsons.add("Homer");
    simpsons.add("Maggie");
    simpsons.add("Roy");
    simpsons = Collections.synchronizedSet(simpsons);
    synchronized (simpsons) {
        Iterator iter = simpsons.iterator();
        while (iter.hasNext()) {
            System.out.println(iter.next());
        }/*from   w  w w  . j a  va2  s  . c om*/
    }
    Map map = Collections.synchronizedMap(new HashMap(89));
    Set set = map.entrySet();
    synchronized (map) {
        Iterator iter = set.iterator();
        while (iter.hasNext()) {
            System.out.println(iter.next());
        }
    }
}

From source file:io.rodeo.chute.ChuteMain.java

public static void main(String[] args)
        throws SQLException, JsonParseException, JsonMappingException, IOException {
    InputStream is = new FileInputStream(new File(CONFIG_FILENAME));
    ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
    mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
    ChuteConfiguration config = mapper.readValue(is, ChuteConfiguration.class);
    Map<String, Importer> importManagers = new HashMap<String, Importer>(config.importerConfigurations.size());
    for (Entry<String, ImporterConfiguration> importerConfig : config.importerConfigurations.entrySet()) {
        importManagers.put(importerConfig.getKey(), importerConfig.getValue().createImporter());
    }/*from  w  w w . j  a  va  2s  . c  om*/
    Map<String, Exporter> exportManagers = new HashMap<String, Exporter>(config.exporterConfigurations.size());
    for (Entry<String, ExporterConfiguration> exporterConfig : config.exporterConfigurations.entrySet()) {
        exportManagers.put(exporterConfig.getKey(), exporterConfig.getValue().createExporter());
    }
    for (Entry<String, ConnectionConfiguration> connectionConfig : config.connectionConfigurations.entrySet()) {
        importManagers.get(connectionConfig.getValue().in)
                .addProcessor(exportManagers.get(connectionConfig.getValue().out));
    }

    for (Entry<String, Exporter> exportManager : exportManagers.entrySet()) {
        exportManager.getValue().start();
    }

    for (Entry<String, Importer> importManager : importManagers.entrySet()) {
        importManager.getValue().start();
    }
}

From source file:com.yahoo.labs.samoa.topology.impl.StormTopologySubmitter.java

public static void main(String[] args) throws IOException {
    Properties props = StormSamoaUtils.getProperties();

    String uploadedJarLocation = props.getProperty(StormJarSubmitter.UPLOADED_JAR_LOCATION_KEY);
    if (uploadedJarLocation == null) {
        logger.error("Invalid properties file. It must have key {}",
                StormJarSubmitter.UPLOADED_JAR_LOCATION_KEY);
        return;/*from w ww  .  j a  v  a  2 s .c  o  m*/
    }

    List<String> tmpArgs = new ArrayList<String>(Arrays.asList(args));
    int numWorkers = StormSamoaUtils.numWorkers(tmpArgs);

    args = tmpArgs.toArray(new String[0]);
    StormTopology stormTopo = StormSamoaUtils.argsToTopology(args);

    Config conf = new Config();
    conf.putAll(Utils.readStormConfig());
    conf.putAll(Utils.readCommandLineOpts());
    conf.setDebug(false);
    conf.setNumWorkers(numWorkers);

    String profilerOption = props.getProperty(StormTopologySubmitter.YJP_OPTIONS_KEY);
    if (profilerOption != null) {
        String topoWorkerChildOpts = (String) conf.get(Config.TOPOLOGY_WORKER_CHILDOPTS);
        StringBuilder optionBuilder = new StringBuilder();
        if (topoWorkerChildOpts != null) {
            optionBuilder.append(topoWorkerChildOpts);
            optionBuilder.append(' ');
        }
        optionBuilder.append(profilerOption);
        conf.put(Config.TOPOLOGY_WORKER_CHILDOPTS, optionBuilder.toString());
    }

    Map<String, Object> myConfigMap = new HashMap<String, Object>(conf);
    StringWriter out = new StringWriter();

    try {
        JSONValue.writeJSONString(myConfigMap, out);
    } catch (IOException e) {
        System.out.println("Error in writing JSONString");
        e.printStackTrace();
        return;
    }

    Config config = new Config();
    config.putAll(Utils.readStormConfig());

    String nimbusHost = (String) config.get(Config.NIMBUS_HOST);

    NimbusClient nc = new NimbusClient(nimbusHost);
    String topologyName = stormTopo.getTopologyName();
    try {
        System.out.println("Submitting topology with name: " + topologyName);
        nc.getClient().submitTopology(topologyName, uploadedJarLocation, out.toString(),
                stormTopo.getStormBuilder().createTopology());
        System.out.println(topologyName + " is successfully submitted");

    } catch (AlreadyAliveException aae) {
        System.out.println("Fail to submit " + topologyName + "\nError message: " + aae.get_msg());
    } catch (InvalidTopologyException ite) {
        System.out.println("Invalid topology for " + topologyName);
        ite.printStackTrace();
    } catch (TException te) {
        System.out.println("Texception for " + topologyName);
        te.printStackTrace();
    }
}

From source file:com.avego.oauth.migration.OauthDataMigrator.java

/**
 * This migrates spring security oauth 2 token data in m6 form to 1.0.5 release form
 * @param args/*ww w.  jav a  2  s.c  o m*/
 * @throws Exception
 */
public static void main(String[] args) throws Exception {

    if (args.length < 3) {
        System.err.println("Usage <db_jdbc_url> <db_user> <db_pw>");
        System.err.println("Or <db_jdbc_url> <db_user> <db_pw>"
                + " <remove_refresh_tokens> <serialize_new_token_values> <oauth_access_token_table> <oauth_refresh_token_table>");
        System.exit(1);
    }

    Boolean removeRefreshTokens = Boolean.FALSE;
    if (args.length > 3) {
        removeRefreshTokens = Boolean.parseBoolean(args[3]);
    }
    Boolean serializeNewTokenValues = Boolean.FALSE;
    if (args.length > 4) {
        serializeNewTokenValues = Boolean.parseBoolean(args[4]);
    }

    Map<String, Object> params = new HashMap<String, Object>(3);
    params.put(JdbcOauthMigrationDao.JDBC_URL_KEY, args[0]);
    params.put(JdbcOauthMigrationDao.USER_KEY, args[1]);
    params.put(JdbcOauthMigrationDao.PASS_KEY, args[2]);
    params.put(REMOVE_REFRESH_TOKENS_PARAM, removeRefreshTokens);
    params.put(SERIALIZE_NEW_TOKEN_VALUES_PARAM, serializeNewTokenValues);

    if (args.length > 5) {
        params.put(JdbcOauthMigrationDao.ACCESS_TOKEN_TABLE, args[5]);
    }
    if (args.length > 6) {
        params.put(JdbcOauthMigrationDao.REFRESH_TOKEN_TABLE, args[6]);
    }
    OauthDataMigrator migrator = new OauthDataMigrator(params);
    migrator.migrateData();
}