0

Is there any way to convert a nested arrayBuffer to a nested array in scala ? I tried using the toArray function, but it did not convert the nested arrayBuffers

I have an array buffer of type Any and following is my sample nested

ArrayBuffer(
ArrayBuffer(ArrayBuffer(ArrayBuffer(1, b), 5)))
10
  • what have you done so far? can you show the example? Commented Jun 14, 2017 at 13:17
  • I have an array buffer of type Any This is probably an XY (meta.stackexchange.com/questions/66377/what-is-the-xy-problem) question. You don't want to do that. Please explain a bit about what you're trying to do. Commented Jun 15, 2017 at 17:15
  • I want to write a nested arraybuffer to a dataframe. So for that I need to convert the arraybuffer to a nested seq so that I can pass it to a spark row Commented Jun 16, 2017 at 6:09
  • Here's the code I'm trying to use. def convert(a: Any): Array[Any] = a match { case ArrayBuffer(head @ ArrayBuffer(*), tail @ ArrayBuffer()) => Array(Row.fromSeq(convert(head)), Row.fromSeq(convert(tail))) case ArrayBuffer(inner @ ArrayBuffer(_)) => Array(Row.fromSeq(convert(inner))) case ArrayBuffer(head @ ArrayBuffer(*), tail @ _*) => (Row.fromSeq(convert(head)) +: tail).toArray case arr @ ArrayBuffer(*) => Row(arr.toArray).toSeq.toArray } @TheArchetypalPaul Commented Jun 16, 2017 at 8:50
  • Edit the question. But I'm not asking how you are trying to convert it, I'm asking why you think you need an array buffer of type Any in the first place. It's almost never a good idea to try to work with Any. Commented Jun 16, 2017 at 10:07

2 Answers 2

1

The code below works for your specific test case. It converts nested ArrayBuffers that are the first element of the enclosing ArrayBuffer:

def convert(a: Any): Array[Any] = a match {
  case ArrayBuffer(inner @ ArrayBuffer(_*)) => Array(convert(inner))
  case ArrayBuffer(head @ ArrayBuffer(_*), tail @ _*) => (convert(head) +: tail).toArray
  case arr @ ArrayBuffer(_*) => arr.toArray
}

val result = convert(ArrayBuffer(ArrayBuffer(ArrayBuffer(ArrayBuffer(1, "b"), 5))))
// result is Array(Array(Array(Array(1, "b"), 5)))

Perhaps the above will help you come up with a more general solution.

Sign up to request clarification or add additional context in comments.

1 Comment

I have posted the generic function. @chunjef
0

I have created a generic function to convert arraybuffer to array.This takes care of nested arrayBuffers also

def convert(a: ArrayBuffer[Any]): Array[Any] = {

  val checkExistance = (x:ArrayBuffer[Any]) => x.zipWithIndex.collect{ case(x,y) if x.isInstanceOf[ArrayBuffer[Any]] => y}.toArray
  //filter(x => x.isInstanceOf[ArrayBuffer[Any]])
  val arr = checkExistance(a)
  for(i <- arr) {
    //if (a(i).isInstanceOf[ArrayBuffer[Any]]) {
    val m = a(i).asInstanceOf[ArrayBuffer[Any]]
    if(checkExistance(m).length > 0) {
      a.update(i,(convert(m)).toArray)
    }
    else {
      val n = m.toArray
      a.update(i,n)
    }
  }
  a.toArray
}

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.