课程: Java Memory Management: Values and References

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Avoiding escaping collection references

Avoiding escaping collection references

- [Instructor] So, we just created a copy to deal with escaping references of collections. But we can also do something different. We can create an unmodifiable collection. This way we are using a special method, actually, one of the special methods, to create a collection that can not be altered. This will solve the problem for collections with immutable objects as a build in Java solution. This way the collections can not be modified. And if there are immutable objects on there, well, they can not be changed anyways. So there's no problem anymore. But it doesn't solve the escaping references of immutable objects on collections though. Let's have a look at how to create unmodifiable collections in IntelliJ. So, where we created the copy we now just send all the product's objects to a new array list. I'm going to do something different right now. I'm going to be returning an unmodifiable list. And I can use the list or the collection's interface to do this. Let me show you two ways of…

内容