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() 

Source Link

Document

Constructs an empty HashMap with the default initial capacity (16) and the default load factor (0.75).

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {

    List stuff = Arrays.asList(new String[] { "a", "b" });

    List list = new ArrayList(stuff);
    list = Collections.unmodifiableList(list);

    try {//from w  ww.j av  a2  s. c o  m
        list.set(0, "new value");
    } catch (UnsupportedOperationException e) {

    }

    Set set = new HashSet(stuff);
    set = Collections.unmodifiableSet(set);

    Map map = new HashMap();
    map = Collections.unmodifiableMap(map);
}

From source file:Main.java

public static void main(String[] args) {
    ReferenceQueue referenceQueue = new ReferenceQueue();
    Object object = new Object() {
        public String toString() {
            return "Referenced Object";
        }/*  ww  w.  j a  v  a2 s. co m*/
    };

    Object data = new Object() {
        public String toString() {
            return "Data";
        }
    };

    HashMap map = new HashMap();
    Reference reference = new SoftReference(object, referenceQueue);

    map.put(reference, data);

    System.out.println(reference.get());
    System.out.println(map.get(reference));
    System.out.println(reference.isEnqueued());

    System.gc();
    System.out.println(reference.get());
    System.out.println(map.get(reference));
    System.out.println(reference.isEnqueued());

    object = null;
    data = null;

    System.gc();
    System.out.println(reference.get());
    System.out.println(map.get(reference));
    System.out.println(reference.isEnqueued());
}

From source file:Main.java

public static void main(String[] args) {
    ReferenceQueue referenceQueue = new ReferenceQueue();
    Object object = new Object() {
        public String toString() {
            return "Referenced Object";
        }/*from   www.  j av  a  2 s  .co  m*/
    };

    Object data = new Object() {
        public String toString() {
            return "Data";
        }
    };

    HashMap map = new HashMap();
    Reference reference = null;
    System.out.println("Testing WeakReference.");
    reference = new WeakReference(object, referenceQueue);

    map.put(reference, data);

    System.out.println(reference.get());
    System.out.println(map.get(reference));
    System.out.println(reference.isEnqueued());

    System.gc();
    System.out.println(reference.get());
    System.out.println(map.get(reference));
    System.out.println(reference.isEnqueued());

    object = null;
    data = null;

    System.gc();
    System.out.println(reference.get());
    System.out.println(map.get(reference));
    System.out.println(reference.isEnqueued());
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);/*  w  w  w  . ja v a 2  s. co m*/
    factory.setExpandEntityReferences(false);

    Document doc = factory.newDocumentBuilder().parse(new File("filename"));

    Map entityValues = new HashMap();
    getEntityValues(doc, entityValues);

    NamedNodeMap entities = doc.getDoctype().getEntities();
    for (int i = 0; i < entities.getLength(); i++) {
        Entity entity = (Entity) entities.item(i);
        System.out.println(entity);
        String entityName = entity.getNodeName();
        System.out.println(entityName);
        String entityPublicId = entity.getPublicId();
        System.out.println(entityPublicId);
        String entitySystemId = entity.getSystemId();
        System.out.println(entitySystemId);
        Node entityValue = (Node) entityValues.get(entityName);
        System.out.println(entityValue);
    }
}

From source file:Main.java

public static void main(final String[] args) {
    final List<String> asu = new ArrayList<String>();
    asu.add("2");
    asu.add("11");
    asu.add("7");
    asu.add("10");
    asu.add("7");
    asu.add("12");
    asu.add("2");
    asu.add("11");
    asu.add("11");
    asu.add("7");
    asu.add("7");
    asu.add("7");

    List<String> list = new ArrayList<String>();
    Map<String, Integer> counts = new HashMap<String, Integer>();
    list.addAll(asu);/*from www  .jav  a2  s .  c  om*/
    for (String item : list) {
        Integer count = counts.get(item);
        if (count == null) {
            count = 1;
        } else {
            count = count + 1;
        }
        counts.put(item, count);
    }
    Collections.sort(asu, new Comparator<String>() {
        @Override
        public int compare(final String left, final String right) {
            int result = counts.get(left).compareTo(counts.get(right));
            if (result == 0) {
                result = left.compareTo(right);
            }
            return result;
        }
    });
    System.out.println(asu);
}

