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?

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?

Programming Languages-OtherJava

ksivananth

>>inside the method declaration one of the parenthesis ends with semicolon..is that correct?

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

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

sarah2248

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:

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);
}
```

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat

William Peck

sarah2248

@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

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

CEHJ

>> <------- I was referring to this semicolon

That marks the end of an anonymous class constructor

That marks the end of an anonymous class constructor

sarah2248

oh thanks

Get an unlimited membership to EE for less than $4 a week.

Unlimited question asking, solutions, articles and more.

silkyguy21

In your code last ; is the ending of anonymous constructor. this is Perfactly valid.

sarah2248

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

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

CEHJ

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

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!

James Murphy

CEHJ

>>and at each iteration,

That was incorrect. Substitute

and after all iterations

That was incorrect. Substitute

and after all iterations

sarah2248

cool thanks that was really helpful. I have some more questions about this one. I will post my questions shortly.

sarah2248

@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:

Could you please explain lines 5-12 and 14-18.

Thanks so much for all the help

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;
}
```

Could you please explain lines 5-12 and 14-18.

Thanks so much for all the help

Get an unlimited membership to EE for less than $4 a week.

Unlimited question asking, solutions, articles and more.

Log in or sign up to see answer

Become an EE member today7-DAY FREE TRIAL

Members can start a 7-Day Free trial then enjoy unlimited access to the platform

or

Learn why we charge membership fees

We get it - no one likes a content blocker. Take one extra minute and find out why we block content.

Not exactly the question you had in mind?

Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.

ask a questionsilkyguy21

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

1 leftshift ops. by 6 points = 64

sarah2248

Thanks so much for all your help. This helps a lot.

silkyguy21

hi sarah,

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

Thanks

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

Thanks

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.

rwheeler23

CEHJ

sarah2248, can you tell me why you ignored my previous contributions to this multipart question?

sarah2248

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.

CEHJ

>>but I don't know how to select multiple answers

Then you must learn

Then you must learn

Get an unlimited membership to EE for less than $4 a week.

Unlimited question asking, solutions, articles and more.

The interface is declared then implemented

>> one of the parenthesis ends with semicolon..

What are you referring to here?