英文:
Java Static Factory Class like Scala
问题
Sure, here's the translated Java code for your Scala code:
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
class Writer {
private static Map<String, String> options = new HashMap<>();
public static Optional<String> a() {
return Optional.ofNullable(options.get("a"));
}
public static Optional<String> b() {
return Optional.ofNullable(options.get("b"));
}
public static Optional<String> c() {
return Optional.ofNullable(options.get("c"));
}
public static Writer option(String key, String value) {
options.put(key, value);
return Writer.this;
}
public static void write() {
System.out.println(a().orElse("Z"));
System.out.println(b().orElse("Z"));
System.out.println(c().orElse("Z"));
}
}
public class Run {
public static void main(String[] args) {
Writer.write();
Writer.option("a", "A")
.option("b", "B")
.option("c", "C")
.write();
}
}
Please note that the translation from Scala to Java might not be a straightforward one-to-one mapping, as the languages have different syntax and features. In this translation, I've used Optional
for handling the absence of values, and I've adjusted the method and field access to Java conventions.
英文:
I have written a sample code in Scala and I need to convert to Java, but I am kinda confused how I can have this pattern in Java,
In Scala I have:
object Writer {
val options = new scala.collection.mutable.HashMap[String, String]
def a: Option[String] = options.get("a")
def b: Option[String] = options.get("b")
def c: Option[String] = options.get("c")
def option(key: String, value: String): Writer.type = {
options += (key -> value)
this
}
def write() ={
println(a.getOrElse("Z"))
println(b.getOrElse("Z"))
println(c.getOrElse("Z"))
}
}
Which I can run with or without passing the option(s):
object Run {
def main(args: Array[String]): Unit = {
Writer
.write()
Writer
.option("a", "A")
.option("b", "B")
.option("c", "C")
.write()
}
}
How can I convert this to Java?
答案1
得分: 2
Scala的对象就像Java中的单例对象:
import scala.collection.mutable.HashMap
object Writer {
private val options: HashMap[String, String] = new HashMap[String, String]()
def a(): Option[String] = {
options.get("a")
}
def b(): Option[String] = {
options.get("b")
}
def c(): Option[String] = {
options.get("c")
}
def option(key: String, value: String): this.type = {
options(key) = value
this
}
def write(): Unit = {
println(a().getOrElse("Z"))
println(b().getOrElse("Z"))
println(c().getOrElse("Z"))
}
def main(args: Array[String]): Unit = {
Writer
.option("a", "A")
.option("b", "B")
.option("c", "C")
.write()
Writer.write() // 与上面的输出相同
}
}
请注意,Scala中的单例对象使用object
关键字定义,其内部成员和方法可以通过点符号访问,类似于Java中的静态成员和方法。
英文:
Scala object is like singleton object in Java:
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
public class Writer {
private Writer(){}
private static Writer instance;
public static Writer getInstance() {
if (instance == null) {
instance = new Writer();
}
return instance;
}
public Map<String, String> options = new HashMap<>();
public Optional<String> a() {
return Optional.ofNullable(options.get("a"));
}
public Optional<String> b() {
return Optional.ofNullable(options.get("b"));
}
public Optional<String> c() {
return Optional.ofNullable(options.get("c"));
}
public Writer option(String key, String value) {
options.put(key, value);
return this;
}
public void write() {
System.out.println(a().orElse("Z"));
System.out.println(b().orElse("Z"));
System.out.println(c().orElse("Z"));
}
public static void main(String[] args) {
Writer.getInstance()
.option("a", "A")
.option("b", "B")
.option("c", "C")
.write();
Writer.getInstance().write(); // same output as above
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论