Example usage for java.util Map put

List of usage examples for java.util Map put

Introduction

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

Prototype

V put(K key, V value);

Source Link

Document

Associates the specified value with the specified key in this map (optional operation).

Usage

From source file:TreeMapExample.java

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

    // Add Items to the TreeMap
    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");
    map.put(new Integer(6), "Six");
    map.put(new Integer(7), "Seven");
    map.put(new Integer(8), "Eight");
    map.put(new Integer(9), "Nine");
    map.put(new Integer(10), "Ten");

    // Use iterator to display the keys and associated values
    System.out.println("Map Values Before: ");
    Set keys = map.keySet();// www.  j a  v  a2s  . c  o  m
    for (Iterator i = keys.iterator(); i.hasNext();) {
        Integer key = (Integer) i.next();
        String value = (String) map.get(key);
        System.out.println(key + " = " + value);
    }

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

    // Use iterator to display the keys and associated values
    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:Main.java

public static void main(String[] args) {
    Map<Employee, String> map2 = new HashMap<Employee, String>();
    Employee e2 = new Employee("J", 26);
    map2.put(e2, "MGMT");
    System.out.println(map2);/*from  w  w  w.j av  a2  s  . c om*/
    e2.setAge(27);
    System.out.println(map2);
    System.out.println(map2.containsKey(e2));//false

    IdentityHashMap<Employee, String> map1 = new IdentityHashMap<Employee, String>(map2);

    System.out.println(map1);
}

From source file:com.plugtree.integration.external.jms.JMSProducer.java

public static void main(final String[] args) throws Exception {
    ApplicationContext context = new ClassPathXmlApplicationContext("camel-client.xml");
    CamelContext camel = context.getBean("camel-client", CamelContext.class);

    // get the endpoint from the camel context
    Endpoint endpoint = camel.getEndpoint("jms:queue:numbers");

    // create the exchange used for the communication
    // we use the in out pattern for a synchronized exchange where we expect a response
    Exchange exchange = endpoint.createExchange(ExchangePattern.InOut);
    // set the input on the in body
    // must you correct type to match the expected type of an Integer object
    Map<String, String> props = new HashMap<String, String>();
    props.put("key1", "value1");
    props.put("key2", "value2");
    props.put("key3", "value3");

    exchange.getIn().setBody(props);//from   w w w  .  jav  a 2  s  .c  o m

    // to send the exchange we need an producer to do it for us
    Producer producer = endpoint.createProducer();
    // start the producer so it can operate
    producer.start();

    // let the producer process the exchange where it does all the work in this oneline of code
    producer.process(exchange);

    // get the response from the out body and cast it to an integer
    String response = exchange.getOut().getBody(String.class);
    System.out.println("... the result is: " + response);
    System.exit(0);

}

From source file:com.espertech.esperio.regression.adapter.SupportJMSSender.java

public static void main(String[] args) throws JMSException {
    SupportJMSSender sender = new SupportJMSSender();
    //sender.sendSerializable("hello");

    Map<String, Object> values = new HashMap<String, Object>();
    values.put("prop1", "a");
    sender.sendMap(values);//from  www.j  a va  2  s  .c  o  m

    sender.destroy();
    System.exit(1);
}

From source file:RandomGenTest.java

public static void main(String[] args) {
    RandomDataGenerator rand = new RandomDataGenerator();

    Map<Object, Integer> map = new HashMap();
    for (int i = 0; i < 101; i++)
        map.put(i, 0);

    Map<Object, Integer> user = new HashMap();
    for (int i = 0; i < 100; i++) {
        int age = rand.nextInt(0, 100);
        /*int age = (int) rand.nextGaussian(50, 10D);
        if ((age > 100) || (age < 0)) {
           age = rand.nextInt(0, 100);//from  w  w w . java 2  s  .c  o m
        }*/
        user.put(i, age);
    }

    for (int i = 0; i < 100000; i++) {
        //int age = rand.nextInt(0, 100);
        /*int age = (int) rand.nextGaussian(50, 10D);
        if ((age > 100) || (age < 0)) {
           age = rand.nextInt(0, 100);
        }*/

        //Seq
        //map.put(user.get(i%100), map.get(user.get(i%100))+1);

        //Rand
        int j = rand.nextInt(0, 99);
        map.put(user.get(j), map.get(user.get(j)) + 1);
    }

    display(map);
}

From source file:com.rest.samples.getReportFromJasperServerWithSeparateAuthFormatURL.java

