如何计算两个数组列表上匹配值的百分比?

How to calculate percentage of matching values on two arraylists?

我想计算两个 ArrayList 上匹配值的百分比。

假设,如果我有两个如下所示的数组列表,分别命名为 actualrequired

ArrayList<String> actual = new ArrayList<String>();
ArrayList<String> required = new ArrayList<String>();

现在我想找到这两个列表中值匹配的百分比。任何 suggestion/solution 赞赏

谢谢

试试这个:

List<String> actual = new ArrayList<>();
List<String> required = new ArrayList<>();

List<String> common = new ArrayList<>(actual);
common.retainAll(required);

System.out.println(" 100.0 * common / actual = " + (actual.size() == 0 ? 0 : 100.0 * common.size() / actual.size()));
System.out.println(" 100.0 * common / required = " + (required.size() == 0 ? 0 : 100.0 * common.size() / required.size()));