Example usage for java.util Collections unmodifiableMap

List of usage examples for java.util Collections unmodifiableMap

Introduction

In this page you can find the example usage for java.util Collections unmodifiableMap.

Prototype

public static <K, V> Map<K, V> unmodifiableMap(Map<? extends K, ? extends V> m) 

Source Link

Document

Returns an unmodifiable view of the specified map.

Usage

From source file:MainClass.java

public static void main(String[] s) {
    Hashtable table = new Hashtable();
    table.put("key1", "value1");
    table.put("key2", "value2");
    table.put("key3", "value3");

    Map m = Collections.unmodifiableMap(table);

    m.put("key3", "value3");

    System.out.println(m);/*from w  ww.j  a  v a  2  s  .c  o  m*/

}

From source file:Main.java

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

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

    try {/* ww w  .j a va2s. c o m*/
        map.put("key", "new value");
    } catch (UnsupportedOperationException e) {
        System.out.println(e.getMessage());
    }

}

From source file:Main.java

public static void main(String[] s) {
    Hashtable<String, String> table = new Hashtable<String, String>();
    table.put("key1", "value1");
    table.put("key2", "value2");
    table.put("key3", "value3");

    Map m = Collections.unmodifiableMap(table);

    m.put("key3", "value3");

    System.out.println(m);//from   w  w w.  j a  va 2s .  co m

}

From source file:Main.java

public static void main(String[] s) {
    //object hash table 
    Hashtable<String, String> table = new Hashtable<String, String>();

    // populate the table
    table.put("key1", "value1");
    table.put("key2", "value2");
    table.put("key3", "from java2s.com");

    System.out.println("Initial collection: " + table);

    // create unmodifiable map
    Map<String, String> m = Collections.unmodifiableMap(table);

    // try to modify the collection
    m.put("key3", "value3");
}

From source file:Main.java

public static void main(String[] s) {
    Hashtable<String, String> table = new Hashtable<String, String>();

    table.put("1", "Sunday");
    table.put("2", "Monday");
    table.put("3", "Tuesday");
    table.put("4", "Wednesday");
    table.put("5", "Thursday");
    table.put("6", "Friday");
    table.put("7", "Saturday");

    System.out.println("Initial collection: " + table);

    Map m = Collections.unmodifiableMap(table);

    // m.put("key3", "value3");
}

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 {/*w  w w  .  j  a v  a  2 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) {
    Map<Month, String> dobCalendar = Employee.persons().stream()
            .collect(Collectors.collectingAndThen(Collectors.groupingBy(p -> p.getDob().getMonth(),
                    Collectors.mapping(Employee::getName, Collectors.joining(" "))), result -> {
                        for (Month m : Month.values()) {
                            result.putIfAbsent(m, "None");
                        }//from   w  w w.j av  a2s .  c o m
                        return Collections.unmodifiableMap(new TreeMap<>(result));
                    }));

    dobCalendar.entrySet().forEach(System.out::println);
}

From source file:Main.java

public static <K, V> Map<K, V> immutableMap(Map<K, V> map) {
    return Collections.unmodifiableMap(map);
}

From source file:Main.java

public static <K, V> Map<K, V> copyNullSafeHashMap(Map<? extends K, ? extends V> map) {
    if (map.isEmpty()) {
        return Collections.emptyMap();
    }//from ww w  . j av  a  2  s . co m
    return Collections.unmodifiableMap(copyNullSafeMutableHashMap(map));
}

From source file:Main.java

public static final Map<String, String> getMap() {
    Map<String, String> map = new HashMap<String, String>();
    map.put("x", "y");
    return Collections.unmodifiableMap(map);
}