Java sorting ArrayList of String Array [duplicate] - java

This question already has answers here:
Sorting an ArrayList of objects using a custom sorting order
(12 answers)
Closed 4 years ago.
I have an ArrayList in the form ArrayList<String[]> table_list = new ArrayList<String[]>();
So for example table_list.get(0) would look like {"Title", "Yes", "No"}.
Now let's say I have a lot of titles and want tot sort those titles in my table alphabetically, what do I do? Collections.sort() does not seem to work.
Thanks.

pass a custom comparator:
table_list.sort(Comparator.comparing(e -> e[0]));

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

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)

How to call on to alphabetize objects in an ArrayList? [duplicate]

This question already has answers here:
How can I sort an ArrayList of Strings in Java?
(5 answers)
Closed 7 years ago.
I'm trying to write a method that receives an ArrayList of Strings and puts it into alphabetical order. Is it possible to use the sort method for strings and not ints?
This is different from the other question because the way he did it is much more complicated than I'm aiming for. I just wanted a line of code rather than a whole block.
I am not completely sure what are you trying to say by
not int s
But if you want to sort A arraylist of string alphabetically, this can be a way.
java.util.Collections.sort(arrayListOfString);

Removes and returns the first occurrence of the specified string in array using Java [duplicate]

This question already has answers here:
Remove a specific string from an array of string
(5 answers)
Closed 8 years ago.
I am trying to delete from an array the first occurence, not all elements like the searched element The array is something like:
String[] names = {"Becky", "Rosa", "Tina", "Jill", "Rosa", "Bill"};
And I want to be able to say remove(Rosa) and only find and remove the first element in the array named Rosa.
So you might want to consider using an Arraylist for dynamic removal and resizing
import java.util.ArrayList;
ArrayList<string> persons = new ArrayList<string>();
persons.add("Becky");
//... adding in people to the list
//then if you want to remove someone name "becky"
persons.removeall.(collection.singleton("Becky"))

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

Categories