Collections.emptyList() does not need to create a new object for each call; it's typical, as in OpenJDK, to just return the singleton EMPTY_LIST object. Additionally, it's clearer that you intend to mean an empty list rather than having forgotten to fill in a placeholder.
Use emptyList(); it's both faster (up to Java target level 1.9) and more readable.
Collections.emptyList() does not need to create a new object for each call; it's typical, as in OpenJDK, to just return the singleton EMPTY_LIST object. Additionally, it's clearer that you intend to mean an empty list rather than having forgotten to fill in a placeholder.
Use emptyList(); it's both faster (up to Java target level 1.9) and more readable.
What is the preferred Java 9 way of pointing to an empty and immutable list?
The difference is rather subtle so "preferred" depends on what you want to achieve. Some behavioral differences:
List.ofwill throw an exception oncontains(null)invocations.- You can deserialize
emptyList()on JDK 8 and previous, but notList.of.
In terms or conveying that you want an empty list, emptyList() might look better, but this is just a temporary convention. If developers start using List.of() (which is much shorter than Collections.emptyList()) then it will become a known and accepted way, it's just new. If you think about it, there are some constructs we use which do not always convey what they do by themselves, but we got accustomed to them.
So there is no strictly preferred way. If the behavior does not matter use whatever you want.
To represent an Empty List in Java (an ArrayList is a List), we use java.util.Collections.emptyList().
So you could use something like this:
Foo foo = new Foo("boo", (ArrayList) java.util.Collections.emptyList());
In case you need other ways to create a new list in java, see this answer How to make a new List in Java.
Now you may have mixed up Arrays with ArrayList, resulting in usage of an array like syntax, [], to create an ArrayList. But that too is off mark from the language specifications, and you can read more about how to create arrays from SO here, or as is always better, the standard java docs.
And, not related to your question, but you should always code to an interface.
Your class Foo has a wrong constructor name, the correct should be:
class Foo {
String ide;
ArrayList list;
public Foo(String ide, ArrayList list){
this.ide = ide;
this.list = list;
}
}
Your first solution is wrong because you are not passing an instance of ArrayList to the constructor. In order to create an empty ArrayList, you may use:
Foo one = new Foo("boo", new ArrayList());
You may rewrite the Foo class to use the generic type of List:
class Foo {
String ide;
List list;
public Foo(String ide, List list){
this.ide = ide;
this.list = list;
}
}
... and use the java.util.Collections.EMPTY_LIST to represent empty lists:
Foo one = new Foo("boo", Collections.EMPTY_LIST);
To represent an Empty List in Java (an ArrayList is a List), we use java.util.Collections.emptyList().
So you could use something like this:
Foo foo = new Foo("boo", (ArrayList) java.util.Collections.emptyList());
In case you need other ways to create a new list in java, see this answer How to make a new List in Java.
Now you may have mixed up Arrays with ArrayList, resulting in usage of an array like syntax, [], to create an ArrayList. But that too is off mark from the language specifications, and you can read more about how to create arrays from SO here, or as is always better, the standard java docs.
And, not related to your question, but you should always code to an interface.
Your class Foo has a wrong constructor name, the correct should be:
class Foo {
String ide;
ArrayList list;
public Foo(String ide, ArrayList list){
this.ide = ide;
this.list = list;
}
}
Your first solution is wrong because you are not passing an instance of ArrayList to the constructor. In order to create an empty ArrayList, you may use:
Foo one = new Foo("boo", new ArrayList());
You may rewrite the Foo class to use the generic type of List:
class Foo {
String ide;
List list;
public Foo(String ide, List list){
this.ide = ide;
this.list = list;
}
}
... and use the java.util.Collections.EMPTY_LIST to represent empty lists:
Foo one = new Foo("boo", Collections.EMPTY_LIST);
List is an interface and cannot be directly created. Instead you create an object based on a class that implements the methods in that interface. There are many classes that implement List, but a common one is ArrayList, which is probably what you want:
List<Categories> catList = new ArrayList<Categories>();
Note that it is still considered good practice to use the interface on the left side of this statement.
Some more info:
- Interfaces in Java
- The List interface and its methods
Lists don't necessarily do what I think you want them to. Switch to ArrayList on both sides of the line. Or switch the right side to new ArrayList<Categories>() ;