Initialization of an ArrayList in one line

后端 未结 30 2202
北恋
北恋 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 01:48
    List<String> names = Arrays.asList("2","@2234","21","11");
    
    0 讨论(0)
  • 2020-11-22 01:51

    (Should be a comment, but too long, so new reply). As others have mentioned, the Arrays.asList method is fixed size, but that's not the only issue with it. It also doesn't handle inheritance very well. For instance, suppose you have the following:

    class A{}
    class B extends A{}
    
    public List<A> getAList(){
        return Arrays.asList(new B());
    }
    

    The above results in a compiler error, because List<B>(which is what is returned by Arrays.asList) is not a subclass of List<A>, even though you can add Objects of type B to a List<A> object. To get around this, you need to do something like:

    new ArrayList<A>(Arrays.<A>asList(b1, b2, b3))
    

    This is probably the best way to go about doing this, esp. if you need an unbounded list or need to use inheritance.

    0 讨论(0)
  • 2020-11-22 01:55

    Collection literals didn't make it into Java 8, but it is possible to use the Stream API to initialize a list in one rather long line:

    List<String> places = Stream.of("Buenos Aires", "Córdoba", "La Plata").collect(Collectors.toList());
    

    If you need to ensure that your List is an ArrayList:

    ArrayList<String> places = Stream.of("Buenos Aires", "Córdoba", "La Plata").collect(Collectors.toCollection(ArrayList::new));
    
    0 讨论(0)
  • With Eclipse Collections you can write the following:

    List<String> list = Lists.mutable.with("Buenos Aires", "Córdoba", "La Plata");
    

    You can also be more specific about the types and whether they are Mutable or Immutable.

    MutableList<String> mList = Lists.mutable.with("Buenos Aires", "Córdoba", "La Plata");
    ImmutableList<String> iList = Lists.immutable.with("Buenos Aires", "Córdoba", "La Plata");
    

    You can also do the same with Sets and Bags:

    Set<String> set = Sets.mutable.with("Buenos Aires", "Córdoba", "La Plata");
    MutableSet<String> mSet = Sets.mutable.with("Buenos Aires", "Córdoba", "La Plata");
    ImmutableSet<String> iSet = Sets.immutable.with("Buenos Aires", "Córdoba", "La Plata");
    
    Bag<String> bag = Bags.mutable.with("Buenos Aires", "Córdoba", "La Plata");
    MutableBag<String> mBag = Bags.mutable.with("Buenos Aires", "Córdoba", "La Plata");
    ImmutableBag<String> iBag = Bags.immutable.with("Buenos Aires", "Córdoba", "La Plata");
    

    Note: I am a committer for Eclipse Collections.

    0 讨论(0)
  • 2020-11-22 01:56

    You can use StickyList from Cactoos:

    List<String> names = new StickyList<>(
      "Scott Fitzgerald", "Fyodor Dostoyevsky"
    );
    
    0 讨论(0)
  • 2020-11-22 01:56

    The best way to do it:

    package main_package;
    
    import java.util.ArrayList;
    
    
    public class Stackkkk {
        public static void main(String[] args) {
            ArrayList<Object> list = new ArrayList<Object>();
            add(list, "1", "2", "3", "4", "5", "6");
            System.out.println("I added " + list.size() + " element in one line");
        }
    
        public static void add(ArrayList<Object> list,Object...objects){
            for(Object object:objects)
                list.add(object);
        }
    }
    

    Just create a function that can have as many elements as you want and call it to add them in one line.

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