java中unmodifiableList方法的应用场景
2024-09-04 17:06:42
java对象中primitive类型变量可以通过不提供set方法保证不被修改,但对象的List成员在提供get方法后,就可以随意add、remove改变其结构,这不是希望的结果。网上看了下,发现Collections的静态方法unmodifiableList可以达到目的。方法原型为:public static <T> List<T> unmodifiableList(List<? extends T> list);用法也很简单,传入一个List实例la,返回这个list的只读视图lb,类型依然是List。之后对lb进行add、remove等改变其内容的操作将导致编译不通过。
首先举例描述问题:
Student.java
package com.liulei.test; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; /**
* Created by Liulei on 2017/5/31.
*/
public class Student {
private String name;
private int age;
private List<String> courses; public Student(){
courses = new ArrayList<String>();
}
public Student(String name,int age,List<String> courses){
this.name = name;
this.age = age;
this.courses = courses;
} public List<String> getCourses(){
return this.courses;
} public void setName(String name) {
this.name = name;
} public void setAge(int age) {
this.age = age;
} public String getName() {
return name;
} public int getAge() {
return age;
} public void describe(){
System.out.println(this.name);
System.out.println(this.age);
for (String course:courses){
System.out.println(course);
}
} }
App.java
package com.liulei.test; import java.util.ArrayList;
import java.util.List; /**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
ArrayList<String> courses = new ArrayList<String>();
courses.add("Math");
courses.add("Chinese"); Student student = new Student("Alice",18,courses);
student.describe();
List<String> myCourses = student.getCourses();
myCourses.add("English");
student.describe(); }
}
执行结果:
Alice
18
Math
Chinese
Alice
18
Math
Chinese
English
虽然只有getCourse,但依然可以被加上1门English。使用unmodifiableList可以解决这个问题,将Student的getCourses改写:
public List<String> getCourses(){
return Collections.unmodifiableList(this.courses);
}
再次执行,编译器提示出错:
Exception in thread "main" java.lang.UnsupportedOperationException
at java.util.Collections$UnmodifiableCollection.add(Collections.java:1055)
总结,使用unmodifiableList可以保证对象的list内容不被意料之外地修改,保证对象的封装性。
最新文章
- JavaScript特性(attribute)、属性(property)和样式(style)
- Spring用代码来读取properties文件
- java的异常
- springmvc spring mybatis插入mysql中文乱码
- SourceInsight
- VC++ 控制外部程序,向外部程序发送一个消息的方法
- js webapp 滑动事件
- 找不到方法";Boolean System.Threading.WaitHandle.WaitOne(TimeSpan)";的解决方案
- (经常看看)jdk 设计模式
- godaddy.com 注册域名 买卖域名
- ASP.NET MVC上传文件的几种方法
- JS中表格的全选和删除要注意的问题
- Dockerfile的书写规则及指令使用方法
- bzoj4830 hnoi2017 抛硬币
- Supervisor守护DotNet Core控制台程序
- 使用Visual Studio Code开发.NET Core看这篇就够了
- mac 安装robot framework报错:No matching distribution found for Pywin32
- oracle SQL性能分析之10053事件
- projective dynamics的global solve中 引入拉格朗日乘子的简化方法
- Reactor反应器模式 (epoll)
热门文章
- MBProgressHUD 优雅地去提示
- HDU.2561 第二小整数(water)
- 复用传统C/S架构系统,升级成‘伪’B/S架构设计
- Redis ---------- Sort Set排序集合类型
- thymelef模板报错 the entity name must immediately follow the &#39;&;&#39; in the entity reference
- 深入理解yii2之RBAC(模块化系统)
- TP3.2.3 页面跳转后 Cookie 失效 —— 参考解决方案
- R-biomaRt使用-代码备份
- SpringMVC使用注解@RequestMapping映射请求
- Android 渗透小知识点