Link to home
Start Free TrialLog in
Avatar of sarah2248
sarah2248

asked on

Java Programming help

I'm trying to understand a java program that looks like followin:


interface Filter<T> {
    boolean matches(T t);
}
public class Test{
    Filter<Set<Set<Integer>>> filter = new Filter<Set<Set<Integer>>>() {
        public boolean matches(Set<Set<Integer>> integers) {
            Set<Integer> union = new LinkedHashSet<Integer>();
            for (Set<Integer> ints : integers)
                union.addAll(ints);
            return union.equals(solutionSet);
        }
    };

}

why does the code have filter interface at the top and inside the method declaration one of the parenthesis ends with semicolon..is that correct?
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

>>why does the code have filter interface at the top

The interface is declared then implemented

>> one of the parenthesis ends with semicolon..

What are you referring to here?
>>inside the method declaration one of the parenthesis ends with semicolon..is that correct?

yes, it says that it doesn't have method body!
Avatar of sarah2248
sarah2248

ASKER

Thanks I'm new to java and trying to understand this program in the book. The program is supposed to find combinations of all sets by calling a method shortesCombination. But I can't figure out what is going on inside the main method after the set and arraylist declaration. I put a comment before the line where I start getting confused. Could you please explain the program to me kindly.

Why are we declaring the interface in the program. why do we need it?


Following is the full program:

interface Filter<T> {
    boolean matches(T t);
}
public class Test3{
public static void main(String... args) throws IOException {
    Integer[][] arrayOfSets = {
        {1,2,5,6,9,10},
        {9,10,11,12},
        {1,2,3,4},
        {3,5,6,7,8},
        {9,10,11,12},
        {4,8},
    };
    Integer[] solution = {1,2,3,4,5,6,7,8,9,10,11,12};

//I don't understand the code below

    List<Set<Integer>> listOfSets = new ArrayList<Set<Integer>>();
    for (Integer[] array : arrayOfSets)
        listOfSets.add(new LinkedHashSet<Integer>(Arrays.asList(array)));
    final Set<Integer> solutionSet = new LinkedHashSet<Integer>(Arrays.asList(solution));

    Filter<Set<Set<Integer>>> filter = new Filter<Set<Set<Integer>>>() {
        public boolean matches(Set<Set<Integer>> integers) {
            Set<Integer> union = new LinkedHashSet<Integer>();
            for (Set<Integer> ints : integers)
                union.addAll(ints);
            return union.equals(solutionSet);
        }
    };

    Set<Set<Integer>> firstSolution = shortestCombination(filter, listOfSets);
    System.out.println("The shortest combination was "+firstSolution);
}

Open in new window

 @Savant

Filter<Set<Set<Integer>>> filter = new Filter<Set<Set<Integer>>>() {
        public boolean matches(Set<Set<Integer>> integers) {
            Set<Integer> union = new LinkedHashSet<Integer>();
            for (Set<Integer> ints : integers)
                union.addAll(ints);
            return union.equals(solutionSet);
        }
    }; <------- I was referring to this semicolon
>> <------- I was referring to this semicolon

That marks the end of an anonymous class constructor
oh thanks
In your code last ; is the ending of anonymous constructor. this is Perfactly valid.
what is the code doing in the following lines:

Filter<Set<Set<Integer>>> filter = new Filter<Set<Set<Integer>>>() {
        public boolean matches(Set<Set<Integer>> integers) {
            Set<Integer> union = new LinkedHashSet<Integer>();
            for (Set<Integer> ints : integers)
                union.addAll(ints);
            return union.equals(solutionSet);
        }
    };

can you please explain..thanks
It adds successive Set<Integer>, derived from the Integer[] instances to one big Set ('union') and at each iteration, 'union' is tested for equality with the solution set
>>and at each iteration,

That was incorrect. Substitute

and after all iterations
cool thanks that was really helpful. I have some more questions about this one. I will post my questions shortly.
@CEHJ thanks so much for the explanations. I'm starting to understand what the code is doing
I have some more questions.
Why are we using this : "<<"  and ">>" in the following code:

private static <T> Set<T> shortestCombination(Filter<Set<T>> filter, List<T> listOfSets) {
    final int size = listOfSets.size();
    int combinations = 1 << size;
    List<Set<T>> possibleSolutions = new ArrayList<Set<T>>();
    for(int l = 0;l < combinations;l++) {
        Set<T> combination = new LinkedHashSet<T>();
        for(int j=0;j<size;j++) {
            if (((l >> j) & 1) != 0)
                combination.add(listOfSets.get(j));
        }
        possibleSolutions.add(combination);
    }
    // the possible solutions in order of size.
    Collections.sort(possibleSolutions, new Comparator<Set<T>>() {
        public int compare(Set<T> o1, Set<T> o2) {
            return o1.size()-o2.size();
        }
    });//end of an anonymous class constructor
    
    for (Set<T> possibleSolution : possibleSolutions) {
        if (filter.matches(possibleSolution))
            return possibleSolution;
    }
    return null;
}

Open in new window


Could you please explain lines 5-12 and 14-18.
Thanks so much for all the help
ASKER CERTIFIED SOLUTION
Avatar of silkyguy21
silkyguy21
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
For your question, why << and >> is being used. This is because we need to use the left shift operator to know how many possible sets are to be derived out of "listOfSets". As this value is 6 so we know there are 2^6 = 64 possible combination of sets.

1 leftshift ops. by 6 points = 64
Thanks so much for all your help. This helps a lot.
hi sarah,

if you are satisfied... pls close the question and recommend solution.

Thanks
sarah2248, can you tell me why you ignored my previous contributions to this multipart question?
Sorry about that. Everyone equally helped me with this one but I don't know how to select multiple answers and once. I did not ignore any of your contributions.
>>but I don't know how to select multiple answers

Then you must learn