public class Solution { public static void main(String[] args) { Map<java.lang.String, java.lang.String> map = new HashMap<>(); map.put("name", "'Ivanov'"); map.put("country", "'Ukraine'"); map.put("city", "'Kiev'"); map.put("age", null); System.out.println(getQuery(map)); } public static String getQuery(Map<String, String> params) { StringBuilder stbname = new StringBuilder(); StringBuilder stbcountry = new StringBuilder(); StringBuilder stbcity = new StringBuilder(); StringBuilder stbage = new StringBuilder(); for (Map.Entry<String, String> item : params.entrySet()) { //System.out.println(item.getKey()); if (item.getKey().equals("name")) { if (item.getValue() != null) { stbname.append("name = " + item.getValue()); continue; }} if (item.getKey().equals("country")) { if (item.getValue() != null) { stbcountry.append(" and country = " + item.getValue()); continue; }} if (item.getKey().equals("city")) { if (item.getValue() != null) { stbcity.append(" and city = " + item.getValue()); continue; } } if (item.getKey().equals("age")) { if (item.getValue() != null) { stbcity.append(" and age = " + item.getValue()); continue; } else{ continue; } } } String strName = stbname.toString(); String strCountry = stbcountry.toString(); String strCity = stbcity.toString(); String strAge = stbage.toString(); String str = strName + strCountry + strCity + strAge; return str; } }