比较两个数组并获得相似性 java

comparing two arrays and getting similarity java

我在自己的自定义对象上有两个数组。我需要找到相似度来给它打分。用户将提交他们烹制的食谱,程序应该对原始食谱的准确性进行评分。这是我尝试过的:

int increase = 100 / userRecipe.size();
for(int i = 0; i < userRecipe.size(); i++) {
    if(userRecipe.get(i).equals(bookRecipe.get(i))) {
         percent += increase;
    }
}

然而,此解决方案的主要缺陷在于,理论上,下面这两个配方的准确度应为 75%,而准确度为 0%。

Item[] userRecipe = {milk, sugar, eggs, flour};
Item[] bookRecipe = {sugar, eggs, flour};

但是它不起作用,因为两个列表的长度不同,所以它不起作用。如果有人知道我应该如何处理这个问题,我将不胜感激。以下是问题

我是 java 的初学者,所以如果有人不介意给我一个好的解决方案,我将不胜感激!谢谢。

假设列表是有序的,为了最优地解决这个问题(没有运行在O(n^2)时间),这是一个经典的操作两个指针问题。

int increase = 100 / userRecipe.size();
int userIndex = 0;
int bookIndex = 0;
while (bookIndex < boockRecipe.size() && userIndex < userRecipe.size()) {
    if (userRecipe.get(userIndex).equals(bookRecipe.get(bookIndex))) {
        percentage += increase;
        bookIndex++;
    }
    userIndex++;
}

这只会循环每个列表一次

这是我的看法。

  • 用一套来装食谱的原料。
  • 然后简单地流式传输每个提交并获得正确成分的数量。
  • 然后根据提交的成分数量和配方数量中的较大者计算百分比(对过度指定进行惩罚)

这与成分的顺序无关,并且由于使用一组来保存食谱,因此每个用户都可以在线性时间内工作。

Set<String> bookRecipe = Set.of("sugar", "eggs", "flour");
double ingredientCount = bookRecipe.size();

List<List<String>> entries = List.of(List.of("milk", "sugar"),
        List.of("milk", "sugar", "eggs", "flour"),
        List.of("milk", "sugar", "eggs", "flour", "beer"),
        List.of("milk", "sugar", "whiskey", "paint"),
        List.of("milk", "sugar", "eggs", "beer", "hay"),
        List.of("milk", "beer", "hay"),
        List.of("milk", "sugar", "eggs", "beer", "hay",
                "orange juice"));

System.out.println("Recipe: " + bookRecipe + "\n");
for (List<String> submission : entries) {
    long correct = submission.stream()
            .filter(ingredient -> bookRecipe.contains(ingredient))
            .count();
    
    double percentage = correct
            / Math.max(ingredientCount, submission.size());
    
    System.out.printf("%3d%% - %s%n",
            (int) (percentage * 100), submission);
}

打印

Recipe: [eggs, flour, sugar]

 33% - [milk, sugar]
 33% - [eggs]
  0% - [milk]
 75% - [milk, sugar, eggs, flour]
 60% - [milk, sugar, eggs, flour, beer]
 25% - [milk, sugar, whiskey, paint]
 40% - [milk, sugar, eggs, beer, hay]
  0% - [milk, beer, hay]
  0% - [beer, chips, salsa]
 33% - [milk, sugar, eggs, beer, hay, orange juice]

我想你的意思是这样的。

public static void main(String[] args) {





    String[] s1 = {"A", "B", "D"};//userRecipe
    String[] s2 = {"A", "B", "C","D","E"};//bookRecipe

    int percent=0;

    int largerCount=s1.length>s2.length ? s1.length:s2.length;
    int smallerCount=s1.length<s2.length ? s1.length:s2.length;
    String[] largeArr =s1.length>s2.length ?s1:s2;
    String[] smallerArr =s1.length<s2.length ?s1:s2;

    for(String x:largeArr)
        for (String y:smallerArr) {
            if (x==y) percent++;
        }

    System.out.println((percent*100)/largerCount+"%");

}