how to initialize static ArrayList in one line

后端 未结 6 399
醉话见心
醉话见心 2020-12-24 12:50

i have MyClass as

MyClass(String, String, int);

i know about how to add to add to ArrayList in this way:

MyClass.name = \"N         


        
相关标签:
6条回答
  • 2020-12-24 13:01

    You can do

    List<MyClass> list = Arrays.asList(
                             new MyClass("Name", "Address", age),
                             // many more
                         );
    

    Note: this will create a list where you can't change its size.

    0 讨论(0)
  • 2020-12-24 13:14

    If what you want is having the cleanest and simplest initialization, you might have this :

        List<MyClass> list = MyClass.build(
            "Name", "Address", 51,  
            "a2", "c", 4,
            "v", "d", 2
        );
    

    This uses this utility method :

    public static List<MyClass> build(Object... array) {
        List<MyClass> list = new ArrayList<Test>();
        for (int i=0; i<array.length-2; i+=3) {
            list.add(new MyClass((String)array[i], (String)array[i+1], (Integer)array[i+2]));
        }
        return list;
    }
    
    0 讨论(0)
  • 2020-12-24 13:15

    Something like this:

    List<MyClass> list = new ArrayList<MyClass>(Arrays.asList(new MyClass[] {new MyClass("Name", "Address", age}));
    

    Of course, your class must have a constructor like this:

    public MyClass(String name, String address, int age) {
    ...
    }
    
    0 讨论(0)
  • 2020-12-24 13:18

    You can use double braces initialization: -

    List<MyClass> list = new ArrayList<MyClass>() {
        {
            add(new MyClass("name", "address", 23));
            add(new MyClass("name2", "address2", 45));
        }
    };
    

    As you can see that, inner braces is just like an initializer block, which is used to initialize the list in one go..

    Also note the semi-colon at the end of your double-braces

    0 讨论(0)
  • 2020-12-24 13:20

    The "best" way to do this in an effective way would be to :

    List<MyClass> list = new ArrayList<MyClass>();
    
     list.add(new MyClass("name", "address", 23));
     list.add(new MyClass("name2", "address2", 45));
    

    although it requires a lot of typing but as you can clearly see this is more efficient

    Another alternative would be to use google guava (not tested for efficiency):

    ArrayList<MyClass> list = new ArrayList<MyClass>(
               new MyClass("name", "address", 23), 
               new MyClass("name2", "address2", 45) );
    

    The required import is import static com.google.common.collect.Lists.newArrayList;

    also, you can use double braces initialization as originally proposed by @Rohit Jain: -

    List<MyClass> list = new ArrayList<MyClass>() {
        {
            add(new MyClass("name", "address", 23));
            add(new MyClass("name2", "address2", 45));
        }
    };
    

    As you can see that, inner braces is just like an initializer block, which is used to initialize the list in one go..

    note the semi-colon at the end of your double-braces

    also note the last method has some downsides as discussed here.

    0 讨论(0)
  • 2020-12-24 13:22

    You can instantiate ArrayList like so:

    new ArrayList<myclass>() {{
      add(new MyClass("Name", "Address", age));
    }};
    

    This creates an anonymous inner class that actually extends ArrayList, with an initialiser block that calls add. This is obviously completely filthy and will make your colleagues want to hurt you, so you should use Arrays.asList instead. :)

    0 讨论(0)
提交回复
热议问题