Skip to content
Reference > Methods

switch(key,defaultValue,keyValueChoices)

Definition

Object switch(Object key, Object defaultValue, Object ... keyValueChoices)

Description

Given a mapping of key/value pairs and a supplied key, returns the associated value, or a defaultValue if the supplied key does not exist in the provided mapping.

Parameter Definition

Name Type Description
key Object the key used to look up a value in the map
defaultValue Object the defaultValue to return if the key is not found in supplied keyValueChoices
keyValueChoices Object... An even number of arguments ordered such that: key1,value2,key2,value2,.... KEYS MUST EVALUATE TO CONSTANT EXPRESSIONS AND BE UNIQUE

Examples

1
2
3
4
String r1 = switch(0,"NA",0,"ZERO",1,"ONE",2,"TWO",null,"NULL"); // r1 == ZERO
String r2 = switch(2,"NA",0,"ZERO",1,"ONE",2,"TWO",null,"NULL"); // r2 == TWO
String r3 = switch(7,"NA",0,"ZERO",1,"ONE",2,"TWO",null,"NULL"); // r3 == NA
String r4 = switch(null,"NA",0,"ZERO",1,"ONE",2,"TWO",null,"NULL"); // r4 == NULL