简体   繁体   中英

example of using scala.collection.immutable.Set from java

Does anyone out there who's familiar with Scala know how I could use scala.collection.immutable.Set from Java? I can vaguely read the scaladoc, but am not sure how to call scala methods like "-" from java (I assume that I just need to include some scala .jar file in my classpath...?)

Scala writes those special symbols out as $plus, $minus, etc. You can see that for yourself by running javap against scala.collection.immutable.HashSet.

That allows you to do code like this:

Set s = new HashSet<String>();
s.$plus("one");

Not pretty, and it doesn't actually work at runtime! You get a NoSuchMethodError. I'm guessing it's related to this discussion . Using the workaround they discuss, you can get things working:

import scala.collection.generic.Addable;
import scala.collection.generic.Subtractable;
import scala.collection.immutable.HashSet;
import scala.collection.immutable.Set;

public class Test {
    public static void main(String[] args) {
        Set s = new HashSet<String>();
        s = (Set<String>) ((Addable) s).$plus("GAH!");
        s = (Set<String>) ((Addable) s).$plus("YIKES!");
        s = (Set<String>) ((Subtractable) s).$minus("GAH!");
        System.out.println(s); // prints Set(YIKES!)
    }
}

Isn't that a beauty!?

I believe Java 7 is going to allow funky method names to be escaped, so maybe by then you'll be able to do

s = s.#"-"('GAH!')

To try this, you need scala-library.jar from the lib/ folder of the Scala distribution.

Update : fixed Java 7 syntax, thanks Mirko.

you can use this if its only for init a Set of less then 5 items

import scala.collection.immutable.Set;

Set mySet = (Set<String>)new Set.Set1<String>("better")
Set mySet = (Set<String>)new Set.Set2<String>("better","andmore")

Another Way to do it is as follows:

import scala.collection.JavaConversions$;
import scala.collection.immutable.Set;
import scala.collection.immutable.Set$;


//code

 java.util.HashSet hashsSet = new java.util.HashSet<String>();
        hashsSet.add("item1");
        hashsSet.add("item2");
        hashsSet.add("item3");
        hashsSet.add("item4");
        hashsSet.add("item5");
        // this is the mutable set of scala
        scala.collection.mutable.Set scalaSet = JavaConversions$.MODULE$.asScalaSet(hashsSet);
//this is immutable set
        Set immutable = scalaSet.toSet();

        System.out.println(immutable);

Based on Adam's answer, the following works fine for me with Scala 2.7.7 under Eclipse:

package com.example.test.scala;

import scala.collection.immutable.HashSet;
import scala.collection.immutable.Set;

public class ImmutableSetTest1 {
    public static void main(String[] args) {
        Set s0 = new HashSet<String>();
        Set[] s = new Set[3];
        s[0] = s0.$plus("GAH!");
        s[1] = s[0].$plus("YIKES!");
        s[2] = s[1].$minus("GAH!");

        for (int i = 0; i < 3; ++i)
            System.out.println("s["+i+"]="+s[i]);
    }
}

which prints:

s[0]=Set(GAH!)
s[1]=Set(GAH!, YIKES!)
s[2]=Set(YIKES!)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM