Example usage for java.util Map containsKey

List of usage examples for java.util Map containsKey

Introduction

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

Prototype

boolean containsKey(Object key);

Source Link

Document

Returns true if this map contains a mapping for the specified key.

Usage

From source file:Counter.java

public static void main(String[] args) {
    Map hm = new HashMap();
    for (int i = 0; i < 10; i++) {
        Integer r = new Integer(20);
        if (hm.containsKey(r))
            ((Counter) hm.get(r)).i++;/*from  w ww  .j a  va 2 s  .  c  o  m*/
        else
            hm.put(r, new Counter());
    }
    System.out.println(hm);
}

From source file:Main.java

public static void main(String[] args) {
    String yourString = "UNDERSTAND";
    Map<Character, Integer> count = new TreeMap<Character, Integer>();
    for (char c : yourString.toCharArray()) {
        if (count.containsKey(c)) {
            count.put(c, (int) count.get(c) + 1);
        } else {//from  ww  w  . ja  va  2  s. c o m
            count.put(c, 1);
        }
    }
    System.out.println(count);
}

From source file:Main.java

public static void main(String args[]) {

    String s = "this is a test this is a test";
    String[] splitted = s.split(" ");
    Map<String, Integer> hm = new HashMap<String, Integer>();

    for (int i = 0; i < splitted.length; i++) {
        if (hm.containsKey(splitted[i])) {
            int cont = hm.get(splitted[i]);
            hm.put(splitted[i], cont + 1);
        } else {//w ww.  ja  v a2s.c  o  m
            hm.put(splitted[i], 1);
        }
    }
    System.out.println(hm);
}

From source file:MainClass.java

public static void main(String[] a) {

    Map map = new HashMap();
    map.put("key1", "value1");
    map.put("key2", "value2");
    map.put("key3", "value3");

    System.out.println(map.containsKey("key1"));
    System.out.println(map.containsValue("value2"));
}

From source file:Main.java

public static void main(String[] args) {

    String sentence = "is this a sentence this is a test";
    String[] myStringArray = sentence.split("\\s"); // Split the sentence by
                                                    // space.

    Map<String, Integer> wordOccurrences = new HashMap<String, Integer>(myStringArray.length);

    for (String word : myStringArray) {
        if (wordOccurrences.containsKey(word)) {
            wordOccurrences.put(word, wordOccurrences.get(word) + 1);
        } else {/*ww w  .  j  a v a  2s . c o  m*/
            wordOccurrences.put(word, 1);
        }
    }
    for (String word : wordOccurrences.keySet()) {
        if (wordOccurrences.get(word) > 1) {
            System.out.println("1b. - Tokens that occurs more than once: " + word + "\n");
        }
    }
}

From source file:Main.java

public static void main(String[] a) {
    Map<String, String> map = new HashMap<String, String>();
    map.put("key1", "value1");
    map.put("key2", "value2");
    map.put("key3", "value3");

    System.out.println(map.containsKey("key1"));
    System.out.println(map.containsValue("value2"));
}

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++;/*from  w  w  w. j  a v a  2s. c  o m*/
        else
            hm.put(r, new Counter());
    }
    System.out.println(hm);
}

From source file:Main.java

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

    String blah = "aaaabbbbddd";

    for (int i = 0; i < blah.length(); i++) {
        char c = blah.charAt(i);
        if (!map.containsKey(c)) {
            map.put(c, 1);/*from  ww w. ja v  a  2  s .  co m*/
        } else {
            map.put(c, (map.get(c) + 1));
        }
    }

    for (Map.Entry<Character, Integer> entry : map.entrySet()) {
        System.out.print(entry.getKey() + "" + entry.getValue());
    }
}

From source file:Weak.java

public static void main(String args[]) {
    final Map map = new WeakHashMap();
    map.put(new String("Java2s"), "www.java2s.com");
    Runnable runner = new Runnable() {
        public void run() {
            while (map.containsKey("Java2s")) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException ignored) {
                }//  ww  w .j  av a  2s  .c  o  m
                System.out.println("Waiting");
                System.gc();
            }
        }
    };
    Thread t = new Thread(runner);
    t.start();
    System.out.println("Main waiting");
    try {
        t.join();
    } catch (InterruptedException ignored) {
    }
}

From source file:Main.java

public static void main(String args[]) {
    final Map<String, String> map = new WeakHashMap<String, String>();
    map.put(new String("A"), "B");
    Runnable runner = new Runnable() {
        public void run() {
            while (map.containsKey("A")) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException ignored) {
                }/*  ww  w .  ja  v  a2  s.co m*/
                System.gc();
            }
        }
    };

    Thread t = new Thread(runner);
    t.start();
    try {
        t.join();
    } catch (InterruptedException ignored) {
    }
}