我正在尝试获得产品的最高价格。我可以将价格存储在一个对象中。 我想知道如何从该对象中获取最大值。下面是我的代码!
public class amazon {
static WebDriver driver;
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.chrome.driver", "C://Selenium/chromedriver.exe");
driver = new ChromeDriver();
driver.get("xyzzz.com");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
amazon az = new amazon();
az.run();
}
public void run() throws Exception {
List<Object> obj = new ArrayList<Object>();
List<WebElement> tag = driver.findElements(By.xpath("//span[@class='a-price-whole']"));
int i;
for(i=0; i<tag.size(); i++) {
obj.add(tag.get(i).getText());
}
System.out.println(obj);
driver.close();
}
我有输出..
[64,900, 99,900, 1,23,900, 64,900, 64,900, 69,900, 64,900, 64,900, 1,23,900, 52,900]
最佳答案
将字符串映射为整数(或长整型,或某种类型的货币),然后您可以使用比较器获得最大值
int max = driver.findElements(By.xpath("//span[@class='a-price-whole']")).stream()
.map(WebElement::getText)
.map(s -> s.replace(",", ""))
.map(Integer::parseInt)
.max(Integer::compare)
.get();
关于java - 如何从ArrayList中获取最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59233856/