标签云

微信群

扫码加入我们

WeChat QR Code

I want to create a list of options for testing purposes. At first, I did this:ArrayList<String> places = new ArrayList<String>();places.add("Buenos Aires");places.add("Córdoba");places.add("La Plata");Then I refactored the code as follows:ArrayList<String> places = new ArrayList<String>(Arrays.asList("Buenos Aires", "Córdoba", "La Plata"));Is there a better way to do this?


If this is intended for unit testing, try groovy out for a swing. You can write your test code in it while testing java code, and use ArrasyList<String> places = ["Buenos Aires", "Córdoba", "La Plata"]

2019年05月24日17分37秒

In Java SE 7, you can substitute the parameterized type of the constructor with an empty set of type parameters (<>): Map<String, List<String>> myMap = new HashMap<>();

2019年05月23日17分37秒

See also stackoverflow.com/questions/157944/create-arraylist-from-array

2019年05月23日17分37秒

use double bracing initialization :)

2019年05月23日17分37秒

Stream.of("val1", "val2").collect(Collectors.toList()); //creates ArrayList, Java8 solution.

2019年05月24日17分37秒

See stackoverflow.com/questions/924285 for more information about the double-brace initialization, pros and cons.

2019年05月23日17分37秒

Eddie: Good call for the link -- one sentence on double-brace initialization isn't enough to describe it fully.

2019年05月23日17分37秒

Only works if you aren't relying on Auto boxing List<Double> list = [1.0, 2.0, 3.0]; fails.

2019年05月24日17分37秒

Well, just goes to show - one man's syntactic sugar is another mans syntactic diabetes. I'll stay away from these 'alternatives'.

2019年05月23日17分37秒

Since this answer is the most upvoted and mentioned Project Coin I think you should call out that java 9 shipped with List.of(...) syntax: docs.oracle.com/javase/9/docs/api/java/util/List.html#of-E...-

2019年05月24日17分37秒

Marcase: Can you not change your class to use a List instead of ArrayList?

2019年05月24日17分37秒

As per my answer, if you're not using methods specific to ArrayList, it would be better design to change the declaration to List. Specify interfaces, not implementations.

2019年05月24日17分37秒

Christoffer Hammarström: if he changes the declaration to List and uses the List<String> places = Arrays.asList(...); he will not be able to use places.add("blabla")

2019年05月23日17分37秒

Just to be clear, asList(...) returns a fixed size List that blows up on mutating operations like remove and clear, things the List contract claims to support. Even if you left declaration as List, you sill need to use List l = new ArrayList(asList(...)) in order to get an object that doesn't throw OperationNotSupported exceptions. Liskov Substitution Principle anyone?

2019年05月24日17分37秒

Splash: remove and clear are optional operations in List, so asList(...) does follow the contract. Nowhere does the OP say he needs to add more elements later, the example is just of a List that needs to be initialized with three elements.

2019年05月24日17分37秒

jollyroger: Arrays.asList is a static method. See docs.oracle.com/javase/1.5.0/docs/guide/language/…

2019年05月23日17分37秒

ChristofferHammarström In that case, my novice mind tells me that static import bares strong resemblance to global variables and the perils that pertain to such usage. Is this assumption correct and is it also the reason for the similar answer above getting more votes?

2019年05月23日17分37秒

The answer above i think you're talking about was made a year before. And no, the problem with global variables is that they are mutable. They have nothing to do with static imports.

2019年05月23日17分37秒

If you don't want the static import you can also define the full path to the static asList method, as so: List<String> strings = java.util.Arrays.asList("","");

2019年05月24日17分37秒

Or you can use import java.util.Arrays; and Arrays.asList("","");You don't have to use a static import.You don't have to use a full path.Static methods don't care about the import.They just get annoyed if you use an instance to find them.

2019年05月24日17分37秒

I'm pretty sure you can do this with just java.util.Arrays such as, List<String> names = Arrays.asList("Beckah", "Sam", "Michael");

1970年01月01日00分03秒

beckah method Arrays.asLists creates object of type List, while question is about creating ArrayList

2019年05月23日17分37秒

This method is not actually very useful and will likely be deprecated in the future.

2019年05月24日17分37秒

I don't want to add a new dependency just to do that.

2019年05月24日17分37秒

That's the same as Collections.unmodifiableList(Arrays.asList("Buenos Aires", "Córdoba", "La Plata")), which becomes unmodifiableList(asList("Buenos Aires", "Córdoba", "La Plata")) with static imports. You don't need Google Collections for this.

2019年05月24日17分37秒

No, it's not the same. As ImmutableList is documenting its immutability in the result type when unmodifiableList masquerades itas a normal List.

2019年05月23日17分37秒

Instead of the immutable one, google collections also offer mutable array list: List<String> = Lists.newArrayList("Buenos Aires", "Córdoba", "La Plata");

2019年05月24日17分37秒

You're going to pass that ImmutableList to other methods that take a List, and then you've lost that documentation anyway.

2019年05月23日17分37秒

Look back at the original post, it is asking for array initialization in one line, not 7 additional lines.

2019年05月23日17分37秒

LeoHolanda: Creating factory methods for every little thing is too much, I agree. But depending on the situation, and on the number of times that that method is going to be used, it might make sense to create it. Creating extra abstraction layers is meant to remove complexity, by creating more meaningful methods that capture the intent of the designer.

2019年05月23日17分37秒

bsd You can use this method to declare the list before entering any method. So when defining class variables, content can be added to the list without having to invoke a method to do so.

2019年05月23日17分37秒

double braces initialization should be avoided as far as possible. see : stackoverflow.com/a/924326/760393

2019年05月24日17分37秒

You can inline first expression to have compact solution: letters = Arrays.asList(new String[]{"A", "B", "C"});

2019年05月23日17分37秒

Add a brief description of your answer.

2019年05月23日17分37秒

Wrong! You can do the first line, and that is the right answer btw

2019年05月24日17分37秒

If you go through all the trouble you might as well make it a template method instead of using plain Object.

2019年05月24日17分37秒

Why is Arrays.asList() better than creating a new ArrayList in one line with add() methods?

2019年05月23日17分37秒

May I ask what this adds to the question? This answer is already covered multiple times by other answers.

2019年05月23日17分37秒

This answer does not add much to the already existing ones.

2019年05月24日17分37秒

This is actually a really bad soluition as it creates an ABSTRACT LIST. You will not be able to add anything more to the container.

2019年05月24日17分37秒

-1 Those are not the same class.

2019年05月24日17分37秒