0

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.

matt
  • 10,892
  • 3
  • 22
  • 34
John
  • 276
  • 1
  • 9
  • 29

5 Answers5

3

Just use the contains method on the LinkedList

if (idList.contains(userId)) {

see https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html#contains(java.lang.Object)

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
2

Unless I'm missing something, instead of

if (idList.toString().contains(userId)) {

use List.contains(Object)

if (idList.contains(userId)) {
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • List idList = new LinkedList<>(); idList.add("1"); idList.add(" 2"); String userId = "2"; if (idList.contains(userId)) { System.out.println("It Worked"); } – John Jun 02 '20 at 05:44
  • @Sam Why did you add a space when you added `2` to `idList`? `List idList = new LinkedList<>(Arrays.asList(arr));` does not do that. Also, if you only store `int` values, using a `List idList` would make more sense. Since `Integer` can never contain a space. – Elliott Frisch Jun 02 '20 at 05:46
  • I have string values and idList.add(" 2"), this is what I am getting after calling an API, cannot change it. – John Jun 02 '20 at 05:47
  • Then `trim()` the values. Or `trim()` and convert to `int`. And that's not a great result from calling an API. – Elliott Frisch Jun 02 '20 at 05:53
  • @Sam listen to Elliot. Otherwise `idList.add(" 123"); String userId = "12"; if (idList.toString().contains(userId)) { System.out.println("It failed"); } ` – daniu Jun 02 '20 at 06:03
0

You're printing out a list, the default toString will add spaces between elements.

System.out.println("[" + idList.stream().collect( Collectors.joining(",")) + "]");

That will concatenate the list without spaces.

This:

System.out.println("==>"+idList);

Adds spaces to the printed string only, not the elements of the list. So do both of these.

System.out.println("Updated==>"+afterRemovingWhiteSpace);
System.out.println("Updated==>"+idList);

The elements of the list were not changed

Another misconception you seem to be having.

"[123, 456]".contains("456");
"[123,456]".contains("456");

Both strings will contain 456, the spaces won't change that.

matt
  • 10,892
  • 3
  • 22
  • 34
0

You can trim spaces using this:

List<String> newList = new LinkedList<String>();
for(String x: idList)
        newList.add(x.trim());
-2

This worked, by combining @matt answer's

List<String> idList = new LinkedList<>();
    idList.add("1");
    idList.add("          2");
    String userId = "2";
    List<String> removeLeadingWhiteSpacesList = new ArrayList<>();
    System.out.println("idList=>"+idList);
    for (String temp : idList) {
        removeLeadingWhiteSpacesList.add(temp.trim());
    }
    if (removeLeadingWhiteSpacesList.contains(userId)) {
        removeLeadingWhiteSpacesList.remove(userId);
        System.out.println("removeLeadingWhiteSpacesList=>"+removeLeadingWhiteSpacesList);
    }

It outputs

idList=>[1,           2]
removeLeadingWhiteSpacesList=>[1]
John
  • 276
  • 1
  • 9
  • 29
  • As in my other comment, try `idList.add(" 123"); String userId = "12"; if (idList.stream().collect( Collectors.joining(",")).contains(userId)) { System.out.println("It failed"); } `. – daniu Jun 02 '20 at 06:24
  • Yup!, basically I have to trim the values and store it in other List and use that list. But this is what I did not wanted to do, as list is going to contain very large number of elements and it is not an optimized solution. Anyways Thanks for your efforts. – John Jun 02 '20 at 06:32
  • No, what you have to do is listen to the people here and check the list's content rather than analyzing a stringified version. – daniu Jun 02 '20 at 06:41