Way to create a list in Java [duplicate] - java

This question already has answers here:
What does it mean to program to an interface?
(17 answers)
Arraylist and List in Java
(3 answers)
Closed 7 months ago.
I am wondering about the difference between these two options to create a list of string. Which one to use when creating a list in Java?
Option 1
List<String> list1 = new ArrayList<String>();
Option 2
ArrayList<String> list2 = new ArrayList<String>();

Related

List delimiter with custom symbol [duplicate]

This question already has answers here:
Best way to concatenate List of String objects? [duplicate]
(19 answers)
Java: convert List<String> to a join()d String
(23 answers)
Closed 2 months ago.
I have list with some values:
List<String> list = Arrays.asList("1","2","3");
I want convert it to String "1; 2; 3"
I have only idea with loop. It works but it doesn't looks elegant. Maybe somebody can advice more easy way to do it?
Try to use String.join("; ", list)
It should work

what is the meaning of list.toString(new int[0][]); [duplicate]

This question already has answers here:
What's the use of new String[0] in toArray(new String[0]);
(3 answers)
.toArray(new MyClass[0]) or .toArray(new MyClass[myList.size()])?
(8 answers)
What does "new Boolean[0]" or "new String[0]" in Java evaluate to [duplicate]
(3 answers)
Closed 7 months ago.
I am new to java, I have encountered a statement in "merge sublists" program,
list.toArray(new int[0][]); I found this statement in the end of program. Here list consist of list of integer array. It has some data, but why they are using a 2d array of 0 rows in above statement.
Please anyone give a detailed explanation

JAVA8: Map list of objects to String[] [duplicate]

This question already has answers here:
How to convert a Java 8 Stream to an Array?
(9 answers)
Closed 5 years ago.
I have a List of 'Client' objects each one with a field "email".
I need something like:
List<String> listEmails = clients.stream().map(client->client.getEmail())
.collect(Collectors.toList());
...but returning directly a String[].
Is there a proper way to map a List<Client> to a String[] listEmails using Java 8 streams?
Sure :
String[] result = clients
.stream()
.map(client->client.getEmail())
.toArray(String[]::new)

Putting arrays in ArrayList [duplicate]

This question already has answers here:
Create ArrayList from array
(42 answers)
Closed 8 years ago.
Probably a very stupid question, but how can I put an array into an ArrayList?
Example:
String[] strings = {"ex","okay",};
ArrayList<String> stringslist = new ArrayList<String>();
Now how do I put everything in strings, into stringlist?
You need to use Arrays.asList() function.
String[] strings = {"ex","okay",};
ArrayList newStringslist = new ArrayList<Element>(Arrays.asList(strings))
Tutorials-Point Link
Also - Create ArrayList from array
new ArrayList<Element>(Arrays.asList(array)) -- JAVA
per answer at Create ArrayList from array

Declaring ArrayLists: difference in Functionality? [duplicate]

This question already has answers here:
What is the point of the diamond operator (<>) in Java?
(7 answers)
Closed 8 years ago.
I'm curious, When declaring ArrayLists, What is the difference in doing this:
List<String> arrayList1= new ArrayList<String>();
and this:
List<String> arrayList2= new ArrayList<>();
i.e. not declaring the <String> twice?
the only difference is that the first form is compatible with earlier java versions than java 7.
And you don't need the <> either in latter versions. e.g
List<String> arrayList2= new ArrayList();

Categories