1

I have a list of Class Object in NamAryVar
I need check if String is equals to any NamVar of NamCls in the list.
I am doing this in two ways and I get the desired result.
But
I want to know which is faster, more efficient and uses less ressources in these two methods.

|*| Using for loop :

boolean FndResVab = false;
for(NamCls NamObjIdxVar : NamAryVar)
{
    if(NamObjIdxVar.NamVar.equals("SomString"))
    {
        FndDupVab = true;
        break;
    }
}

|O| Using List Filter :

Boolean FndResVab = NamAryVar.stream()
                        .filter(IdxVar -> IdxVar.NamVar.equals("SomString"))
                        .count() == 1;

if(FndResVab)
{
    // TskTdo :=> When Found
}
else
{
    // TskTdo :=> When Not Found
}
Sujay U N
  • 4,974
  • 11
  • 52
  • 88

1 Answers1

0

Java 8 streams will almost always perform worst than regular iteration. The streams were added to Java to bring functional programming constructs into the language which would in-turn provide convenience to the programmers. Ease of use and maintainability are the primary motivators behind streams.

Have a look at this article: How Java 8 Lambdas and Streams Can Make Your Code 5 Times Slower

And this question: Java 8: performance of Streams vs Collections

iavanish
  • 509
  • 3
  • 8