A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/java/util/vector_removeif.htm below:

Java Vector removeIf() Method

Java Vector removeIf() Method Description

The Java Vector removeIf(Predicate<? super E> filter) method retrieves and removes all the elements of this Vector that satisfy the given predicate. In case of exception, the exception is relayed to the caller.

Declaration

Following is the declaration for java.util.Vector.removeIf(filter) method

public boolean removeIf(Predicate<? super E> filter)
Parameters Return Value

true if any elements were removed.

Exception

NullPointerException − if the specified filter is null

Removing a Element of a Vector of Integer by Condition Example

The following example shows the usage of Java Vector removeIf(filter) method. We're creating an Vector of Integers, adding some elements, print it and then use removeIf(filter) method to remove even numbers. As Vector is modified it is printed to check if even numbers are removed or not.

package com.tutorialspoint;

import java.util.Vector;

public class VectorDemo {
   public static void main(String[] args) {
      
      // create an empty vector
      Vector<Integer> vector = new Vector<>();

      // use add() method to add elements in the vector
      vector.add(1);
      vector.add(2);
      vector.add(3);
      vector.add(4);
      vector.add(5);

      // let us print all the elements available in vector
      System.out.println("Vector = " + vector);

      // it will remove even numbers from the vector
      vector.removeIf(i -> i%2 == 0);
	  
      // let us print all the elements available in vector again
      System.out.println("Vector = " + vector);
   }
}   
Output

Let us compile and run the above program, this will produce the following result −

Vector = [1, 2, 3, 4, 5]
Vector = [1, 3, 5]
Removing a Element of a Vector of String by Condition Example

The following example shows the usage of Java Vector removeIf(filter) method. We're creating an Vector of String, adding some elements, print it and then use removeIf(filter) method to remove even numbers. As Vector is modified it is printed to check if even numbers are removed or not.

package com.tutorialspoint;

import java.util.Vector;

public class VectorDemo {
   public static void main(String[] args) {
      
      // create an empty vector
      Vector<String> vector = new Vector<>();

      // use add() method to add elements in the vector
      vector.add("A");
      vector.add("BB");
      vector.add("C");
      vector.add("DD");
	  vector.add("E");

      // let us print all the elements available in vector
      System.out.println("Vector = " + vector);

      // it will remove single length string
      vector.removeIf(i -> i.length() == 1);
	  
      // let us print all the elements available in vector again
      System.out.println("Vector = " + vector);
   }
}   
Output

Let us compile and run the above program, this will produce the following result −

Vector = [A, BB, C, DD, E]
Vector = [BB, DD]
Removing a Element of a Vector of Object by Condition Example

The following example shows the usage of Java Vector removeIf(filter) method. We're creating an Vector of Student objects, adding some elements, print it and then use removeIf(filter) method to remove even numbers. As Vector is modified it is printed to check if even numbers are removed or not.

package com.tutorialspoint;

import java.util.Vector;

public class VectorDemo {
   public static void main(String[] args) {
      
      // create an empty vector
      Vector<Student> vector = new Vector<>();

      // use add() method to add elements in the vector
      vector.add(new Student(1, "Julie"));
      vector.add(new Student(2, "Robert"));
      vector.add(new Student(3, "Adam"));
      vector.add(new Student(4, "Jene"));
      vector.add(new Student(5, "Jacob"));
	  
	  // let us print all the elements available in vector
      System.out.println("Vector = " + vector);

      // it will remove if roll no is 2
      vector .removeIf(i -> i.rollNo == 2);
	  
      // let us print all the elements available in vector again
      System.out.println("Vector = " + vector);
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }
}   
Output

Let us compile and run the above program, this will produce the following result −

Vector = [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ], [ 4, Jene ], [ 5, Jacob ]]
Vector = [[ 1, Julie ], [ 3, Adam ], [ 4, Jene ], [ 5, Jacob ]]

java_util_vector.htm


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4