Example usage for java.util Hashtable put

List of usage examples for java.util Hashtable put

Introduction

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

Prototype

public synchronized V put(K key, V value) 

Source Link

Document

Maps the specified key to the specified value in this hashtable.

Usage

From source file:MainClass.java

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

    Enumeration<String> names;
    String str;/*from w  w  w .j av a2  s  .com*/

    balance.put("A", 3434.34);

    names = balance.keys();
    while (names.hasMoreElements()) {
        str = names.nextElement();
        System.out.println(str + ": " + balance.get(str));
    }

    System.out.println();
}

From source file:HTDemo2.java

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

    String str;//w ww.  jav  a  2  s .  c o  m
    double bal;

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

    Set<String> set = balance.keySet();

    Iterator<String> itr = set.iterator();
    while (itr.hasNext()) {
        str = itr.next();
        System.out.println(str + ": " + balance.get(str));
    }

    System.out.println();

    bal = balance.get("A");
    balance.put("A", bal + 1000);
    System.out.println("A's new balance: " + balance.get("A"));
}

From source file:DigestRealm.java

public static void main(String[] args) {

    // Set up environment for creating initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

    // Authenticate as C. User and password "mysecret" in realm "JNDITutorial"
    env.put(Context.SECURITY_AUTHENTICATION, "DIGEST-MD5");

    env.put(Context.SECURITY_PRINCIPAL, "dn:cn=C. User, ou=NewHires, o=JNDITutorial");
    env.put(Context.SECURITY_CREDENTIALS, "mysecret");
    env.put("java.naming.security.sasl.realm", "JNDITutorial");

    try {/*w  ww.  j a  v a 2 s .  c om*/
        // Create initial context
        DirContext ctx = new InitialDirContext(env);

        System.out.println(ctx.lookup("ou=NewHires"));

        // do something useful with ctx

        // Close the context when we're done
        ctx.close();
    } catch (NamingException e) {
        e.printStackTrace();
    }
}

From source file:Ldaps.java

public static void main(String[] args) {
    // Set up environment for creating initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");

    // Specify LDAPS URL
    env.put(Context.PROVIDER_URL, "ldaps://localhost:636/o=JNDITutorial");

    // Authenticate as S. User and password "mysecret"
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, "cn=S. User, ou=NewHires, o=JNDITutorial");
    env.put(Context.SECURITY_CREDENTIALS, "mysecret");

    try {/*from ww  w .ja v  a 2 s.co m*/
        // Create initial context
        DirContext ctx = new InitialDirContext(env);

        System.out.println(ctx.lookup("ou=NewHires"));

        // ... do something useful with ctx

        // Close the context when we're done
        ctx.close();
    } catch (NamingException e) {
        e.printStackTrace();
    }
}

From source file:RenameInterior.java

public static void main(String[] args) {

    // Set up environment for creating initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

    try {/*  w w  w .  j  a  v  a 2  s. com*/
        // Create initial context
        DirContext ctx = new InitialDirContext(env);

        // Perform rename
        ctx.rename("ou=NewHires", "ou=OldHires");

        // Check that it worked
        System.out.println(ctx.lookup("ou=OldHires"));

        // Revert change
        ctx.rename("ou=OldHires", "ou=NewHires");

        // Check that we are back at our original setup
        System.out.println(ctx.lookup("ou=NewHIres"));

        // Close the context when we're done
        ctx.close();
    } catch (NamingException e) {
        e.printStackTrace();
    }
}

From source file:RetrievingLdapName.java

public static void main(String[] args) {

    // Set up the environment for creating the initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

    try {//from w w  w  .  j  av  a  2s . c  o m
        // Create initial context
        DirContext ctx = new InitialDirContext(env);

        NamingEnumeration answer = ctx.search("ou=People", null);

        // Print the answer
        while (answer.hasMore()) {
            SearchResult sr = (SearchResult) answer.next();
            String name = sr.getNameInNamespace();
            System.out.println(name);
            LdapName dn = new LdapName(name);

            // do something with the dn
        }

        // Close the context when we're done
        ctx.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:RenameDiffParent.java

public static void main(String[] args) {

    // Set up environment for creating initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

    try {// w ww. j a v  a  2s.  c  o  m
        // Create initial context
        DirContext ctx = new InitialDirContext(env);

        // Perform rename
        ctx.rename("cn=C. User, ou=NewHires", "cn=C. User, ou=People");

        // Check that it worked
        System.out.println(ctx.lookup("cn=C. User, ou=People"));

        // Revert change
        ctx.rename("cn=C. User, ou=People", "cn=C. User, ou=NewHires");

        // Check that we are back at our original setup
        System.out.println(ctx.lookup("cn=C. User, ou=NewHires"));

        // Close the context when we're done
        ctx.close();
    } catch (NamingException e) {
        e.printStackTrace();
    }
}

From source file:Ssl.java

public static void main(String[] args) {
    // Set up environment for creating initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:636/o=JNDITutorial");

    // Specify SSL
    env.put(Context.SECURITY_PROTOCOL, "ssl");

    // Authenticate as S. User and password "mysecret"
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, "cn=S. User, ou=NewHires, o=JNDITutorial");
    env.put(Context.SECURITY_CREDENTIALS, "mysecret");

    try {//from ww  w.j  a  va2 s. co  m
        // Create initial context
        DirContext ctx = new InitialDirContext(env);

        System.out.println(ctx.lookup("ou=NewHires"));

        // ... do something useful with ctx

        // Close the context when we're done
        ctx.close();
    } catch (NamingException e) {
        e.printStackTrace();
    }
}

From source file:SerObj.java

public static void main(String[] args) {

    // Set up environment for creating initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

    try {//from   w ww . j a v a2 s .c om
        // Create the initial context
        Context ctx = new InitialContext(env);

        // Create object to be bound
        Button b = new Button("Push me");

        // Perform bind
        ctx.bind("cn=Button", b);

        // Check that it is bound
        Button b2 = (Button) ctx.lookup("cn=Button");
        System.out.println(b2);

        // Close the context when we're done
        ctx.close();
    } catch (NamingException e) {
        System.out.println("Operation failed: " + e);
    }
}

From source file:Digest.java

public static void main(String[] args) {

    // Set up environment for creating initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

    // Authenticate as C. User and password "mysecret"
    env.put(Context.SECURITY_AUTHENTICATION, "DIGEST-MD5");

    env.put(Context.SECURITY_PRINCIPAL, "dn:cn=C. User, ou=NewHires, o=JNDITutorial");
    env.put(Context.SECURITY_CREDENTIALS, "mysecret");

    env.put("com.sun.jndi.ldap.trace.ber", System.out);

    try {//from  w  w w .j  a va 2  s.c  o  m
        // Create initial context
        DirContext ctx = new InitialDirContext(env);

        System.out.println(ctx.lookup("ou=NewHires"));

        // do something useful with ctx

        // Close the context when we're done
        ctx.close();
    } catch (NamingException e) {
        e.printStackTrace();
    }
}