public static void main(String[] args) {
    // TODO code application logic here
    Map<String, String> params = new HashMap<String, String>();
    params.put("host", "10.49.28.3");
    params.put("port", "8081");
    params.put("reportName", "vencimientos");
    params.put("parametros", "feini=2016-09-30&fefin=2016-09-30");
    StrSubstitutor sub = new StrSubstitutor(params, "{", "}");
    String urlTemplate = "http://{host}:{port}/jasperserver/rest_v2/reports/Reportes/{reportName}.pdf?{parametros}";
    String url = sub.replace(urlTemplate);

    try {/*from ww  w .j av  a2 s  . c om*/
        CredentialsProvider cp = new BasicCredentialsProvider();
        cp.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("jasperadmin", "jasperadmin"));
        CloseableHttpClient hc = HttpClientBuilder.create().setDefaultCredentialsProvider(cp).build();

        HttpGet getMethod = new HttpGet(url);
        getMethod.addHeader("accept", "application/pdf");
        HttpResponse res = hc.execute(getMethod);
        if (res.getStatusLine().getStatusCode() != 200) {
            throw new RuntimeException("Failed : HTTP eror code: " + res.getStatusLine().getStatusCode());
        }

        InputStream is = res.getEntity().getContent();
        OutputStream os = new FileOutputStream(new File("vencimientos.pdf"));
        int read = 0;
        byte[] bytes = new byte[2048];

        while ((read = is.read(bytes)) != -1) {
            os.write(bytes, 0, read);
        }
        is.close();
        os.close();

        if (Desktop.isDesktopSupported()) {
            File pdfFile = new File("vencimientos.pdf");
            Desktop.getDesktop().open(pdfFile);
        }

    } catch (IOException ex) {
        Logger.getLogger(SamplesUseHttpclient.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.textocat.textokit.postagger.GenerateAggregateDescriptorForMorphAnnotator.java

public static void main(String[] args) throws UIMAException, IOException, SAXException {
    if (args.length != 1) {
        System.err.println("Usage: <output-path>");
        System.exit(1);//from www.j  a  va2 s  .c om
    }
    String outputPath = args[0];
    // NOTE! A file URL for generated SerializedDictionaryResource description assumes
    // that the required dictionary file is within one of UIMA datapath folders.
    // So users of the generated aggregate descriptor should setup 'uima.datapath' properly .
    ExternalResourceDescription morphDictDesc = getMorphDictionaryAPI()
            .getResourceDescriptionWithPredictorEnabled();

    Map<String, MetaDataObject> aeDescriptions = Maps.newLinkedHashMap();
    aeDescriptions.put("tokenizer", TokenizerAPI.getAEImport());
    //
    aeDescriptions.put("sentence-splitter", SentenceSplitterAPI.getAEImport());
    //
    aeDescriptions.put("morph-analyzer", MorphologyAnnotator.createDescription(DefaultAnnotationAdapter.class,
            PosTaggerAPI.getTypeSystemDescription()));
    //
    aeDescriptions.put("tag-assembler", TagAssembler.createDescription());
    AnalysisEngineDescription desc = PipelineDescriptorUtils.createAggregateDescription(aeDescriptions);
    // bind the dictionary resource
    bindExternalResource(desc, "morph-analyzer/" + MorphologyAnnotator.RESOURCE_KEY_DICTIONARY, morphDictDesc);
    bindExternalResource(desc, "tag-assembler/" + GramModelBasedTagMapper.RESOURCE_GRAM_MODEL, morphDictDesc);

    FileOutputStream out = FileUtils.openOutputStream(new File(outputPath));
    try {
        desc.toXML(out);
    } finally {
        IOUtils.closeQuietly(out);
    }
}

From source file:Main.java

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

    atomNums.put("A", 1);
    atomNums.put("B", 2);
    atomNums.put("C", 3);
    atomNums.put("D", 4);
    atomNums.put("E", 5);
    atomNums.put("F", 6);

    System.out.println("The map contains these " + atomNums.size() + " entries:");

    Set<Map.Entry<String, Integer>> set = atomNums.entrySet();

    for (Map.Entry<String, Integer> me : set) {
        System.out.print(me.getKey() + ", Atomic Number: ");
        System.out.println(me.getValue());
    }//from   ww  w. j  a  va  2  s .  com
    TreeMap<String, Integer> atomNums2 = new TreeMap<String, Integer>();

    atomNums2.put("Q", 30);
    atomNums2.put("W", 82);

    atomNums.putAll(atomNums2);

    set = atomNums.entrySet();

    System.out.println("The map now contains these " + atomNums.size() + " entries:");
    for (Map.Entry<String, Integer> me : set) {
        System.out.print(me.getKey() + ", Atomic Number: ");
        System.out.println(me.getValue());
    }

    if (atomNums.containsKey("A"))
        System.out.println("A has an atomic number of " + atomNums.get("A"));

    if (atomNums.containsValue(82))
        System.out.println("The atomic number 82 is in the map.");
    System.out.println();

    if (atomNums.remove("A") != null)
        System.out.println("A has been removed.\n");
    else
        System.out.println("Entry not found.\n");

    Set<String> keys = atomNums.keySet();
    for (String str : keys)
        System.out.println(str + " ");

    Collection<Integer> vals = atomNums.values();
    for (Integer n : vals)
        System.out.println(n + " ");

    atomNums.clear();
    if (atomNums.isEmpty())
        System.out.println("The map is now empty.");
}

From source file:Main.java

public static void main(String[] args) {
    Map<String, String> map = new ConcurrentHashMap<String, String>();
    for (int i = 0; i < MAP_SIZE; i++) {
        map.put("key:" + i, UUID.randomUUID().toString());
    }//  w  w w  .  j  av  a 2s . c o  m
    ExecutorService executor = Executors.newCachedThreadPool();
    Accessor a1 = new Accessor(map);
    Accessor a2 = new Accessor(map);
    Mutator m = new Mutator(map);
    executor.execute(a1);
    executor.execute(m);
    executor.execute(a2);
}

From source file:com.surfs.storage.common.util.HttpUtils.java

public static void main(String[] args) throws UnsupportedEncodingException {
    Map<String, String> args1 = new HashMap<String, String>();
    args1.put("1", "1");
    args1.put("2", "2");
    args1.put("3", "3");

    String url = getUrlForParams("127.0.0.1", "8080", "/service", "/test", args1);
    System.out.println(url);/*  w w w  . j a v a2 s.  c o  m*/
}