Home > OS >  How to avoid too many if while checking hash map key exist or not in java
How to avoid too many if while checking hash map key exist or not in java

Time:11-05

I've map where user set these fields name, productId, productSerial, productDesc, productPrice since all these are optional fields before setting in Java bean I've to check if key is exist or not and set the value. To achieve this I've written the below code but I'm having too many if-else conditions to do that. Can someone please suggest how we can write this code efficiently in java? Sorry, I'm new to Java so apologize for the poor coding. Appreciate your help in advance. Thanks!

Product.java

public class Product {

    private String name;
    private String productId;
    private String productSerial;
    private String productDesc;
    private String productPrice;
}

Main.java

public class Main {

    public static void main(String[] args) {

        Map<String, String> map = new HashMap<>();
        map.put("name", "iPhone");
        map.put("productId", "2342343");
        map.put("productSerial", "FG44D#KLD");
        map.put("productDesc", "iPhone plus");
        map.put("productPrice", "1500");

        Product product = new Product();

        if (map.containsKey("name")) {
            product.setName((String) map.get("name"));
        } else {
            product.setName("NOT_EXIST");
        }
        if (map.containsKey("productId")) {
            product.setProductId((String) map.get("productId"));
        } else {
            product.setProductId("NOT_EXIST");
        }
        if (map.containsKey("productSerial")) {
            product.setProductSerial((String) map.get("productSerial"));
        } else {
            product.setProductSerial("NOT_EXIST");
        }
        if (map.containsKey("productDesc")) {
            product.setProductDesc((String) map.get("productDesc"));
        } else {
            product.setProductDesc("NOT_EXIST");
        }

        if (map.containsKey("productPrice")) {
            product.setProductPrice((String) map.get("productPrice"));
        } else {
            product.setProductPrice("NOT_EXIST");
        }
    }

}

CodePudding user response:

You need to use Map.getOrDefault(key,defValue) which returns the value in the map or the default value - second parameter.

product.setXYZ(map.getOrDefault("xyz", "NOT_EXIST"));

Note: since JDK1.8

CodePudding user response:

You should use the Map::getOrDefault method, like so:

Map<String, String> map = new HashMap<>();
Product product = new Product();
//...
product.setName(map.getOrDefault("name", "NOT_EXIST"));
product.setProductId(map.getOrDefault("productId", "NOT_EXIST"));
product.setProductSerial(map.getOrDefault("productSerial", "NOT_EXIST"))
//...

If you have tons of such properties to set, then you can also consider using Reflection - although it's better to avoid it in my opinion when possible.

Also, you don't have to cast the value returned by the map, since you have the map type specified Map<String,String>, meaning, that all your .get(..) and .getOrDefault(..) calls are known to always return String (or null) object :)

  • Related