Example usage for java.util HashMap keySet

List of usage examples for java.util HashMap keySet

Introduction

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

Prototype

public Set<K> keySet() 

Source Link

Document

Returns a Set view of the keys contained in this map.

Usage

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    MyPdfPageEventHelper tracker = new MyPdfPageEventHelper();
    writer.setPageEvent(tracker);/*from  ww  w . j a v a  2 s. c  om*/
    document.open();
    document.add(createParagraph("Fox", "Hello lazy dog."));

    document.newPage();
    HashMap lines = tracker.getLines();
    for (Iterator i = lines.keySet().iterator(); i.hasNext();) {
        String speaker = (String) i.next();
        Integer count = (Integer) lines.get(speaker);
        document.add(new Paragraph(speaker + ": " + count.intValue() + " lines."));
    }
    document.close();
}

From source file:Main.java

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

    map.put(1, 2);/*from  w  w  w .  j a va  2s  .  co  m*/
    map.put(2, 3);
    map.put(3, 4);

    for (Integer key : map.keySet()) {
        System.out.println(map.get(key));
    }
}

From source file:Main.java

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

    myMap.put("a", 1);
    myMap.put("b", 2);
    myMap.put("c", 3);

    Set<String> stStrKeys = myMap.keySet();
    Iterator<String> itrs = stStrKeys.iterator();
    while (itrs.hasNext()) {
        String s = itrs.next();//from  ww  w  . j  a va2 s.  c o m
        System.out.println(s + ": " + myMap.get(s));
    }
}

From source file:Main.java

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

    hMap.put("1", "One");
    hMap.put("2", "Two");
    hMap.put("3", "Three");

    Set st = hMap.keySet();
    Iterator itr = st.iterator();

    while (itr.hasNext())
        System.out.println(itr.next());

    // remove 2 from Set
    st.remove("2");

    System.out.println(hMap.containsKey("2"));
}

From source file:Main.java

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

    map.put("a", 1);
    map.put("b", 2);

    System.out.println("Before removal");
    for (String s : map.keySet()) {
        System.out.println(s);// w w w  . j a  v a  2  s.co  m
    }

    System.out.println("\n\nAfter removal");

    map.remove("a");
    for (String s : map.keySet()) {
        System.out.println(s);
    }
}

From source file:Main.java

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

    // populate hash map
    newmap.put(1, "tutorials");
    newmap.put(2, "from");
    newmap.put(3, "java2s.com");

    // get keyset value from map
    Set keyset = newmap.keySet();

    // check key set values
    System.out.println("Key set values are: " + keyset);
}

From source file:Main.java

public static void main(String[] args) {
    HashMap<String, Integer> map = new HashMap<String, Integer>();
    map.put("d", 5);
    map.put("c", 4);
    map.put("b", 2);
    map.put("a", 1);

    Integer value[] = new Integer[map.size()];

    Set keySet = map.keySet();
    Iterator t = keySet.iterator();
    int a = 0;/*from w  w w .j  av  a  2  s.co  m*/
    while (t.hasNext()) {
        value[a] = map.get(t.next());
        a++;
    }

    Arrays.sort(value);

    for (int i = 0; i < map.size(); i++) {
        t = keySet.iterator();
        while (t.hasNext()) {
            String temp = (String) t.next();
            if (value[i].equals(map.get(temp))) {
                System.out.println(value[i] + " = " + temp);
            }
        }
    }
}

From source file:Main.java

public static void main(String[] args) {

    HashMap<String, String> hashmap = new HashMap<String, String>();

    hashmap.put("one", "1");
    hashmap.put("two", "2");
    hashmap.put("three", "3");
    hashmap.put("four", "4");
    hashmap.put("five", "5");
    hashmap.put("six", "6");

    Iterator<String> keyIterator = hashmap.keySet().iterator();
    Iterator<String> valueIterator = hashmap.values().iterator();

    while (keyIterator.hasNext()) {
        System.out.println("key: " + keyIterator.next());
    }/*from ww w . j  a  v  a 2 s  .  com*/

    while (valueIterator.hasNext()) {
        System.out.println("value: " + valueIterator.next());
    }
}

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);
    }//w w  w .j av  a2 s .co m

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

From source file:at.tuwien.ifs.somtoolbox.apps.VisualisationImageSaver.java

public static void main(String[] args) {
    JSAPResult res = OptionFactory.parseResults(args, OPTIONS);

    String uFile = res.getString("unitDescriptionFile");
    String wFile = res.getString("weightVectorFile");
    String dwmFile = res.getString("dataWinnerMappingFile");
    String cFile = res.getString("classInformationFile");
    String vFile = res.getString("inputVectorFile");
    String tFile = res.getString("templateVectorFile");
    String ftype = res.getString("filetype");
    boolean unitGrid = res.getBoolean("unitGrid");

    String basename = res.getString("basename");
    if (basename == null) {
        basename = FileUtils.extractSOMLibInputPrefix(uFile);
    }//w ww. ja v a2s  . co m
    basename = new File(basename).getAbsolutePath();
    int unitW = res.getInt("width");
    int unitH = res.getInt("height", unitW);

    String[] vizs = res.getStringArray("vis");

    GrowingSOM gsom = null;
    CommonSOMViewerStateData state = CommonSOMViewerStateData.getInstance();
    try {
        SOMLibFormatInputReader inputReader = new SOMLibFormatInputReader(wFile, uFile, null);
        gsom = new GrowingSOM(inputReader);

        SharedSOMVisualisationData d = new SharedSOMVisualisationData(cFile, null, null, dwmFile, vFile, tFile,
                null);
        d.readAvailableData();
        state.inputDataObjects = d;
        gsom.setSharedInputObjects(d);

        Visualizations.initVisualizations(d, inputReader, 0, Palettes.getDefaultPalette(),
                Palettes.getAvailablePalettes());

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.exit(1);
    } catch (SOMLibFileFormatException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.exit(1);
    }

    if (ArrayUtils.isEmpty(vizs)) {
        System.out.println("No specific visualisation specified - saving all available visualisations.");
        vizs = Visualizations.getReadyVisualizationNames();
        System.out.println("Found " + vizs.length + ": " + Arrays.toString(vizs));
    }

    for (String viz : vizs) {
        BackgroundImageVisualizerInstance v = Visualizations.getVisualizationByName(viz);
        if (v == null) {
            System.out.println("Visualization '" + viz + "' not found!");
            continue;
        }
        BackgroundImageVisualizer i = v.getVis();

        GrowingLayer layer = gsom.getLayer();
        try {
            int height = unitH * layer.getYSize();
            int width = unitW * layer.getXSize();
            HashMap<String, BufferedImage> visualizationFlavours = i.getVisualizationFlavours(v.getVariant(),
                    gsom, width, height);
            ArrayList<String> keys = new ArrayList<String>(visualizationFlavours.keySet());
            Collections.sort(keys);

            // if the visualisation has more than 5 flavours, we create a sub-dir for it
            String subDirName = "";
            String oldBasename = basename; // save original base name for later
            if (keys.size() > 5) {
                String parentDir = new File(basename).getParentFile().getPath(); // get the parent path
                String filePrefix = basename.substring(parentDir.length()); // end the file name prefix
                subDirName = parentDir + File.separator + filePrefix + "_" + viz + File.separator; // compose a new
                // subdir name
                new File(subDirName).mkdir(); // create the dir
                basename = subDirName + filePrefix; // and extend the base name by the subdir
            }
            for (String key : keys) {
                File out = new File(basename + "_" + viz + key + "." + ftype);
                System.out.println("Generating visualisation '" + viz + "' as '" + out.getPath() + "'.");
                BufferedImage image = visualizationFlavours.get(key);
                if (unitGrid) {
                    VisualisationUtils.drawUnitGrid(image, gsom, width, height);
                }
                ImageIO.write(image, ftype, out);
            }
            basename = oldBasename; // reset base name
        } catch (SOMToolboxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    System.exit(0);
}