El método remove() de la clase SimpleBindings se usa para eliminar la asignación de esta clave de este objeto SimpleBindings, si está presente. Este método devuelve el valor anterior asociado con la clave especificada, o nulo si no hubo una asignación para la clave.
Sintaxis:
public Object remove(Object key)
Parámetros: este método acepta una clave de parámetro que es la clave cuya asignación se eliminará del mapa.
Valor devuelto: este método devuelve el valor anterior asociado con la clave especificada, o nulo si no hubo una asignación para la clave.
Excepción: este método arroja las siguientes excepciones:
- NullPointerException : si la clave es nula.
- ClassCastException : si la clave no es String.
- IllegalArgumentException : si la clave está vacía String.
A continuación hay programas para ilustrar el funcionamiento del método remove():
Ejemplo 1:
Java
// Java programs to Illustrate // the working of remove() method import javax.script.SimpleBindings; public class GFG { public static void main(String[] args) { // create simpleBindings object SimpleBindings bindings = new SimpleBindings(); // add key value pair using remove() bindings.put("key1", "value1"); bindings.put("key2", "value2"); bindings.put("key3", "value3"); // print before removing key1 and key2 System.out.println("before removing key1 and key2"); System.out.println("Key1: " + bindings.get("key1")); System.out.println("Key2: " + bindings.get("key2")); System.out.println("Key3: " + bindings.get("key3")); // remove key1 and key2 bindings.remove("key1"); bindings.remove("key2"); // print after removing key1 and key2 System.out.println("after removing key1 and key2"); System.out.println("Key1: " + bindings.get("key1")); System.out.println("Key2: " + bindings.get("key2")); System.out.println("Key3: " + bindings.get("key3")); } }
before removing key1 and key2 Key1: value1 Key2: value2 Key3: value3 after removing key1 and key2 Key1: null Key2: null Key3: value3
Ejemplo 2:
Java
// Java programs to Illustrate // the working of remove() method import javax.script.SimpleBindings; public class GFG { public static void main(String[] args) { // create simpleBindings object SimpleBindings asiaTeamList = new SimpleBindings(); // add team in asiaTeamList using remove() asiaTeamList.put("team1", "India"); asiaTeamList.put("team2", "Sri Lanka"); asiaTeamList.put("team3", "Pakistan"); asiaTeamList.put("team4", "Bangladesh"); // print before removing System.out.println("before removing team3 and team4"); System.out.println("Team1: " + asiaTeamList.get("team1")); System.out.println("Team2: " + asiaTeamList.get("team2")); System.out.println("Team3: " + asiaTeamList.get("team3")); System.out.println("Team4: " + asiaTeamList.get("team4")); // remove team3 and team4 asiaTeamList.remove("team3"); asiaTeamList.remove("team4"); // print before removing key1 and key2 System.out.println("after removing team3 and team4"); System.out.println("Team1: " + asiaTeamList.get("team1")); System.out.println("Team2: " + asiaTeamList.get("team2")); System.out.println("Team3: " + asiaTeamList.get("team3")); System.out.println("Team4: " + asiaTeamList.get("team4")); } }
before removing team3 and team4 Team1: India Team2: Sri Lanka Team3: Pakistan Team4: Bangladesh after removing team3 and team4 Team1: India Team2: Sri Lanka Team3: null Team4: null
Publicación traducida automáticamente
Artículo escrito por AmanSingh2210 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA