I have a string array that I convert to ArrayList and remove an item from it. Then use that updated ArrayList.
Problem Initial Input:
String[] arr = {"467","470","464","410"};
List<String> idList = new LinkedList<>(Arrays.asList(arr));
After converting and removing item it prints
[467, 470, 464]
It adds unwanted leading white spaces between the elements of list, which causes trouble as then I operate on it using
if (idList.toString().contains(userId)) {
idList.remove(userId);
}
So if userId="470" and in this list it is " 470", it doesn't match due to white space's issue and code fails.
It tried few things like using integer array instead of string
, but it makes no difference.
or
List<String> idList = new LinkedList<>(Arrays.asList(arr));
System.out.println("==>"+idList);
List afterRemovingWhiteSpace = new ArrayList<>();
idList.forEach(e -> afterRemovingWhiteSpace.add(e.trim()));
System.out.println("Updated==>"+afterRemovingWhiteSpace);
or
String[] arr = {"467", "470", "464"};
List<String> idList = new LinkedList<>(Arrays.asList(arr));
System.out.println("==>"+idList);
String regex = "^\\s+";
idList.forEach(e -> e.replaceAll(regex, ""));
System.out.println("Updated==>"+idList);
But none works. Any idea how to do it efficiently, I don't want use array and remove an item from it, as list can contain large number of elements and that will become very costly operation.