From source file:Synchronization.java

public static void main(String[] args) {
    Collection c = Collections.synchronizedCollection(new ArrayList());
    List list = Collections.synchronizedList(new ArrayList());
    Set s = Collections.synchronizedSet(new HashSet());
    Map m = Collections.synchronizedMap(new HashMap());
}

From source file:net.anymeta.Test.java

/**
 * @param args//from ww w.j a  v  a  2 s  .  c o m
 */
public static void main(String[] arg) throws AnyMetaRegistryException, AnyMetaException, JSONException {
    HashMap<String, Object> args;

    // Load the API
    AnyMetaAPI api = AnyMetaAPI.fromRegistry("pluto.local");

    // Get information for the currently logged in user.
    JSONObject o = (JSONObject) api.doMethod("anymeta.user.info");
    System.out.println("Logged in as " + o.getString("title"));

    //  
    args = new HashMap<String, Object>();
    args.put("q_kind", "PERSON");

    JSONArray a = (JSONArray) api.doMethod("query.execute", args);
    System.out.println(a.toString());
    System.out.println(a.length());

    // Lookup an RFID tag.
    args = new HashMap<String, Object>();
    ArrayList<String> ids = new ArrayList<String>();
    ids.add("65");
    ids.add("94");
    args.put("ids", ids);
    args.put("predicate", "KNOWS");

    o = (JSONObject) api.doMethod("contact.link", args);
    System.out.println(o.toString());

    // Upload an image
    args = new HashMap<String, Object>();
    args.put("mime", "image/jpeg");
    args.put("data", "@C:\\bla.jpg");

    o = (JSONObject) api.doMethod("anymeta.attachment.create", args);
    System.out.println(o.toString());

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    Document doc = factory.newDocumentBuilder().parse(new InputSource(new StringReader(getXMLData())));

    Map entityValues = new HashMap();
    getEntityValues(doc, entityValues);//from w ww .  ja  v  a2  s.  co m

    NamedNodeMap entities = doc.getDoctype().getEntities();
    for (int i = 0; i < entities.getLength(); i++) {
        Entity entity = (Entity) entities.item(i);
        System.out.println(entity);
        String entityName = entity.getNodeName();
        System.out.println(entityName);
        String entityPublicId = entity.getPublicId();
        System.out.println(entityPublicId);
        String entitySystemId = entity.getSystemId();
        System.out.println(entitySystemId);
        Node entityValue = (Node) entityValues.get(entityName);
        System.out.println(entityValue);
    }
}

From source file:TestRedundantObjectPool.java

 public static void main(String args[]) throws Exception {

   SoftReferenceObjectPool pool =/*from w  ww  .  ja  v  a  2s . co  m*/
     new SoftReferenceObjectPool(new EmployeeFactory(), 5);

   try{

      System.err.println("Number of employees in pool: " + pool.getNumIdle());

      Employee employee = (Employee)pool.borrowObject();

      System.err.println("Borrowed Employee: " + employee);

      employee.doWork();

      pool.returnObject(employee);

      // employee = null;

      HashMap map = new HashMap();

      System.err.println("Running memory intensive operation");
      for(int i = 0; i < 1000000; i++) {
         map.put(new Integer(i), new String("Fred Flintstone" + i));
      }

   }catch(OutOfMemoryError e) {
      System.err.println("Borrowed employee after OutOfMemory: " +
        pool.borrowObject());
      System.err.println("Error: "  + e);
   }
}

From source file:com.home.ln_spring.ch5.env.EnvironmentSample.java

public static void main(String[] args) {
    GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
    ctx.refresh();/*from www .  j a  va 2s .  co m*/

    ConfigurableEnvironment env = ctx.getEnvironment();
    MutablePropertySources propertySources = env.getPropertySources();
    Map appMap = new HashMap();
    appMap.put("user.home", "/home/vitaliy/NetBeansProjects/Ln_Spring");
    propertySources.addFirst(new MapPropertySource("SPRING3_MAP", appMap));

    System.out.println("user.home: " + System.getProperty("user.home"));
    System.out.println("JAVA_HOME: " + System.getProperty("JAVA_HOME"));

    System.out.println("user.home: " + env.getProperty("user.home"));
    System.out.println("JAVA_HOME: " + env.getProperty("JAVA_HOME"));
}