6 Replies Latest reply: Jun 20, 2014 11:41 AM by gimbal2 RSS

    List question

    961190

      Hi All

       

      I have a super list and it has 10 elements for example

       

      superList = {1,2,3,4,5,6,7,8,9,0}

       

      I have a sub list which has 3 elements

       

      subList = {1,2,3}

       

      I want to know if the superList only has elements in sublist and not any additional elements. Say in this case the superList has {4,5,6,7,8,9,0} it should throw false

       

      also the superList need not have all the values of the sublist

      Example 1:

      superList = {1}

      subList = {1,2,3}

      Result = true

       

      Example 2:

      superList = {1,2}

      subList = {1,2,3}

      Result = true

       

      Example 3:

      superList = {1,2,3}

      subList = {1,2,3}

      Result = true

       

      Example 4:

      superList = {1,2,3,1}

      subList = {1,2,3}

      Result = true

       

      Example 3:

      superList = {1,2,3,4}

      subList = {1,2,3}

      Result = false

       

      How should i go about this, any comments would be of great help!

       

      Thanks

      Kirti

        • 1. Re: List question
          gimbal2

          Start by looking at the API documentation for the List interface and see what methods it has available. Perhaps there is something in there that might help you get closer to your goal?

           

          List (Java Platform SE 7 )

           

          You have to be creative with these kind of things and attack the problem from different angles. One really simplistic way that I can reword your problem description for example is "When I remove any element contained in sublist from the superList, the remaining list must be empty".

          • 2. Re: List question
            rp0428
            When I remove any element contained in sublist

            I think you perhaps meant to use ALL instead of ANY?

            • 3. Re: List question
              rp0428

              I want to know if the superList only has elements in sublist and not any additional elements.

              . . .

              How should i go about this,

              The time to think about solutions to a problem like that is during the design phase BEFORE you have decided on using lists.

               

              Factors affecting the proper 'solution' include:

              1. The types of objects being compared - are they primitives? Or might they be generic objects of any kind?

              2. How many elements might there be in each list? Unlimited for both lists? Small for one but large for the other?

              3. Where do the elements for each list come from? A database? Manual entry by a user? Properties file?

              4. Are the list entries sorted?

              5. Can either list have duplicates?

              6. What does 'not in' mean? The value of an element? An object reference? Or the actual element if you are dealing with objects.

              7. Is the result set needed? For example if list1 has elements not in list2 should the result be a list of the extra elements?

              8. How often does this 'problem' need to be 'solved'?

               

              Requirements come first. And the requirements should be as fully defined as possible BEFORE code is implemented.

               

              Poor requirements results in poor, non-scalable and non-performant  solutions.

               

              The best solution for a small number of elements is often the worst solution for a large number of elements.

               

              1. A database query can do the work if the list data comes from a DB

              2. Sorted lists can be used effectively depending on the number of elements

              3. Converting the short list to a HashMap and then probing the map with the other list can be effective

              4. If objects are being compared proper comparators need to be

               

              No specific solutions can be offered without knowing MUCH more about the requirements.

              • 4. Re: List question
                gimbal2

                Off-topic: funny that you should say that, it was a difficult choice for me actually. I chose 'any' because of the fact that not all elements in subList need to actually physically exist in the parentList.

                 

                Is it really incorrect?

                • 5. Re: List question
                  rp0428
                  Off-topic: funny that you should say that, it was a difficult choice for me actually. I chose 'any' because of the fact that not all elements in subList need to actually physically exist in the parentList.

                   

                  Is it really incorrect?

                  Correct - not all elements in subList need to be in the parent list. OPs examples already showed that.

                  Example 2:

                  superList = {1,2}

                  subList = {1,2,3}

                  Result = true

                  Item '3' is in the sublist but not in the main list. But that isn't what I was referring to. This is what you said:

                  When I remove any element contained in sublist from the superList, the remaining list must be empty

                  If you remove item '1' (which is in the sublist) from the main list the remaining list is NOT empty - it still has item '2' in it. Hence I said:

                  I think you perhaps meant to use ALL instead of ANY?

                  Which converts your statement to this:

                  When I remove any element contained in sublist from the superList, the remaining list must be empty

                  That statement is now what I think you meant. Of course it still depends on the actual requirements many of which I listed. What if there are two item '1's in the sub list. Are they to be considered TWO distinct items? Does the main list also have to have TWO of them? Are they considered to be TWO items if they compare equally even if they refer to different objects? Or are they the same item only if they refer to the same object? For either case what does the parent list need to have: one of them, both of them, two items that compare the same even if those two items are two totally different objects than the two objects in the sublist.

                   

                  It might be straightforward for primitives but it isn't for object references.

                  • 6. Re: List question
                    gimbal2

                    Darnit, I try to find an answer to my question in your reply but I have to make guesswork of it. I am going to make an assumption and hope for the best: Yes it is really wrong and the statement should have been:

                     

                    "When I remove all elements contained in sublist from the superList, the remaining list must be empty"