Reputation: 7164
In a Java code, I have 11 different strings, some of them (at most 9) can be null. Strings are a part of an object. If none of them is null, I make a string like this :
string1 + "," + string2 + "," + string3 + "," + string4 + "," + string5 + "," + string6 + "(" + string7 + ")" + string8 + string9 + "+" + string10 + string11
If none of them is null, it's okey. But some of them can be null. If I check if each of them is null, code gets really slow and long. How can I generate such a string in an efficient and fast way? Thanks.
Upvotes: 1
Views: 265
Reputation: 370
If 6th index of your string is always surrounded by (
and )
then you can use following code.
List<String> vals = new ArrayList<>();
vals.add(string1);
vals.add(string2);
vals.add(string3);
vals.add(string4);
vals.add(string5);
vals.add(string6);
vals.add(string7);
vals.add(string8);
vals.add(string9);
vals.add(string10);
vals.add(string11);
for (int i =0 ; i < vals.size(); i++) {
// check null values
if (vals.get(i) != null) {
// as your requirement surround 7th value with ( and )
if(vals.indexOf(vals.get(i)) == 6) {
values += "\b(" + vals.get(i)+")";
} else {
values += vals.get(i)+",";
}
}
}
System.out.println(values+"\b");
Output
if 4th and 9th strings are null
then,
test1,test2,test3,test5,test6(test7)test8,test10,test11
Upvotes: 1
Reputation: 3103
If you simply want your code to be as short as possible, use String.format and get rid of the "null" in the string.
String result = String.format("%s,%s,%s,%s,%s,%s(%s)%s,%s,%s,%s",
string1, string2, string3, string4, string5,
string6, string7, string8, string9, string10,
string11);
System.out.println(result.replace("null", ""));
Upvotes: 1
Reputation: 985
In Java 8, you can stream and join with Collectors, and filter with Objects classes
List<String> strings = Arrays.asList("first", "second", null, "third", null, null, "fourth");
String res = strings.stream().filter(Objects::nonNull).collect(Collectors.joining(","));
System.out.println(res);
results in output
first,second,third,fourth
Upvotes: 1
Reputation: 200226
With Java 8 you could use this for the first part with comma-delimited strings:
String part1 = Stream.of(string1, string2, string3, string4, string5, string6)
.filter(Objects.notNull())
.collect(joining(",");
From then on you have an irregular structure so you'll need custom logic to handle it. The helper function
static String emptyForNull(String s) {
return s == null ? "" : s;
}
can get you part of the way.
Upvotes: 2
Reputation: 496
public static void main(String[] args) {
String string1 = "test1";
String string2 = "test2";
String string3 = "test3";
String string4 = null;
String string5 = "test5";
StringBuilder stringBuilder = new StringBuilder();
List<String> valueList = new ArrayList<>();
valueList.add(string1);
valueList.add(string2);
valueList.add(string3);
valueList.add(string4);
valueList.add(string5);
// etc.
for (String value : valueList) {
if (value != null) {
stringBuilder.append(value);
}
else {
value = ",";
stringBuilder.append(value);
}
}
System.out.println(stringBuilder);
}
Output :
test1test2test3,test5
Upvotes: 3