-3

I want to point to the item that is on each list. Lists are kept in List <Set >. The point is, in each of the lists, it stores several numbers. I want to add to qSE the numbers that will appear in each of the lists. What should I add in the code to make it work as it should?

public static Set<Integer> getSameElements(List<Set<Integer>> listOfElements){
    Set<Integer> gSE = new HashSet<>();
    for (Set<Integer> list : listOfElements){
        for (int number : list){

            }
        }
    }
    return gSE;
}
Martin009
  • 1
  • 3
  • No idea what you're asking, sorry. – Michael Oct 04 '22 at 21:42
  • Its sounds like you want the intersection of your sets. see this https://stackoverflow.com/questions/2851938/efficiently-finding-the-intersection-of-a-variable-number-of-sets-of-strings – bhspencer Oct 04 '22 at 21:42
  • The point is, in each of the lists, it stores several numbers. I want to add to qSE the numbers that will appear in each of the lists. – Martin009 Oct 04 '22 at 21:47

2 Answers2

0

i think

public static Set<Integer> getSameElements(List<Set<Integer>> listOfElements){
    Set<Integer> gSE = new HashSet<>();
    for (Set<Integer> list : listOfElements){
        for (int number : list){
//this part here\/
               gSE.add(number);

            }
        }
    }
    return gSE;
}

may be what youre looking for? if youre trying to put the numbers from the other sets into that set. question is a tad hard to follow but hope that helps

sean rogan
  • 43
  • 5
  • Thanks for the answer, but the problem is that gSE will have to find the numbers that are in each of the lists. If each list contains the digit 1, then gSE is supposed to contain only the digit 1. – Martin009 Oct 05 '22 at 16:44
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 08 '22 at 00:47
0
Set<Integer> list1=Set.of(5,7,9,11,13);
        Set<Integer> list2=Set.of(5,7,9,11,13);
        Set<Integer> list3=Set.of(2,4,6,8,10);
        Set<Integer> list4=Set.of(3,6,9,12,15);
        List<Set<Integer>> addlist=List.of(list1,list2,list3,list4);
        Set<Integer> gse = new HashSet<>();
        for(Set<Integer> s:addlist) {
            for(int n:s ) {
                gse.add(n);
            }
        }
Output:[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15]