Android - How to compare data of a key with other keys data in firebase

Viewed 251

enter image description here

This is just a demo data. I am using JAVA. So, I know how to fetch data from firebase but, what I want to know is how can I compare all the data of KEY i.e. user 1 with user 2 and user 3 and show it in percentage.

For example - If I compare user 1 data with user 2 data then it should show 50% similar or dissimilar. Similarly, for user1 and user3, it should show 0% similarity.

So, how can I compare and show the similar or dissimilar in percentage? Any Idea?

2 Answers

Assumptions:

  1. You know how to retrieve data from firebase.
  2. The data is stored as an object of the type User model.

Rough Pseudocode: user1 and user2 are objects of type User.

float userSimilarity(user1,user2)
{
   String[] likes1=user1.likes.split(",");
   String[] likes2=user2.likes.split(",");
   int count=0;
   if(likes1.length()>=likes2.length())
   {
       for(int i=0;i<likes1.length();i++)
           for(int j=0;j<likes2.length();2++)
               if(likes1[i].trim()==likes2[j].trim()) count++;
       return (count/likes1.length());
   }
   else
   {
       for(int i=0;i<likes2.length();i++)
           for(int j=0;j<likes1.length();2++)
               if(likes2[i].trim()==likes1[j].trim()) count++;
       return (count/likes2.length());
   } 
}

Try this one

    private static float checkSimilarity(String u1, String u2) {
        String s[] = u1.split(",");
        int count = 0;
        
        for(String v : s) {
            if(u2.contains(v.trim())) count++;
        }

        return ((float)count/s.length)*100; 
    }
Related