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:Counter.java

public static void main(String[] args) {
    Map hm = new HashMap();
    for (int i = 0; i < 10000; i++) {
        // Produce a number between 0 and 20:
        Integer r = new Integer(rand.nextInt(20));
        if (hm.containsKey(r))
            ((Counter) hm.get(r)).i++;/* w w  w  . j a v a  2  s.c om*/
        else
            hm.put(r, new Counter());
    }
    System.out.println(hm);
}

From source file:Main.java

License:asdf

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

    map.put("KeyA", "erty");
    map.put("KeyC", "trwe");
    map.put("KeyD", "wert");
    map.put("KeyB", "fdsd");
    map.put("KeyS", "dsa");
    map.put("KeyE", "fdas");
    map.put("KeyG", "asdf");

    System.out.println(sort(map, new Comparator<String>() {
        @Override//from w  w w .j a va  2s  .  c  o m
        public int compare(String o1, String o2) {
            return o1.compareTo(o2);
        }
    }));
}

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]);
    }//w ww  .  ja va2 s  .c o  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) {
    Map<String, Student> map = new HashMap<>();
    map.put("s1", new Student(5, "A"));
    map.put("s2", new Student(4, "B"));
    map.put("s3", new Student(10, "C"));
    map.put("s4", new Student(2, "D"));
    Collection<Student> students = map.values();
    List<Student> list = new ArrayList<>(students);
    Collections.sort(list, new MyComparator());

    for (Iterator<Student> it = list.iterator(); it.hasNext();) {
        Student stdn = (Student) it.next();
        System.out.println("Student id : " + stdn.id);
        System.out.println("Student Name : " + stdn.name);
    }/*from   ww w  . j  ava 2 s  .  co  m*/
}

From source file:Main.java

public static void main(String[] args) {
    Map<String, String> unsortMap = new HashMap<String, String>();
    unsortMap.put("1", "1");
    unsortMap.put("2", "A");
    unsortMap.put("3", "2");
    Iterator iterator = unsortMap.entrySet().iterator();
    for (Map.Entry entry : unsortMap.entrySet()) {
        System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue());
    }//w  ww  . j  a v  a  2  s . c  o m
    Map<String, String> sortedMap = sortByComparator(unsortMap);
    for (Map.Entry entry : sortedMap.entrySet()) {
        System.out.println("Key : " + entry.getKey() + " Value : " + entry.getValue());
    }
}

From source file:Test.java

public static void main(String[] args) throws Exception {
    Map<String, String> attributes = new HashMap<>();
    attributes.put("create", "true");
    URI zipFile = URI.create("jar:file:/home.zip");
    try (FileSystem zipFileSys = FileSystems.newFileSystem(zipFile, attributes);) {
        Path path = zipFileSys.getPath("docs");
        Files.createDirectory(path);
        try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(zipFileSys.getPath("/"));) {
            for (Path file : directoryStream) {
                System.out.println(file.getFileName());
            }//  w  w w .  ja v a 2  s  .  c o  m
        }
    }
}

From source file:Main.java

public static void main(String[] args) throws IllegalAccessException {
    Class clazz = Color.class;
    Field[] colorFields = clazz.getDeclaredFields();

    HashMap<String, Color> singleColors = new HashMap<String, Color>();
    for (Field cf : colorFields) {
        int modifiers = cf.getModifiers();
        if (!Modifier.isPublic(modifiers))
            continue;

        Color c = (Color) cf.get(null);
        if (!singleColors.values().contains(c))
            singleColors.put(cf.getName(), c);
    }// ww w. j a  v a 2 s .  c om

    for (String k : singleColors.keySet()) {
        System.out.println(k + ": " + singleColors.get(k));
    }
}

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);//from w  ww.j  a va 2  s. c  o m
        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[] argv) {
    List collection = new ArrayList();

    // For a set or list
    for (Iterator it = collection.iterator(); it.hasNext();) {
        Object element = it.next();
    }// ww  w  .j  ava  2s.  c om
    Map map = new HashMap();
    // For keys of a map
    for (Iterator it = map.keySet().iterator(); it.hasNext();) {
        Object key = it.next();
    }

    // For values of a map
    for (Iterator it = map.values().iterator(); it.hasNext();) {
        Object value = it.next();
    }

    // For both the keys and values of a map
    for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
        Map.Entry entry = (Map.Entry) it.next();
        Object key = entry.getKey();
        Object value = entry.getValue();
    }
}

From source file:CopyPDFFileAndAddMetaData.java

public static void main(String[] args) {
    try {/* w  w w  .ja va 2  s .  c  om*/
        PdfReader reader = new PdfReader("YourOwnPDF.pdf");

        PdfStamper stamp = new PdfStamper(reader, new FileOutputStream("NewPDFFileFromPdfStamper.pdf"));

        HashMap<String, String> moreInfo = new HashMap<String, String>();
        moreInfo.put("Author", "YourName");
        moreInfo.put("Title", "YourTitle");
        moreInfo.put("Subject", "YourSubject");
        stamp.setMoreInfo(moreInfo);
        stamp.close();

    } catch (Exception de) {
        de.printStackTrace();
    }
}