Verify Two lists using Java using stream

package com.sam.testcases.Knowledge_Management.regression;
import org.testng.Assert;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class ListComparison {


public static void main(String[] args) {
// All the fruitsWithCommas must be available in fruitSaladWithCommas, or else the assertion fails.
String fruitsWithCommas="Apple,Orange,Banana,Pineapple,Avacado";
List<String> fruitsList = Stream.(fruitsWithCommas.split(",")).map(String::trim).collect(Collectors.());
System..println("allExpectedRecipientsFound==" + fruitsList);

// Adding Mango additionally is OK won't fail.
String fruitSaladWithCommas="Banana,Avacado,Pineapple,Apple,Orange,Mango";
List<String> fruitSaladList = Stream.(fruitSaladWithCommas.split(",")).map(String::trim).collect(Collectors.());

// Then check that actual actualRecipientList contains all the expectedRecipientList
boolean allExpectedRecipientsFound = fruitSaladList.containsAll(fruitsList);

Assert.(true, allExpectedRecipientsFound, "All the Fruits were not found in fruit Salad.");

// ways to iterate though a list. https://crunchify.com/how-to-iterate-through-java-list-4-way-to-iterate-through-loop/
// Simple For loop
System..println("==============> 1. Simple For loop Example.");
for (int i = 0; i < fruitsList.size(); i++) {
System..println(fruitsList.get(i));
}
// New Enhanced For loop
System..println("\n==============> 2. New Enhanced For loop Example..");
for (String temp : fruitSaladList) {
System..println(temp);
}
// collection Stream.forEach() util: Returns a sequential Stream with this collection as its source
System..println("\n==============> 7. Stream.forEach() Example....");
fruitsList.stream().forEach((crunchifyTemp) -> System..println(crunchifyTemp));
}



}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store