Id like to convert an existing kotlin/java set, to scala immutable set, using kotlin/java code.
scala.collection.JavaConversions.asScalaSet only gives me the mutable set.
Must do it that way, because I am inheriting from a scala class on another repo, and would not like to insert scala packages and plugins to my project.
A simple answer would be: you cannot. Java/Kotlin collections are inherently mutable, while Scala immutable collections have a completely different implementation on the inside, so they are not allowed to be freely converted from one to the other.
Now you have two options: first, you can use a more generic interface (scala.collection.Set for example), or, second, you can create a new immutable Scala collection and put all the elements into it: scala.collection.immutable.HashSet().concat(yourExistingCollection)
Related
Kotlin somehow considers java.util.Collection as extending kotlin.collections.MutableCollection.
Is there any way for this same functionality to be used by code written outside the Kotlin standard libraries?
e.g., I want to create my own Kotlin NavigableMap & MutableNavigableMap interfaces (with the latter extending the former). I want java.util.NavigableMap considered as extending my MutableNavigableMap.
I'm currently learning java in order to make an app on android and I checked that swift has a structure that stores information in memory. I'd like to know, if in java this type of object exists, because the class storage the reference on the memory. Also I checked that Kotlin has a data class, does java have a similar object?
No, there is nothing like that, but there are tools, that try to mimic this behavior, for example lombok. Using #Data annotation we're getting default constructor, getters, setters, toString, equals, hashCode. We can fine-tune it by using annotations like #Getter, #NoArgsConstructor etc.
Neither Java nor Kotlin have anything similar to those Swift types you are talking about. Assignment always copies references to an object, rather than the object itself. What Kotlin's data classes do is that they create a copy method (among other things) that allows you to explicitly make a copy of an object, but you still have to actually call the method.
val b = a // b and a point to the same object, even if it is a data class
val b = a.copy() // this is what you need to do to create a copy of a data class
Java assignment copies references, not objects, and the same is true for Kotlin. There is no way around this, because it is a feature of the language itself. Copy constructors and methods (like what Kotlin's data class gives you) are the closest thing you have to such a feature. To get something like this in Java without having to manually write the code everytime, you could look into Project Lombok.
Starting with Java 14 you will have access to Record immutable class. It is similar in concept to data class in Kotlin.
Is there any way to force an instance or a functional interface static method output to be inmutable in a fashion like Collections.immutable(x)?
I'd like for instance to create sort of Comparator functional interface and disallow chained operations like ".thenComparing()" for some of the static builder-like methods created instances.
You can force your return value to be immutable by returning an instance of an immutable class. There is no general purpose way to make instances of your class immutable without knowing what the class does.
The second paragraph of your question contains an incorrect assumption that .thenComparing(...) makes a comparator mutable. thenComparing leaves the original comparator intact and makes a new comparator.
Sadly, the Java collection types lack immutable collections, but google guava comes with lots of immutable collections (see https://code.google.com/p/guava-libraries/wiki/ImmutableCollectionsExplained), so you can use those as a return type.
Yet, I can't see how this correlates with the ability to compare something, since comparing doesn't change stuff.
I wonder why Java doesn't have a tuple data structure implementation in its standard library. For instance C++ has a very good implementation of this fixed-size collection of heterogeneous values. The same in Haskell. In Java I only know javatuples and some support in Functional Java library via Product (P1 - P8) types. I wonder why tuple or at least pair not in standard library at all? Even Android SDK developers added their own implementation of 2-tuple (pair).
The "Java way" is to define use-specific classes rather than these sorts of lightweight semi-class types. If you think about it, a tuple is really just a simplified struct; the Java folks would prefer you to just go ahead and create the struct.
This perspective is changing a bit, especially in Java 8 with its lambdas (which put pressure on the JDK to provide generic Function-type interfaces rather than use-case-specific interfaces like FooCallback). But it's still a fairly strong mindset for a lot of Java developers, and there's some sense to it. Java is a very statically typed language; a tuple is somewhere between static typing and dynamic typing, in that there's nothing in the type system to prevent you from thinking this (int, String) that represents a customer ID and name is actually an (int, String) representing an order ID and its description.
See, for instance, this discussion ("Tuples for n >= 2") on the issue within the Guava project. Granted, that's not official; but it's a good representation of the Java mindset.
There is a nice library called javatuples. It defines generic tuple types for arities from 1(Unit) to 10(Decade) and all essential methods like equals, hashCode, toString and even compareTo.
Official site: http://www.javatuples.org/
Maven dependency:
<dependency>
<groupId>org.javatuples</groupId>
<artifactId>javatuples</artifactId>
<version>[version]</version>
<scope>compile</scope>
</dependency>
(at the moment latest version is 1.2)
It has:
Collections.unmodifiableList(yourList)
will do the trick.
I think that the versatility of the JCF makes the existence of explicit tuple datastructures unnecessary.
I assume that by tuple you mean an ordered immutable list of elements (according to the wikipedia definition).
As for the 3rd party library my power search on google yielded this, this and of course this.
There is a type in the Java runtime and it is called Entry. This is an interface inside Map and there are simple implementation of it inside AbstractMap.
http://docs.oracle.com/javase/7/docs/api/java/util/Map.Entry.html
http://docs.oracle.com/javase/7/docs/api/java/util/AbstractMap.SimpleEntry.html
You can use it as a tuple for two members, you can even apply generics. I would think hard what I did wrong if ever I needed more elements than two. Probably worth it own class then.
A colleague and I were discussing this a few weeks ago. The only solution I could come up with was to internally store the values in a Map<Class, Object> and have some accessor method getValue(Class). Otherwise, how do you access the values in your tuple? You can't have a generic Tuple class with methods for each member, e.g. getInteger, getString, etc because those methods would not be known until runtime, when you create the tuple. This also means you could never have two members with the same type -- how would you be able to write such a class so that at runtime, it knew which object to retrieve?
FunctionalJava has provided a set of P (Product) classes that I believe meet this idea. It provides P1 - P8 to allow for up to 8 element Tuples. I agree with Guava's explantation of why they are discouraged but there you go.
FunctionalJava-P
Is there a generic properties in Java?
Properties taks generic Hashmap inside, but itself seems is nongenric, is there generic properties?
Not in the standard API. You would have to roll your own (which by the way is quite easy).
Still though, I'm not sure I see any use of it. If the class itself should be non-generic, i.e. have a non-generic interface, what does it matter if it's internal structure is generic or not?
its EntrySet method is coming from HashMap which returns Map, I want something returned to be Map
Yes, that is unwieldy. Since JDK1.6 there is at least a getStringPropertyNames, which returns a Set<String> (as opposed to the old Enumeration<?>).