I know there is a String split method that returns an array but I need an ArrayList.
I am getting input from a textfield (a list of numbers; e.g. 2,6,9,5) and then splitting it at each comma:
String str = numbersTextField.getText();
String[] strParts = str.split(",");
Is there a way to do this with an ArrayList instead of an array?
You can create an ArrayList from the array via Arrays.asList:
ArrayList<String> parts = new ArrayList<>(
Arrays.asList(textField.getText().split(",")));
If you don't need it to specifically be an ArrayList, and can use any type of List, you can use the result of Arrays.asList directly (which will be a fixed-size list):
List<String> parts = Arrays.asList(textField.getText().split(","));
There is no such thing as a Split functionfor list, but you can do the split and then convert to a List
List myList = Arrays.asList(myString.split(","));
Related
I have an array:
String[] a = {"abc","def","ghi"}
Now I want to store this array into my string arraylist
ArrayList<String> arr = new ArrayList<>();
so that it becomes like this:
[["abc","def","ghi"]]
I have tried this code but it doesn't work:
arr.add(Arrays.asList(a));
Please help me
Since Arrays.asList(a) returns List, to add it to your list you need to use addAll()
arr.addAll(Arrays.asList(a));
Instead of
arr.add(Arrays.asList(a));
But the result will be ["abc","def","ghi"]
If you want to achieve this [["abc","def","ghi"]] then define your ArrayList as
List<List<String>> arr = new ArrayList<>();
using arralist addAll() method we can do but,
Using arrayList is depricated approach , use Streams instead of it:
System.out.println(Collections.singletonList(Stream.of(new String[] {"abc","def","ghi"}).collect(Collectors.toList())));
will result in:
[[abc, def, ghi]]
Is it possible to copy and/or convert all values from String[] array into an ArrayList<BigInteger> in one just line?
Like this:
List<String> strings = Arrays.asList(StringArray);
My current source code had no problem but i'm finding a way (if there is) to make it more efficient.
List<BigInteger> Data = new ArrayList<BigInteger>();
for (String current : StringArray) //Gets values from array String[] unsorted
Data.add(new BigInteger(current)); //Each string will be added in the list
My logic to achieve my goal is to iterate through entire array of String[] then get each Strings and add every String into the List<BigInteger>
Use Streams:
List<BigInteger> data = Arrays.stream(StringArray).map(BigInteger::new).collect(Collectors.toList());
With Java 8 you could shortcut :
List<BigInteger> data = Arrays.stream(strings)
.map(BigInteger::new)
.collect(toList());
Need some help to understand how I can put all the elements from an ArrayList to a single Array. Not sure if its possible to do it in a single Array.
Declaration
List componentNameList = new ArrayList();
String[] componentNameItem = soapApiCall.getComponentNames();
componentNameList.add(Arrays.toString(componentNameItem));
Here is the element of the ArrayList:
[[Index, Pattern, Smart, Intell][Index, Tree, Pet, Intel][Index, Pattern, Bear, Intell, Dog][Sky, Intern, Blond]]
Expected output for the Array
<Index><Pattern><Smart><Intell><Index><Tree><Pet><Intel><Index><Pattern><Bear><Intell><Dog><Sky><Intern><Blond>
Thanks in advance.
First, I'd suggest you not use raw types, nor do I suggest to add the string representation of an array to a raw type list.
Thus, change this:
List componentNameList = new ArrayList();
to this:
List<List<String>> componentNameList = new ArrayList<>();
then change this:
componentNameList.add(Arrays.toString(componentNameItem));
to this:
componentNameList.add(new ArrayList<>(Arrays.asList(componentNameItem)));
Then you can accomplish the task at hand with streams like below:
String[] resultSet = componentNameList.stream()
.flatMap(List::stream) // flatten
.toArray(String[]::new); // collect to array
then print:
System.out.println(Arrays.toString(resultSet));
I have an ArrayList that contains a messageId, then a -, then a username.
Example : E123-sam
I want to divide each element of my List such that the part before the - goes to one ArrayList and the part after that goes to an other ArrayList.
How can I do it?
Assuming you have these ArrayLists:
List<String> allStrings;
// ... initialization and filling of 'allStrings'
List<String> messageIDs = new ArrayList<>();
List<String> userNames = new ArrayList<>();
you can loop through elements of the ArrayList and use String#split(delimiter) to separate the string based in the delimiter:
for (String s : allStrings) {
String[] parts = s.split("-");
messageIDs.add(parts[0]);
userNames.add(parts[1]);
}
Note: This will work if all the strings in allStrings follows the pattern "something-something". If not, then you can check if the length of parts is correct before accessing its elements, otherwise you will get a IndexOutOfBoundsException.
If you plan to use Java 8, you could do:
List<String> listOfIds = original.stream().map(e -> e.split("-")[0]).collect(Collectors.toList());
List<String> listOfUsernames = original.stream().map(e -> e.split("-")[1]).collect(Collectors.toList());
I have a string variable similar to
String my = "split it";
I split this using split().
java.util.Arrays.toString(my.split("\\s+"))
The output is [split it]. But I want to store this in an arraylist, in FIRST index split and in SECOND index. Pls let me know how will I can achieve this.Thanks in advance.
You can always use
new ArrayList(Arrays.asList(myArray));
assuming of course that myArray is a array from str.split(..)
or get an Iterable with Google Guava split method:
Iterable split = Splitter.on(",").split(stringToSplit);
of course you can replace a comman ',' with regex.
Arrays.asList(my.split("\\s+"))
String my = "split it";
String[] splitArray = my.split(" ");
List<String> splitList = new ArrayList<String>(Arrays.asList(splitArray));
Notice that calling new ArrayList... is needed if you want your List to be resizable, since Arrays.asList() will return a fixed-size list backed by the original array.
String my ="String my";
String array[]= my.split(" ");
System.out.println(array[0]);
List<String > list=Arrays.asList(array);
System.out.println(list.get(0));