0
 if ((A!= null || A!= "") && (B!= null || B!= "")
                && (C!= null || C!= "")elseif...elseif...elseif...

How we can do this without if else condition or in minimum code ??

Jacob
  • 77,566
  • 24
  • 149
  • 228
Kartik Setia
  • 31
  • 1
  • 8
  • 2
    Possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – TayTay Feb 18 '16 at 19:20
  • 1
    Well, using `!=` is *also* a problem with the sample code, but not really the gist of the question. – azurefrog Feb 18 '16 at 19:21
  • dont use equals, you are comparing references with that.... – ΦXocę 웃 Пepeúpa ツ Feb 18 '16 at 19:22
  • There's no way to do that sort of test on lots of strings at once. What you have is what I'd write. – Louis Wasserman Feb 18 '16 at 19:23
  • 4
    @kartik Any time you find yourself doing something like this, you need to go back and evaluate the underlying data structure, since you're probably doing something wrong. For instance, in this case, why do you have so many individual variables at all? Why aren't all your strings in a list, so you can loop through and only write the comparison logic once? – azurefrog Feb 18 '16 at 19:23
  • 1
    Did you mean `A!= null` _**`&&`**_ `A!= ""`? – Adam Michalik Feb 18 '16 at 20:31

2 Answers2

1

Say you have a List<String> and any number could be null or empty you can do

List<String> values = ...
List<String> good = values.stream()
                          .filter(s -> s != null && !s.isEmpty())
                          .collect(Collectors.toList());

Instead of having lots of variables, you are better off having an appropriate collection.

SiKing
  • 10,003
  • 10
  • 39
  • 90
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

I would group all the strings in a stream and then apply the same logic to each element:

Stream<String> strings = Stream.of(a, b, c, ...);

if (strings.allMatch(s -> s != null || s != "")) {
    //
}

Note: I've changed variable names to a, b, c, etc., since Java conventions establish that variable names should be lowerCamelCase.

fps
  • 33,623
  • 8
  • 55
  • 110