0

I have following three lists:

List((List(vmnic2),"VM Network",10,"vSwitch0"), (List("vmnic2"),"Management Network",0,"vSwitch0"))
List(List("vmnic2"))
List("VM Network 2", "VM Network 3", "VM Network")

I want to do following:

1) I want to check whether first list contains second list and 2) Any of element from third list matches with second value in any element of first list.

From above three list I want output as

List(List("vmnic2"),"VM Network",10,"vSwitch0")

How do I get above output using scala??

5
  • 2
    Have you considered using case classes instead of lists? They seem to work better than these heterogenous (Any Lists). Commented Nov 17, 2014 at 13:14
  • Actually I am using case classes for it. I have three different classes for these three lists.. Commented Nov 17, 2014 at 13:17
  • Your example is not valid scala code. That makes it hard for people to understand what you want and help you. Commented Nov 17, 2014 at 13:19
  • What do you mean by "contains" in your first query? That the head of the second list appears as the first element of one of the lists in the first list? Commented Nov 17, 2014 at 13:20
  • @paul you are right.. Commented Nov 17, 2014 at 13:22

1 Answer 1

2

Try this:

list1.filter(e =>
  e._1.intersect(list2(0)) == list2(0)  // check if first element contains second list
    && list3.contains(e._2))            // check if third list contains second element
Sign up to request clarification or add additional context in comments.

4 Comments

Not in your example it isn't. if it can't be known to contain Lists of something, it's a bit more difficult. Please add to your example enough to demonstrate it's a List[Any]
@Vishwas So what means first list contains second list?
And if it's List[Any] attempts to pattern match any lists it does contain are defeated by type erasure. I think your design needs re-visiting.
@sergey: how to match if I change list2 to List(("vmnic2","vSwitch0"),("vmnic1",vSwitch1)). In short how do I match above given list to list1 instead of using list2(0)) == list2(0)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.