Initialization of List in a JSF Managed bean

后端 未结 5 943
闹比i
闹比i 2020-12-01 13:19

I\' have a question about initialization of List in the POJO as it follows the next code:

public class Person {

 //other fields...
 private List

        
5条回答
  •  粉色の甜心
    2020-12-01 13:40

    I would suggest this:

    public class Person {
         //other fields...
         private List friends=new ArrayList<>();
    
         // returns a copy to protect original list
         public List getFriends() {
            Collections.unmodifiableList(new ArrayList<>(friends));
         }
         public void addFriend(String> friend) {
            this.friends.add(friend);
         }
         public void addFriends(List friends) {
            this.friends.addAll(friends);
         }
    }
    

提交回复
热议问题