⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 e348. making a collection read-only.txt

📁 这里面包含了一百多个JAVA源文件
💻 TXT
字号:
Making a collection read-only involves wrapping the collection in another object whose mutation methods all throw UnsupportedOperationException. 
    List stuff = Arrays.asList(new String[]{"a", "b"});
    
    // Make a list read-only
    List list = new ArrayList(stuff);
    list = Collections.unmodifiableList(list);
    
    try {
        // Try modifying the list
        list.set(0, "new value");
    } catch (UnsupportedOperationException e) {
        // Can't modify
    }
    
    // Make a set read-only
    Set set = new HashSet(stuff);
    set = Collections.unmodifiableSet(set);
    
    // Make a map read-only
    Map map = new HashMap();
    // Add key/value pairs ...
    map = Collections.unmodifiableMap(map);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -