如何使用java将集合转换为scala

如何使用java将集合转换为scala,java,scala,scala-java-interop,Java,Scala,Scala Java Interop,下面的代码将类型列表转换为类型列表 其中,类型列表中的每个列表元素都是 1号。这是一种非常迭代的方法,可以转换成更实用的方法吗 使用scala的方法? 一个可能的解决方案是:在列表上进行模式匹配,然后使用 每个头部元素 以下java代码的输出: strVal is 1 New list strVal is 2 New list public class Driver { public static void main (String args[]){

下面的代码将类型列表转换为类型列表 其中,类型列表中的每个列表元素都是 1号。这是一种非常迭代的方法,可以转换成更实用的方法吗 使用scala的方法? 一个可能的解决方案是:在列表上进行模式匹配,然后使用 每个头部元素

以下java代码的输出:

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;
    }

}

阅读和翻译Java代码的功劳。我使用Java已经14年了,使用Scala已经3年了。对我来说,找出Scala代码的作用要快得多。我甚至不知道Java代码是做什么的…@Ptharien's Flame如果可以的话,我会投几次票,谢谢。
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")
  }

}