-2

In my query I can't use hibernate and I need to generate a String as follows:

I have Map<String, String> restrictions instance with 3 keys (id, name and value) and I want to get the entry (String).

if (restrictions.get("id") != null && restrictions.get("name") == null && restrictions.get("value") == null){
       return "ID = " + restrictions.get("id");
} else if (restrictions.get("id") != null && restrictions.get("name") != null && restrictions.get("value" != null)){
       return "ID = " + restrictions.get("id") + " and Name = " + restrictions.get("name");
}

And so forth...

Explicitly writting the if-else clauses is very unflexible and hardly maintainable way. Any ideas?

Joseph118
  • 475
  • 5
  • 18
St.Antario
  • 23,179
  • 26
  • 96
  • 243

3 Answers3

3

Use java.util.StringJoiner:

import java.util.HashMap;
import java.util.Map;
import java.util.StringJoiner;

public class SOPlayground {

    public static void main(String[] args) throws Exception {
        Map<String, String> restrictions = new HashMap<>();
        restrictions.put("id", "foo");
        restrictions.put("name", "bar");
        restrictions.put("not set", null);

        StringJoiner joiner = new StringJoiner(" AND ");
        restrictions.keySet().stream().filter((column) -> (restrictions.get(column) != null)).map((column) -> {
            StringBuilder builder = new StringBuilder();
            builder.append(column).append("='").append(restrictions.get(column)).append("'");
            return builder;
        }).map((builder) -> builder.toString()).forEach((term) -> {
            joiner.add(term);
        });
        System.out.println(joiner.toString());
    }
}

Output:

id='foo' AND name='bar'
  • Well, it's a solution for Java-8 . Maybe there is something similar in Java-7? – St.Antario Apr 17 '15 at 06:58
  • 1
    Java 8 is out for over a year now. Use it. –  Apr 17 '15 at 06:59
  • Then the question should be tagged `java-7`. The `java` tag implies the current Java version which is 8. –  Apr 17 '15 at 07:01
  • but to the matter, you can easily achieve the same just with StringBuilder. The code snippet provided should be hint enough. By the way the tagging, that is just pure nittpicking ;) java means any version, its' generalization, not restriction. – Jan Hruby Apr 17 '15 at 07:04
  • 1
    "java means any version". So let's write code without generics ;) –  Apr 17 '15 at 07:14
1

Just try to search for questions on "how to iterate over a map in java". How to efficiently iterate over each Entry in a Map? should give you an example.

As for comment, below can be the code, though you can easily optimize it:

StringBuffer clause = new StringBuffer();
for(Map.Entry<String, String> entry : restrictions.entrySet()) {
    clause.append(entry.getKey()).append(\"=\").append(entry.getValue());
    clause.append(" AND ");
}

String strClause = clause.toString();
strClause = strCluase.subString(0, strClause.length() - 5); //5 is length of " AND "
Community
  • 1
  • 1
Aakash
  • 1,875
  • 13
  • 22
1

This question has been answered before. I would prefer using Colin Hebert answer in my opinion. Your if-else would be fine but you could always override the functions to meet your needs thanks to OOP (code re-usability). What you want to achieve could be done in various ways and everyone has his own way of coding.

Community
  • 1
  • 1
Joseph118
  • 475
  • 5
  • 18