试用java 8流.在流中是否可以从包含大量元素的List中查找以X,Y,Z开头的元素计数.
transactions.stream()
.filter(e -> startsWith("X"))
.count();
transactions.stream()
.filter(e -> startsWith("Y"))
.count();
transactions.stream()
.filter(e -> startsWith("Z"))
.count();
Run Code Online (Sandbox Code Playgroud)
上面的代码给出了列表中以X,Y,Z开头的元素的计数,但在上面的例子中,我遍历列表三次以获得数据.这可以通过使用简单的for循环迭代列表一次来完成.是否可以在单个流中执行所有这些条件[仅迭代一次]而不是使用多个流?
任何帮助是极大的赞赏.
我有一个在类示例中初始化的驱动程序对象。我也想将驱动程序对象传递给其他类,但我得到一个空指针异常。我的代码是
样本类
public class sample {
WebDriver driver ;
@Test(priority=1)
public void openbrowser(){
System.setProperty("webdriver.chrome.driver",
"/home/ss4u/Desktop/Vignesh/jars/chromedriver");
driver = new ChromeDriver();
driver.get("http://www.google.com");
System.out.println(driver instanceof WebDriver);
}
@Test(priority=2)
public void maximize(){
driver.manage().window().maximize();
}
@Test(priority=3)
public void transfer_instance(){
sampleone obj=new sampleone(driver);
}
}
Run Code Online (Sandbox Code Playgroud)
样本类
public class sampleone {
WebDriver driver;
public sampleone(WebDriver driver){
this.driver=driver;
System.out.println(driver instanceof WebDriver);
System.out.println(this.driver instanceof WebDriver);
System.out.println("constructor2");
}
public sampleone(){
System.out.println("Default constructor called");
}
@Test(priority=1)
public void gettitle(){
System.out.println(this.driver instanceof WebDriver);
System.out.println(driver instanceof WebDriver);
String title=this.driver.getTitle();
System.out.println(this.driver instanceof WebDriver);
System.out.println(title); …Run Code Online (Sandbox Code Playgroud)