Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java how to make a parameter optional

void foo(String a, Optional<Integer> bOpt) {
    Integer b = bOpt.isPresent() ? bOpt.get() : 0;
    //...
}

foo("a", Optional.of(2));
foo("a", Optional.<Integer>absent());
Comment

java how to make a parameter optional

void foo(String a, Integer b) {
    //...
}

void foo(String a) {
    foo(a, 0); // here, 0 is a default value for b
}

foo("a", 2);
foo("a");
Comment

java how to make a parameter optional

void foo(String a, Object... b) {
    Integer b1 = 0;
    String b2 = "";
    if (b.length > 0) {
      if (!(b[0] instanceof Integer)) { 
          throw new IllegalArgumentException("...");
      }
      b1 = (Integer)b[0];
    }
    if (b.length > 1) {
        if (!(b[1] instanceof String)) { 
            throw new IllegalArgumentException("...");
        }
        b2 = (String)b[1];
        //...
    }
    //...
}

foo("a");
foo("a", 1);
foo("a", 1, "b2");
Comment

java how to make a parameter optional

void foo(String a, Integer b, Integer c) {
    b = b != null ? b : 0;
    c = c != null ? c : 0;
    //...
}

foo("a", null, 2);
Comment

java how to make a parameter optional

void foo(String a, Integer... b) {
    Integer b1 = b.length > 0 ? b[0] : 0;
    Integer b2 = b.length > 1 ? b[1] : 0;
    //...
}

foo("a");
foo("a", 1, 2);
Comment

java how to make a parameter optional

//Java 9 and above only    
@SuppressWarnings("unchecked")
    static <T> T getParm(Map<String, Object> map, String key, T defaultValue)
    {
        return (map.containsKey(key)) ? (T) map.get(key) : defaultValue;
    }

    void foo(Map<String, Object> parameters) {
        String a = getParm(parameters, "a", "");
        int b = getParm(parameters, "b", 0);
        // d = ...
    }

    foo(Map.of("a","a",  "b",2,  "d","value"));
Comment

java how to make a parameter optional

 class Foo {
     private final String a; 
     private final Integer b;

     Foo(String a, Integer b) {
       this.a = a;
       this.b = b;
     }

     //...
 }

 class FooBuilder {
   private String a = ""; 
   private Integer b = 0;

   FooBuilder setA(String a) {
     this.a = a;
     return this;
   }

   FooBuilder setB(Integer b) {
     this.b = b;
     return this;
   }

   Foo build() {
     return new Foo(a, b);
   }
 }

 Foo foo = new FooBuilder().setA("a").build();
Comment

java how to make a parameter optional

void foo(Map<String, Object> parameters) {
    String a = ""; 
    Integer b = 0;
    if (parameters.containsKey("a")) { 
        if (!(parameters.get("a") instanceof Integer)) { 
            throw new IllegalArgumentException("...");
        }
        a = (Integer)parameters.get("a");
    }
    if (parameters.containsKey("b")) { 
        //... 
    }
    //...
}

foo(ImmutableMap.<String, Object>of(
    "a", "a",
    "b", 2, 
    "d", "value")); 
Comment

java optional parameters

    void foo(String a, Object... b) {
        Integer b1 = 0;
        String b2 = "";
        if (b.length > 0) {
          if (!(b[0] instanceof Integer)) { 
              throw new IllegalArgumentException("...");
          }
          b1 = (Integer)b[0];
        }
        if (b.length > 1) {
            if (!(b[1] instanceof String)) { 
                throw new IllegalArgumentException("...");
            }
            b2 = (String)b[1];
            //...
        }
        //...
    }

    foo("a");
    foo("a", 1);
    foo("a", 1, "b2");
Comment

java optional parameters

@SuppressWarnings("unchecked")
static <T> T getParm(Map<String, Object> map, String key, T defaultValue) {
  return (map.containsKey(key)) ? (T) map.get(key) : defaultValue;
}

void foo(Map<String, Object> parameters) {
  String a = getParm(parameters, "a", "");
  int b = getParm(parameters, "b", 0);
  // d = ...
}

foo(Map.of("a","a",  "b",2,  "d","value"));
Comment

PREVIOUS NEXT
Code Example
Java :: java foreach method 
Java :: Delete Specials Caractères from a String in java 
Java :: how to install clojure on linux 
Java :: thread priorities in java 
Java :: Task :run FAILED Error: Could not find or load main class Caused by: java.lang.ClassNotFoundException: 
Java :: using a SnackBar on androidstudio 
Java :: algorithm to know if a number is an integer 
Java :: split string into array java 
Java :: https://www.baeldung.com/hibernate-inheritance 
Java :: euclidean algorithm java recursive 
Java :: how to develop web apps using java 
Java :: difference between maven plugin and dependency 
Java :: Java Create a LinkedHashSet 
Java :: how to disable the cors in spring boot 
Java :: Send keybord key P in selenium java 
Java :: what is delegation in java 
Java :: binary search algorithm in java 
Java :: android alert change color 
Java :: java program to search item from name in class 
Java :: java exception to string 
Java :: object null java 
Java :: mongodb spring boot index usage 0 
Java :: java get parent class 
Java :: sending a excel in an attachment in email java 
Java :: how to multiply a number by itself using for loop in java 
Java :: java integer object to char 
Java :: lcm of two number in java 
Java :: java for loop syntax 
Java :: how to check size of arrayin java 
Java :: array of string java 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =