Common categories and methods in Scala Collections
Scala Collection Compat is a library for compatible operations between Scala 2.13 and 2.12.It provides some commonly used methods and methods that enable developers to share the same code in different versions of SCALA.This article will introduce these commonly used classes and methods, and will provide some related Java code examples.
In the Scala Collection Compat, there are several commonly used classes, namely `Arrayseq`,` ArrayDeque` and `Queue`.
1. `arrayseq`: This class provides an array -based non -variable sequence.In Scala 2.13, `arrayseq` has been used as part of the standard set library, but it does not exist in SCALA 2.12.By using Scala Collection Compat, we can share the same code between the two versions.
The following is an example of Java code, which shows how to use `arrayseq`:
import scala.collection.compat.immutable.ArraySeq;
public class ArraySeqExample {
public static void main(String[] args) {
ArraySeq<Integer> arraySeq = ArraySeq.of(1, 2, 3, 4, 5);
System.out.println (arrayseq.head ()); // Output: 1
System.out.println (arrayseq.tail ()); // Output: ArraySeq (2, 3, 4, 5)
}
}
2. `ArrayDeque`: This class provides a two -terminal queue based on array.In Scala 2.13, `ArrayDeque` is already part of the standard set library, but it does not exist in SCALA 2.12.By using Scala Collection Compat, we can share the same code between the two versions.
The following is an example of Java code, which shows how to use `arraydeque`:
import scala.collection.compat.mutable.ArrayDeque;
public class ArrayDequeExample {
public static void main(String[] args) {
ArrayDeque<Integer> arrayDeque = new ArrayDeque<>();
arrayDeque.append(1);
arrayDeque.append(2);
arrayDeque.prepend(0);
System.out.println (ArrayDeque.mkstring (","); // Output: 0, 1, 2
}
}
3. `Queue`: This class provides an immutable queue.In Scala 2.13, `Queue` is already part of the standard set library, but it does not exist in SCALA 2.12.By using Scala Collection Compat, we can share the same code between the two versions.
The following is an example of Java code, showing how to use `Queue`:
import scala.collection.compat.immutable.Queue;
public class QueueExample {
public static void main(String[] args) {
Queue<Integer> queue = Queue.empty();
queue = queue.enqueue(1);
queue = queue.enqueue(2);
System.out.println (queue.dequeue () ._ 1 ()); // Output: 1
System.out.println (queue.dequeue () ._ 2 ()); // Output: queue (2)
}
}
By using these commonly used categories and methods in the Scala Collection Compat, we can share the same code between Scala 2.12 and 2.13, and then simplify the work of cross -version development.Hope this article will help you!