blue-sky
blue-sky

Reputation: 53786

How to convert a collection conversion using java to scala

Below code converts a List of type List<TestObj> to a list of type List<List<String>> where each List element in type List<List<String>> is a String List of size 1. This is a very iterative approach, can this be converted to a more functional method using scala ? A possible solution : Pattern match on List<String> and just create a new List with every head element ?

Output of below java code :

strVal is 1 
New list 
strVal is 2 
New list


public class Driver {

        public static void main (String args[]){

        /**
         * Setup the test list data
         */
        List<String> l = new ArrayList<String>(); 
        l.add("1");
        l.add("2");     
        TestObj t = new TestObj();
        t.setTestList(l);
        t.setName("test");
        List<TestObj> tList = new ArrayList<TestObj>();
        tList.add(t);

        /**
         * Convert the list to a new data structure
         */
        List<List<String>> l3 = new ArrayList<List<String>>();
        List<String> l2 = new ArrayList<String>();      
        int counter = 0;
        for(TestObj tListElement : tList){ 
            if(tListElement.getName().equalsIgnoreCase("test")){
            List<String> lvo = tListElement.getTestList();      
            for(String lv : lvo){
                l2.add(lv);
                ++counter;
                if(counter == 1){
                    counter = 1;
                    l3.add(l2);
                    l2 = new ArrayList<String>();
                    counter = 0;
                }
            }
        }
        }

        /**
         * Output the values of the new data structure
         */
        for(List<String> listVals : l3){
            for(String strVal : listVals){
                System.out.println("strVal is "+strVal);
            }
            System.out.println("New list");
        }
    }
}

import java.util.List;


public class TestObj {

    private String name;
    private List<String> testList;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public List<String> getTestList() {
        return testList;
    }
    public void setTestList(List<String> testList) {
        this.testList = testList;
    }

}

Upvotes: 4

Views: 175

Answers (1)

Ptharien&#39;s Flame
Ptharien&#39;s Flame

Reputation: 3246

case class TestObj(name: String, testList: Seq[String])

object Driver extends App {

  // Setup the test list data
  val tList = Seq(TestObj("test", Seq("1", "2")))

  // Convert the list to a new data structure
  val l3 = for {
      t <- tList
      if t.name equalsIgnoreCase "test"
      lv <- t.testList
    } yield Seq(lv)

  // Output the values of the new data structure
  for (listVals <- l3) {
    for (strVal <- listVals) {
      println("strval is " + strVal)
    }
    println("New list")
  }

}

Upvotes: 5

Related Questions