335 questions
3
votes
0
answers
99
views
How to override a varargs java method in a scala class which is still callable in both scala and java variadically?
Assume there is a base class in a Java library which cannot be modified
public class BaseClass {
public Something function(int... args){ ... }
}
Typically, this would be overridden in Scala by ...
0
votes
3
answers
147
views
How to convert Scala `for..yield` (for comprehension) to Java?
How to convert the following scala to java code?
for {
x <- List(1, 2)
y <- List(3, 4)
} yield (x, y)
Is it possible? what is yield?
I guess it's possible to convert any scala code to java.....
1
vote
1
answer
97
views
Subclassing and overriding Java class with protected field access
The scala project is depending on the Java lib that I cannot modify. I need to extend one of the Java classes and override method, however I have issues with protected visibility:
package com.a;
...
2
votes
1
answer
73
views
Scala class implementing Java interface - double definition with same erasure
I hava a Java interface:
public interface FooJava {
void consume(String... strings);
void consume(Integer... ints);
}
and I want to implement it in Scala:
class BarScala extends FooJava {
...
0
votes
1
answer
64
views
Scala class implementing Java interface - how to implement method taking array of generic type
I hava a Java interface:
public interface FooJava<Element> {
void consume(Element[] elements);
// more methods
}
and I want to implement it in Scala:
// attempt 1
class BarScala[T] extends ...
0
votes
1
answer
134
views
invoking scala generic classes from java looses generic info
trying to use in java class defined in scala library.
Definition:
final class ScenarioBuilder(...) extends StructureBuilder[ScenarioBuilder]
trait StructureBuilder[B <: StructureBuilder[B]] extends ...
0
votes
0
answers
26
views
how to convert java stream to scala stream? [duplicate]
want to iterate thru bidChoices list and map using scala, below is the java code. what to perform the same using scala.
private String generateRestBody(String baseUrl, String auctionId, String ...
0
votes
1
answer
177
views
Scala: type hint for lambda
I'm refreshing scala. This looks very simple to me but I can't get it to run:
import java.nio.file.{FileSystems, Files}
object ScalaHello extends App {
val dir = FileSystems.getDefault.getPath(&...
0
votes
0
answers
142
views
How to declare optional basic type in scala e.g. Option[Int] so that the type gets seen as Option<Integer> from java?
Consider the following scala class:
class Demo {
def mandatoryInt: Int = 42
def optInt: Option[Int] = Some(1)
def optString: Option[String] = Some("demo")
}
When its methods are ...
0
votes
1
answer
728
views
Import Scala object within object in Java
I have an object within another object in Scala which I want to import in Java. The setup is something like this:
Scala:
package scalapart;
object Outer {
object Inner {
def method: ...
3
votes
2
answers
155
views
Instanciating POJO with nulls values from Scala Option
Many years of using Scala and still I don't know the right way to interoperate with Java. :(
To be honest, this is not something that I'm doing every day, but sometimes there is no other option (today ...
1
vote
1
answer
230
views
Implementing Java interface in Scala results in incompatible type map
I have a Java interface that I want to implement in Scala. The interface contains the following method-declaration:
List<Map<String, Object>> xyz(Map<String, Object> var1, Map<...
0
votes
2
answers
146
views
couldn't extend scala class from java class which has abstract function which takes inner class as parameter
I've scala - java polyglot project with scala version 2.13
Below is the standard project structure
$scala-java-extend-demo on master
± tree .
.
├── build.gradle.kts
├── gradlew
...
2
votes
0
answers
173
views
java/scala incompatible types, when casing (or using instanceof) a scala class to java class, even though the scala class extends the java class
this is pretty much the code setup:
// java
public class JavaA {
}
// scala
class ScalaA() extends JavaA {
}
but then in java, if i have an instance of ScalaA somewhere (or even if its null), but ...
2
votes
1
answer
302
views
How to call Scala curry functions from Java with Generics
A Scala code has a retry mechanism which is based on currying function:
object RetryUtil {
def retry[T](retry: Int, timeout: FiniteDuration)(exc: => T): T = {
//
}
}
I want to call this code ...
1
vote
2
answers
590
views
Pass subclass of class as type parameter (type parameter is subclass)
let's consider:
public class Text extends BinaryComparable
implements WritableComparable<BinaryComparable> {
We can see that Text is BinaryComparable.
Then, let's consider
@...
1
vote
0
answers
208
views
Scala case class extends java class
I have a normal Java class BaseModel as below
@AllArgsConstructor
class BaseModel {
private String id;
}
Now I want to extend this in scala case class. Below is giving error:
case class Employee(...
0
votes
1
answer
733
views
How does implicit conversions between scala-java collections work? How is it possible that it is sufficient to import some package?
import scala.collection.JavaConversions._
val j: java.util.Map[String, String] = Map("a" -> "b")
print (j)
How does it work? In other words I am dying to know how import of ...
1
vote
1
answer
206
views
How to access scala object inside scala class in java?
I have scala class like:
class A {
object B {
def c(d: Int) = d + 4
}
}
How do i access function c from a java code?
Edit: assume that I can't change scala class anyhow.
Edit#2:
Here is ...
0
votes
1
answer
340
views
Use functions from a Scala object that have implicit parameters in java code
Is there a way that I can use the fromJson and toJson methods in my java code? I would want to call it to transform a bson document that I get from mongoldb to a model class that I have in Scala
My ...
-4
votes
1
answer
164
views
What does this scala error mean about type mismatch? [closed]
type mismatch;
found : String => Int
required: java.util.function.ToIntFunction[_ >: String]
I really do wish before people wrote up the "next big thing" they got a few ...
1
vote
2
answers
340
views
unable to convert a java.util.List into Scala list
I want that the if block returns Right(List[PracticeQuestionTags]) but I am not able to do so. The if/else returns Either
//I get java.util.List[Result]
val resultList:java.util.List[Result] = ...
3
votes
2
answers
314
views
Interoperability : sharing Datasets of objects or Row between Java and Scala, two ways. I put a Scala dataset operation in the middle of Java ones
Currently, my main application is built with Java Spring-boot and this won't change because it's convenient.
@Autowired service beans implements, for example :
Enterprise and establishment datasets. ...
2
votes
2
answers
528
views
Using reflection to override 2 + 2 = 5
Bear with me, I understand this is a weird problem to have.
I have just stumbled across Java's reflection library, specifically this bit of code from a video by Lex Fridman which overrides 2 + 2 = 5:
...
1
vote
2
answers
395
views
Access scala class fields in java
I have defined the class A in Scala:
class A(var number: Int)
But when I try to access its member field number in java, I get an error:
A a = new A();
a.number = 4;
Results in: java: number has ...
1
vote
1
answer
129
views
Compilation error with bounded wildcards using Java classes in Scala
In Java, we have defined an ObservableCollection.java like this:
public class ObservableCollection<T> implements Collection<T> {
public SubscriptionHandle onElementAdded(Consumer<T&...
0
votes
1
answer
853
views
Converting from scala.collection.immutable.List to a java.util.List in java [duplicate]
I’m currently working within a java runtime google cloud dataflow. The scala sdk I'm using shows the property I'm working with as an immutable list: https://github.com/snowplow/snowplow-scala-...
1
vote
0
answers
320
views
How to call Scala implicit class method from Java class
I'm a beginner programmer working within a mixed Java/Scala codebase. I have a implicit class in Scala that provides extensions to a class within a third-party dependency. The implicit class is as so:
...
0
votes
2
answers
129
views
Where is the Object class or java.lang imported into the scala package or Any class?
From my understanding the ultimate class in Scala is Any class. However, I thought Scala built of the Java, so would not the ultimate class be Object? I have been checking the documentation and I ...
0
votes
1
answer
268
views
Alias a Java method in Scala
Given a Java interface
interface Value {
Value add(Value argument);
}
(since Java does not support symbols like + as method names), is it possible to define an alias method + to alias add such ...
0
votes
1
answer
325
views
Scala inheritance from Java class with generic types in constructor
How can I extends a Java class that expects a Map<?, ?> in its constructor in Scala?
The Java class looks like
public class MyJavaClass {
private final Map<String, ?> originals;
...
0
votes
3
answers
414
views
How to covert java predicate to scala predicate
I have a java method which is accepting
predicate(Predicate<T> predicate)
my java class is
class Employee {
String getEmployeeId() {
return "";
}
boolean isManager() {
return ...
4
votes
2
answers
442
views
'should contain allOf' on java.util.Map?
How do I do a contains allOf assertion on a Java Map?
The following did not work
val testMap = new java.util.LinkedHashMap[Int, Int]()
testMap.put(1,2)
testMap.put(2,4)
testMap.put(3,6)
testMap ...
1
vote
0
answers
544
views
Getting the following error : java.lang.NoSuchMethodError: scala.Predef$.wrapRefArray([Ljava/lang/Object;)Lscala/collection/mutable/WrappedArray;
Getting the following error :
java.lang.NoSuchMethodError:scala.Predef$.wrapRefArray([Ljava/lang/Object;)Lscala/collection/mutable/WrappedArray;
I'm not sure about the nature of the error but I ...
2
votes
1
answer
2k
views
scala.collections.javaconverters._ Error : value map is not a member of java.util.Set
I have been using scala.collections.JavaConversions for implicitly converting, but decided to upgrade the scala version and use scala.collections.JavaConverters instead, I'm confused with the explicit ...
0
votes
1
answer
951
views
Non-variable type argument String in type Map[String,Any]
I have a simple method to retrieve a nested key from a hashmap. I need to pattern match on Map[String,Any] so that I can keep iterating into the nested data until I get to a flat value:
def get(map: ...
1
vote
2
answers
281
views
type JavaConversions is not applicable for the arguments (Set<String>)
Here is the code snippet :
protected static List<String> myclass(String mydata){
scala.collection.Set<String> abc = <**some code that returns set of strings**(scala code)>
...
0
votes
0
answers
120
views
Scala type mismatch from Java library
I am trying to use a Java library (Hipster) with Scala. Running the example on the front page, I get this type error message which I can't make sense of:
My attempt so far was to try convert Java <...
3
votes
1
answer
2k
views
JAVA - Instantiate Scala case class
after few days of search, I need your help to solve my problem.
I have a java program and I want to call a library written in scala, the jar is in classpath.
Scala main class:
object Program{
def ...
1
vote
1
answer
169
views
Doing math operations on Scala objects in Java code
In my java code I am using a method, let's call foo(), that returns a scala.Double[].
scala.Double[] arr = foo();
I want to sum up arr into a scala.Double, but am having trouble. I've found that ...
12
votes
3
answers
3k
views
Scala Option(null) expected as None but I got Some(0)
val i: java.lang.Integer = null
val o: Option[Int] = Option(i) // This yields Some(0)
What is the safe way to convert null: java.lang.Integer to Scala Option[Int]?
1
vote
0
answers
77
views
Rewrite methods with generic with interface type Bound and implicit
I have a trait with generic and two alternative methods for checking age and name of employee.
trait Conditions[T] {
def nameIs(color: String): T
def ageUpperThan(upper: Int): T
}
def ...
2
votes
1
answer
141
views
CollectionConverters always return the original source object?
CollectionConverters API states
In all cases, converting from a source type to a target type and back
again will return the original source object.
This does not seem to be always true. For ...
3
votes
1
answer
214
views
Scala-Java interop, issue with overloading of methods (Array and varargs)
I have a Scala class which has two overloaded set methods,
one with an Array param and the other with varargs.
I want to call these methods from Java side, I am facing some issues due to overloading ...
1
vote
1
answer
1k
views
Make An Empty Constructor for Scala Class
If I have a case class like this:
case class Foo(s : String, k : Int)
How do I specify an empty constructor for this? The reason I need this is because I want to pass this class value to a Java API ...
3
votes
1
answer
554
views
Scala public becomes private when accessed from Java?
I am quite familiar with Java and Maven, but not at all with Scala. I am trying to access Scala from Java and one of the problems I can't seem to understand is the following:
My Question
In ...
0
votes
1
answer
455
views
Type mismatch when call java generic Java method from Scala code
I have a n-quantity of Java classes with one superclass - data model.
The list of the classes is input parameter to Scala method in which I want to create
resultStreams and have to create call Java ...
4
votes
1
answer
131
views
Costly performance of CollectionConverters despite being wrappers?
AFAIU, CollectionConverters are wrappers simply delegating calls to the underlying collection, thus the cost should be single object allocation and then single method call indirection, for example, ...
2
votes
1
answer
550
views
Can't perform HmacSHA256 hashing in Scala.js
I'm trying to implement a method that will calculate & validate the HmacSHA256 signature for verifying a slack request. For... various reasons... I'm using scala.js, and therefore I don't have ...
7
votes
1
answer
520
views
Calling Java Generic Typed Method from Scala gives a Type mismatch error : Scala
This is my very first question down here, so i'll try to make it clear as far as i can.
Other error: type mismatch; questions here are not related to this error.
I have this odd problem with scala/...