java在类定义时对hashset的便捷初始化方法

有时候我们在类成员定义时,当这个类成员类型为 HashSet时,我们可以不方便调用 add函数进行初始化,所以可以采用下面的便捷方式来进行初始化

 1 public class MyTest{
 2     
 3      final HashSet<String> set = new HashSet<String>() {
 4         {
 5             add("a"); 
 6             add("b"); 
 7             add("c"); 
 8      9         }
10     };
11     public static void main(String[] args){
12         MyTest m = new MyTest();
13         System.out.println(m.set);
14     }
15 }
原文地址:https://www.cnblogs.com/itoy/p/5775301.html