How to add ArrayList of ArrayList values into Arraylist? [duplicate] - java

This question already has answers here:
How to store all ArrayList<ArrayList<String>> values into ArrayList<String>?
(2 answers)
Closed 4 years ago.
I have a source list of type ArrayList<ArrayList<String>>. I want to put all the strings from the source list into a destination list of type ArrayList<String>. How do I do this?
Example:
sourceList = { {"a","b"}, {"c","b"} }
destinationList = { "a","b","c","d" }

You can use for-each and addAll():
for (ArrayList<String> subList : listOfLists) {
listofStrings.addAll(subList);
}
See:
https://docs.oracle.com/javase/8/docs/technotes/guides/language/foreach.html
https://docs.oracle.com/javase/7/docs/api/java/util/List.html#addAll(java.util.Collection)

Related

How do I convert List of String array to nested list of type String? [duplicate]

This question already has answers here:
Converting array to list in Java
(24 answers)
Java: is there a map function?
(6 answers)
Closed last month.
How do I convert List<String[]> to List<List<String>>?
List<String[]> allData = csv.readAll();
allData needs to be coverted in List<List<String>>.
You can use stream and Arrays::asList which will convert String[] to a List<String>, like this:
List<List<String>> response = allData.stream()
.map(Arrays::asList)
.collect(Collectors.toList()); // or just .toList();

How to create an immutable list from another list and separate items? [duplicate]

This question already has answers here:
What is an efficient and elegant way to add a single element to an immutable set?
(7 answers)
Returning a new ImmutableList that is an existing list plus an extra element
(2 answers)
Closed 4 years ago.
I am given a list and I want to add a single element of the same type that the list holds
List<String> list = List.of("a", "b", "c");
String item = "d";
I want to create an immutable list from both of them
List<String> combined = List.of(list.toArray(new String[0]), item);
The above of course does not compile because it's looking at the 2 argument overload where one argument is a String[] and the other is a String and want to create a List of size 2. What I want is to use the varargs signature of String, so to combine somehow the item into the array or "explode" the array and then the result will "merge" with the single argument.
What I tried?
List<String> arraylist = new ArrayList<>(list);
arraylist.add(item);
List<String> combined = List.copyOf(arraylist);
This is very inefficient. Can I do better?

Add String Array to ArrayList [duplicate]

This question already has answers here:
How to add elements of a string array to a string array list?
(10 answers)
Closed 6 years ago.
I have 2 String Arrays
String[] question1 = {"Q1","Q2","Q3","Q4"};
String[] question2 = {"Q5","Q6","Q7","Q8"};
And one ArrayList
ArrayList<String> aList = new ArrayList<String>();
How can I manipulate them if i want to access to a member of ArrayList. I tried converting both arrays to String but it doesn't give solution.
ArrayList<String> aList = new ArrayList<String>(Arrays.asList(question1));
aList.addAll(Arrays.asList(question2));

How do i replace an array with an arraylist? [duplicate]

This question already has answers here:
Create ArrayList from array
(42 answers)
Closed 8 years ago.
How do I replace the following array with an ArrayList.
Employee[] companyTeam = {manager, engineer1, superviso1, accountant, intern };
You can do something like
List<Employee> companyTeam = Arrays.asList(manager, engineer1, superviso1, accountant, intern);
Employee[] companyTeam = {manager, engineer1, superviso1, accountant, intern };
List<Employee> list=Arrays.asList(companyTeam);// array to List
You already have an array, So you can use Arrays.asList(companyTeam) to convert array to List

Replacing a for loop with an iterator in java? [duplicate]

This question already has answers here:
Iterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loop
(31 answers)
Is there an accepted best practice in Java for deleting a list element while iterating over the list?
(5 answers)
Closed 9 years ago.
How would I write an iterator for this code? I want to remove multiple entries based on input.
public void cancelRegistration(String someName)
{
for (Name n: ArrayList)
{
if(n.Name.equals(someName))
{
ArrayList.remove(n);
}
}
}
You can use Iterator.remove().
Assuming you have a class called Name with a String field called Name and assuming the class cancelRegistration is in has a field called ArrayList of type List<Name>:
public void cancelRegistration(String someName) {
for (Iterator<Name> iterator = ArrayList.iterator(); iterator.hasNext();)
if (iterator.next().Name.equals(someName))
iterator.remove();
}

Categories