Daily Note: About arrays Aslist and collections Some understanding of unmodifiable list

1. Create a list normally and operate the list

List<Integer> collect = Stream.of(1,3,5,7,9).collect(Collectors.toList());
		//第一位改变为9
		collect.set(0,9);
		//尾部插入一个值
		collect.add(99);
		collect.forEach(System.out::println);

//output
9
3
5
7
9
99

2. Sometimes, in order to create a list more conveniently, use arrays asList

2.1 occurrence

List<Integer> asList = Arrays.asList(1,9);
		asList.set(0,9);
		//asList.add(99);
		asList.forEach(System.out::println);

//output
9
3
5
7
9
   
//把上述注释放开
 oops~报错了

2.2 cause analysis

Click the aslist method directly, check the source code, and find that this is through arrays The list created by aslist inherits the abstractlist

By analyzing the source code, it is found that the ArrayList at this time has the reuse of set, but there is no reuse of the add method at that time, so the parent method is adopted by default. Let's click on the parent analysis

We finally found this method, so that's why we use arrays The list created by aslist uses set. If you use the add method, an error will be reported

2.3 solutions

//使用new ArrayList()包装一下

3. In other cases, if you want to create an immutable list, you can't change the value of set or add, and it's "read-only"

3.1 occurrence

3.2 cause analysis

Using collections After the unmodifiablelist method wraps the list, the regenerated list cannot be modified. Let's observe this through the source code

It can be found that exceptions will be thrown whether it is set, add, or even remove or sort

In other words, the list collection obtained by this wrapping method is read-only and immutable, just as the method name says

3.3 applicable scenarios

Simulate a large shopping mall, where customers choose the goods they want to buy and go to the salesperson for settlement

Abstract out two roles: customer and salesperson

/**
 * 模拟顾客购买商品的流程
 *
 * @author Amg
 * @date 2021/9/8 10:01
 */
public class Customer {
	
	private List<Long> ids;
	
	Customer() {
		ids = new ArrayList<>();
	}
	
	/**
	 * 添加商品
	 * @param goodsId	商品主键id
	 */
	public void add(Long goodsId) {
		ids.add(goodsId);
	}
	
	/**
	 * 移除商品
	 * @param goodsId	商品主键id
	 */
	public void remove(Long goodsId) {
		ids.remove(goodsId);
	}
	
	/**
	 * 返回最终的商品列表
	 * @return	List<Long>
	 */
	public List<Long> getIds() {
		return ids;
	}
}
/**
 * 售货员
 * @author Amg
 * @date 2021/9/8 10:18
 */
public class Sales {
	
	private List<Long> list;
	
	public Sales(List<Long> goodsId) {
		list = goodsId;
	}
	/**
	* 结算
	*/
	public double countPrice() {
		
		double price = 0;
		for (Long id : list) {
			//根据list里面的商品获取价格,这里简单先模拟
			
			if (id % 2 == 0) {
				price += 1;
			} else {
				price += 2;
			}
		}
		return price;
	}
}
/**
 * 商场客户端
 * @author Amg
 * @date 2021/9/8 10:17
 */
public class Client {
	
	public static void main(String[] args) {
		
		Customer customer = new Customer();
		//添加商品
		customer.add(123L);
		customer.add(456L);
		customer.add(789L);
		
		//移除商品
		customer.remove(123L);
		
		List<Long> ids = customer.getIds();
		Sales sales = new Sales(ids);
		System.out.println(sales.countPrice());
	}
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>