Tum
Tum

Reputation: 3652

Can we check complicated condition by just using List and its size as in this case?

There is a Item table, each item has a maximum of 3 different actions (act1, act2, act3), but some item only has 1 or 2 acts. Also, some item has duplicated act like item #2 has 2 entries of act3.

ItemID - ActionID
1 - act1
1 - act2
1 - act3
2 - act1
2 - act3
2 - act3
3 - act1
3 - act3

Let say we have a method to getItemInfo(List<Integer> itemList) to get result of item Info, the condition is that the result is only valid if each item containing all 3 acts & all items in the itemList must be appear in the result & finally there is no duplicated in the result.

List<Integer> itemList=new ArrayList<Integer>();
itemList.add(1);
itemList.add(3);

then run getItemInfo(List<Integer> itemList)

The Result will be valid (met all the conditions) like this example :

1 - act1
1 - act2
1 - act3
3 - act1
3 - act2
3 - act3

The Result will be invalid like this example (cos item #1 does not contain act3:

1 - act1
1 - act2
3 - act1
3 - act2
3 - act3

The Result will be invalid like this example (cos item #1 contains duplicates act2):

1 - act1
1 - act2
1 - act2
1 - act3
3 - act1
3 - act2
3 - act3

The Result will be invalid like this example (cos the result does not contain item#3):

1 - act1
1 - act2
1 - act3

So here is 1 solution, that is i put all the result in the List and check duplicated, then I check the size, ex:

//run via loop to put all result into a `List<String[]> resultList` 
& this `resultList` contains `String[]` like    
String[] s1={"1","act1"}
String[] s2={"1","act2"}
.....

Then

    if(!MyValidation.isDuplicated(resultList) && 
       resultList.size()==3*itemList.size() ){
          System.out.println("valid");
    }
    else{
         System.out.println("not valid");
    }

My simple question is that, Can if(!MyValidation.isDuplicated(resultList) && resultList.size()==3*itemList.size() ){} be used for checking all the conditions mentioned above?

Upvotes: 0

Views: 48

Answers (1)

Axel Amthor
Axel Amthor

Reputation: 11096

Simply: No.

decomposition:

 MyValidation.isDuplicated(resultList)

will check for any duplicates, that is ok, but

  resultList.size()*itemList.size()==3*itemList.size() 

is the same as

 resultList.size() == 3

(just mathematics: if a*b=3*b then a=3) and that's too simple.

Upvotes: 1

Related Questions