0

my idea is this. I want to create a script, linking mutual friends

For example:

Friends of Damian = [Cristian, Pedro, Juan, Andres]

Friends of Andres = [Marcela, Damian, Julian, Carlos]

Friends of Lucas = [John, Damian, Andres, Francisco]

Friends of Roman = [Albert, Lourdes, Jennifer, Diego]

Friends of Diego = [Magali, Brian, Andrew, Peter]

Friends of Brian = [Etc.]

Friends of Leo = [Etc.]

Friends of Ramiro = [Etc.]

Friends of Franco = [Etc.]

I should select 5 names, and I would have to say if any friends in common and what are the names. The names will be limited to 60 approx. And they will be 4 names for groups.

Roman Racca
  • 37
  • 1
  • 6

2 Answers2

1

Use Collection#retainAll().

listA.retainAll(listB);
// listA now contains only the elements which are also contained in listB.

So make a copy of the first list and repeat retainAll for all five lists. That I think would be an easy way to do this.

Referring this answer Common elements in two lists

Community
  • 1
  • 1
gatech-kid
  • 67
  • 8
0

I assume by common Java, you are talking about Java, and not Javascript. However you called it a script... a snippet of code is also a near must to get good answers on stack overflow, I don't know whether you are using arrays, lists, or even Maps... I will give an answer for each. But please, do give more detailed information next time you ask a question.

Lists have a method called contains() which you can pass an object into. From here there are two ways to work with it, the preferred way is through Lists

package com.gamsion.chris;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class SOListExample {
    public static void main(String[] args) {
        List<String> list1 = new ArrayList<String>();
        List<String> list2 = new ArrayList<String>();
        List<String> list3 = new ArrayList<String>();
        String name1 = "John";
        String name2 = "Chris";
        String name3 = "Madalyn";
        list1.add(name1);
        list2.add(name1);
        list2.add(name2);
        list3.add(name2);
        list3.add(name3);
        list1.add(name3);
        list2.add(name3);
        List<List<String>> lists = new ArrayList<List<String>>();
        lists.add(list1);
        lists.add(list2);
        lists.add(list3);
        System.out.println(getCommonFriends(lists));
    }

    public static List<String> getCommonFriends(List<List<String>> lists) {
        List<String> commonfriends = lists.get(0);
        Iterator<List<String>> it = lists.iterator();
        while(it.hasNext()){
            commonfriends.retainAll(it.next());
        }
        return commonfriends;
    }
}

That is the best answer I can give. Of course adapt to your needs and it should fit well. (I just made that as a testable example. The way you get those lists and how many is totally up to you.) It's also the most effective as far as I know, although the initial "commonfriends = lists,get(0)" does annoy me haha.

EDIT: Adding answer for comment below.

To do that, suggest adding another method that just compares two Lists like this:

public static List<String> compareCommonFriends(List<String> list1, List<String> list2){
    list1.retainAll(list2);
    return list1;
}

Also in case you want to do an if something was found, whether it was the compareCommonFreinds() or the getCommonFriends(), you can do something like this:

if(compareCommonFriends(list1, list2).isEmpty()){
    //your code
    }
    //OR
if(!compareCommonFriends(list1, list2).isEmpty()){
    //your code
} 
Gamma2626
  • 81
  • 5
  • Thank you very much for the reply! Just try the script, and meets my needs. But how could I do that if no names in common in at least 2 of the x lists mentioned, I also show them? – Roman Racca Sep 04 '15 at 21:08