使用Java8 计算分类价格的同环比
时间: 2023-02-07 07:58:23
浏览: 116
非常感谢您的问题。
要在Java 8中计算分类价格的同比,您需要首先收集有关每个分类的价格[信息](https://geek.csdn.net/educolumn/4b7516410bb8585d5db30bb2e9a69b47?spm=1055.2569.3001.10083),然后计算每个分类的同比增长率。
假设您有一个由整数组成的[列表](https://geek.csdn.net/educolumn/ba94496e6cfa8630df5d047358ad9719?dp_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NDQ0MDg2MiwiZXhwIjoxNzA3MzcxOTM4LCJpYXQiOjE3MDY3NjcxMzgsInVzZXJuYW1lIjoid2VpeGluXzY4NjQ1NjQ1In0.RrTYEnMNYPC7AQdoij4SBb0kKEgHoyvF-bZOG2eGQvc&spm=1055.2569.3001.10083),其中包含每个分类的价格,则可以使用以下[代码](https://geek.csdn.net/educolumn/1572ef9b473b4e00f6b2bf6d428b7c27?spm=1055.2569.3001.10083)来计算同比增长率:
List<Integer> prices = // list of prices for each category
// calculate year-over-year growth rate for each category
for (int i = 1; i < prices.size(); i++) {
int currentPrice = prices.[get](https://geek.csdn.net/educolumn/31aebce56f4ecc5451aac3a194ff6259?spm=1055.2569.3001.10083)(i);
int previousPrice = prices.[get](https://geek.csdn.net/educolumn/31aebce56f4ecc5451aac3a194ff6259?spm=1055.2569.3001.10083)(i - 1);
double growthRate = (double) (currentPrice - previousPrice) / previousPrice;
System.out.println("Category " + i + " growth rate: " + growthRate);
我希望这对您有帮助。如果您有任何其他问题,请随时告诉我。