Initialization of an ArrayList in one line

后端 未结 30 2066
北恋
北恋 2020-11-22 01:10

I wanted to create a list of options for testing purposes. At first, I did this:

ArrayList places = new ArrayList();
places.add(\         


        
相关标签:
30条回答
  • 2020-11-22 02:02

    Why not make a simple utility function that does this?

    static <A> ArrayList<A> ll(A... a) {
      ArrayList l = new ArrayList(a.length);
      for (A x : a) l.add(x);
      return l;
    }
    

    "ll" stands for "literal list".

    ArrayList<String> places = ll("Buenos Aires", "Córdoba", "La Plata");
    
    0 讨论(0)
  • 2020-11-22 02:04

    With java-9 and above, as suggested in JEP 269: Convenience Factory Methods for Collections, this could be achieved using collection literals now with -

    List<String> list = List.of("A", "B", "C");
    
    Set<String> set = Set.of("A", "B", "C");
    

    A similar approach would apply to Map as well -

    Map<String, String> map = Map.of("k1", "v1", "k2", "v2", "k3", "v3")
    

    which is similar to Collection Literals proposal as stated by @coobird. Further clarified in the JEP as well -


    Alternatives

    Language changes have been considered several times, and rejected:

    Project Coin Proposal, 29 March 2009

    Project Coin Proposal, 30 March 2009

    JEP 186 discussion on lambda-dev, January-March 2014

    The language proposals were set aside in preference to a library-based proposal as summarized in this message.

    Related: What is the point of overloaded Convenience Factory Methods for Collections in Java 9

    0 讨论(0)
  • 2020-11-22 02:04

    Like Tom said:

    List<String> places = Arrays.asList("Buenos Aires", "Córdoba", "La Plata");
    

    But since you complained of wanting an ArrayList, you should firstly know that ArrayList is a subclass of List and you could simply add this line:

    ArrayList<String> myPlaces = new ArrayList(places);
    

    Although, that might make you complain of 'performance'.

    In that case it doesn't make sense to me, why, since your list is predefined it wasn't defined as an array (since the size is known at time of initialisation). And if that's an option for you:

    String[] places = {"Buenos Aires", "Córdoba", "La Plata"};
    

    In case you don't care of the minor performance differences then you can also copy an array to an ArrayList very simply:

    ArrayList<String> myPlaces = new ArrayList(Arrays.asList(places));
    

    Okay, but in future you need a bit more than just the place name, you need a country code too. Assuming this is still a predefined list which will never change during run-time, then it's fitting to use an enum set, which would require re-compilation if the list needed to be changed in the future.

    enum Places {BUENOS_AIRES, CORDOBA, LA_PLATA}
    

    would become:

    enum Places {
        BUENOS_AIRES("Buenos Aires",123),
        CORDOBA("Córdoba",456),
        LA_PLATA("La Plata",789);
    
        String name;
        int code;
        Places(String name, int code) {
          this.name=name;
          this.code=code;
        }
    }
    

    Enum's have a static values method that returns an array containing all of the values of the enum in the order they are declared, e.g.:

    for (Places p:Places.values()) {
        System.out.printf("The place %s has code %d%n",
                      p.name, p.code);
    }
    

    In that case I guess you wouldn't need your ArrayList.

    P.S. Randyaa demonstrated another nice way using the static utility method Collections.addAll.

    0 讨论(0)
  • 2020-11-22 02:04

    Actually, it's possible to do it in one line:

    Arrays.asList(new MyClass[] {new MyClass("arg1"), new MyClass("arg2")})
    
    0 讨论(0)
  • 2020-11-22 02:05

    Here is another way:

    List<String> values = Stream.of("One", "Two").collect(Collectors.toList());
    
    0 讨论(0)
  • 2020-11-22 02:06

    Yes with the help of Arrays you can initialize array list in one line,

    List<String> strlist= Arrays.asList("aaa", "bbb", "ccc");
    
    0 讨论(0)
提交回复
热议问题