0

I have following list -

List(List(
List(((groupName,group1),(tagMember,["192.168.20.30","192.168.20.20","192.168.20.21"]))), 
List(((groupName,group1),(tagMember,["192.168.20.30"]))),
List(((groupName,group1),(tagMember,["192.168.20.30","192.168.20.20"])))))

I want to convert it to -

List((groupName, group1),(tagMember,["192.168.20.30","192.168.20.20","192.168.20.21"]))

I tried to use .flatten but unable to form desired output.

How do I get above mentioned output using scala??

5
  • 1
    Can you simplify your example into some code we can copy into a repl? And maybe use shorter strings? Commented Oct 20, 2014 at 6:26
  • I used list.flatten to remove flatten list but have no idea how to merge list by removing duplicate elements Commented Oct 20, 2014 at 6:32
  • 3
    @Vishwas: ll.flatten.distinct Commented Oct 20, 2014 at 6:33
  • Is the IP address the only variable part in the list, or could there be other entries in the list like (groupName, group2)? Commented Oct 20, 2014 at 8:37
  • This doesn't seem like a valid Scala code. Commented Oct 20, 2014 at 8:42

1 Answer 1

1

I had to make some changes to your input to make it valid.

Input List:

   val ll = List(List(
      List((("groupName","group1"),("tagMember", List("192.168.20.30","192.168.20.20","192.168.20.21")))),
      List((("groupName","group1"),("tagMember",List("192.168.20.30")))),
      List((("groupName","group1"),("tagMember",List("192.168.20.30","192.168.20.20"))))
    ))    

Code below works if the group, and tagMember are the same across all the elements in the list

  def getUniqueIpsConstantGroupTagMember(inputList:  List[List[List[((String, String), (String, List[String]))]]]) = {
      // List[((String, String), (String, List[String]))]
      val flattenedList  = ll.flatten.flatten

      if (flattenedList.size > 0) {
         val group = flattenedList(0)._1
         val tagMember = flattenedList(0)._2._1
         val ips = flattenedList flatMap (_._2._2)

         ((group), (tagMember, ips.distinct))
      } 
         else List()
  }

  println(getUniqueIpsConstantGroupTagMember(ll))

Output:

((groupName,group1),(tagMember,List(192.168.20.30, 192.168.20.20, 192.168.20.21)))

Now, let's assume you could have different groupNames.

Sample input:

  val listWithVariableGroups = List(List(
      List((("groupName","group1"),("tagMember",List("192.168.20.30","192.168.20.20","192.168.20.21")))),
      List((("groupName","group1"),("tagMember",List("192.168.20.30")))),
      List((("groupName","group1"),("tagMember",List("192.168.20.30","192.168.20.20")))),
      List((("groupName","group2"),("tagMember",List("192.168.20.30","192.168.20.10"))))   
    ))

The following code should work.

  def getUniqueIpsForMultipleGroups(inputList:  List[List[List[((String, String), (String, List[String]))]]]) = {
    val flattenedList = inputList.flatten.flatten

    // Map[(String, String),List[(String, List[String])]]
    val groupedByGroupNameId = flattenedList.groupBy(p => p._1) map {
      case (key, value) => (key, ("tagMember", extractUniqueTagIps(value)))
    }
    groupedByGroupNameId
  }

  def extractUniqueTagIps(list: List[((String, String), (String, List[String]))]) = {
    val ips = list flatMap (_._2._2)
    ips.distinct
  }

  getUniqueIpsForMultipleGroups(listWithVariableGroups).foreach(println)

Output:

((groupName,group1),(tagMember,List(192.168.20.30, 192.168.20.20, 192.168.20.21)))
((groupName,group2),(tagMember,List(192.168.20.30, 192.168.20.10)))
Sign up to request clarification or add additional context in comments.

Comments

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.