Check if a specific element in the array contains a specific string. Then remove the item

I have an ArrayList containing: [2x, 5, 6x]. I want to get rid of the elements containing "x", but the contains () method is applied to all elements in the ArrayList.

My attempt:

boolean ready = false;

while(!ready){
    if(first element in the array contains "x"){
    remove that element
    Check next element in line
}

      

When checking all items and removing those that contain "x", set readiness to true and stop the loop. I want the result to be like this: [6]. And then convert it to int instead of String.

EDIT: I couldn't get it to work with the ArrayList because I couldn't change the contents of the ArrayList. So I changed it to a simple array. Now the array: String [] tokens = ligning2.split ("- | \ + | \ * | \ /");

So, the token array will store [2x, 5, 6x]. So how do I remove elements containing x with this type of array instead of Arraylist?

+3


source to share


5 answers


This would be a great opportunity to use lambda when calling List#removeIf

.

For example:

List<String> list = Arrays.asList("2x", "5", "6x");

System.out.println(list); // [2x, 5, 6x]

list.removeIf(element -> element.contains("x"));

System.out.println(list); // [5]

      

Assuming there List

can be multiple values in that does not contain "x", you can use Stream

to map each item to its integer value:



List<Integer> newList = list.stream()
                            .map(Integer::valueOf)
                            .collect(Collectors.toList());

      

The whole algorithm can be summarized as follows:

List<Integer> newList = list.stream()
                            .filter(element -> !element.contains("x"))
                            .map(Integer::valueOf)
                            .collect(Collectors.toList());

      

0


source


The easiest way to do this is with Stream

s. They were added in Java 8 and you can use them in your case to filter specific items from a list and convert them to integers in one pass:



List<String> elements = Arrays.asList("2x", "5", "6x");
List<Double> result = elements.stream()
     .filter(entry -> !entry.contains("x"))
     .map(Double::valueOf) //this assumes that all other entries are valid doubles
     .collect(Collectors.toList());

      

0


source


  • Use an iterator to iterate through a list
  • for each iteration, if the current one String

    contains "x" String

    , remove it from List

    withIterator.remove();

For example:

   for (Iterator<String> iterator = list.iterator(); iterator.hasNext();) {
        String value = list.next();
        if(value.contains("x"){
           iterator.remove();
        }
    }

      

0


source


You can use an Iterator like:

List<String> list = Arrays.asList("Hello", "xyz", "tkx");
Iterator<String> iter = list.iterator();
while (iter.hasNext()) {
    if(iter.next().contains("x")){//check if your element contain x
        iter.remove();//if yes remove it
    }
}

      

0


source


You can use this program

package com.anand;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.ListIterator;

public class ArrayListRemove {
static ArrayList<String> al=new ArrayList<>(Arrays.asList("6x","8","5x"));
public static void main(String[] args) {
    Iterator<String> i=al.iterator();
    while(i.hasNext())
    {
        if(i.next().toString().contains("x"))
        {
            i.remove();
        }
    }
    ListIterator<String> li= al.listIterator();
    while(li.hasNext())
    {
        System.out.println(li.next());
    }

}

}

      

Please let me know if you want to create a new Arraylist from integers or just output for integers. I will make the appropriate changes.

0


source







All Articles