Kotlin stream map to map

Kotlin How to use java streams .map() in kotlin to map a different object response

Edit 1: It works perfectly if I use name of the this.javaClass.kotlin directly like this It works perfectly fine Solution: returns a collection of class member properties. (where ) returns a collection of member properties of class of , which is . It either uses serialized name of the member or prop name

How to convert a map to a map kotlin

My objects are as follows:

val mapOfItems = mapOf("x" to listOf(1, 2), "y" to listOf(1, 3), "z" to listOf(2, 3)) 

I want to get an object like this:

val mapOfSummedItems = mapOf("x" to 3, "y" to 4, "z" to 5) 
mapOfItems.map < (key, value) ->SummedItems(key, value.sum()) > .groupBy < it.key >data class SummedItems(val key: String, val sum: Int) 

That does not give me what I want that gives me an Map>

.map() on a Map generates a List . While you can make this a List and then use associate* operators to get back into a Map , you can also just map the values directly:

Issue while using kotlin reflaction to map object member properties to hashmap

open class Test < fun getAsHashMap() : HashMap< val hashMap = HashMap() val className = this.javaClass.kotlin for (prop in className::class.memberProperties) < val field = className::class.java.getDeclaredField(prop.name) val fieldSerializedName : SerializedName? = field.getAnnotation(SerializedName::class.java) fieldSerializedName?.let < hashMap[fieldSerializedName.value] = prop.get(this)!! >?: run < hashMap[prop.name] = prop.get(this)!! >> return hashMap > > 

Error

I have wrote above function to map the memberProperties of object instance of its child class to hashmap . It either uses serialized name of the member or prop name [Based on availability of serialized name for that property] But unfortunately I get the following error. This is my first time using reflection java/kotlin, please let me know if it can be fixed.

Читайте также:  Javascript onclick input text value

It works perfectly if I use name of the this.javaClass.kotlin directly like this

data class ProductInformation ( @field:SerializedName("productid") val productId: Int, @field:SerializedName("productname") val productName: String, @field:SerializedName("brandname") val brandName: String, @field:SerializedName("originalprice") val originalPrice: Int, @field:SerializedName("sellingprice") val sellingPrice: Int, @field:SerializedName("productgender") val productGender: String, @field:SerializedName("productvariant") val productVariant: String, @field:SerializedName("discounted") val discounted: String, @field:SerializedName("productcategory") val productCategory: String ) : StructuredEventAttribute < override fun getAsHashMap(): HashMap< val hashMap = HashMap() for (prop in ProductInformation::class.memberProperties) < val field = ProductInformation::class.java.getDeclaredField(prop.name) val fieldSerializedName : SerializedName? = field.getAnnotation(SerializedName::class.java) fieldSerializedName?.let < hashMap[fieldSerializedName.value] = prop.get(this)!! >?: run < hashMap[prop.name] = prop.get(this)!! >> return hashMap > > interface StructuredEventAttribute < fun getAsHashMap() : HashMap> 

ProductInformation::class.memberProperties returns a collection of ProductInformation class member properties.

className::class.memberProperties (where className = this.javaClass.kotlin ) returns a collection of member properties of class of className , which is KClass . In short you are getting members of KClass instead of Test .

Solution: change className::class.memberProperties to className.memberProperties .

Convert List of Maps to single Map via streams, We can use the reduce() of java streams to convert a list of maps to a single map in java.

Java Stream with ::new to Kotlin

I’m trying to convert the following Spring Security code from Java to Kotlin.

Collection authorities = Arrays.stream(claims.get(AUTHORITIES_KEY).toString().split(",")) .map(SimpleGrantedAuthority::new) .collect(Collectors.toList()); 
val authorities = Arrays.stream(claims[AUTHORITIES_KEY].toString().split(",".toRegex()).dropLastWhile < it.isEmpty() >.toTypedArray()) .map(SimpleGrantedAuthority()) .collect(Collectors.toList()) 

I get a Type mismatch error ( Required: Function! ) on .map(SimpleGrantedAuthority()) . How do I convert the above Java code to Kotlin with a ::new keyword correctly?

Using Arrays.stream doesn’t make the code super readable:

val authorities = Arrays.stream(claims.get(AUTHORITIES_KEY).toString().split(",").toTypedArray()) .map(::SimpleGrantedAuthority).collect(Collectors.toList()) 

However in Kotlin you can do better:

val authorities = claims.get(AUTHORITIES_KEY).toString() .split(",").filterNot < it.isEmpty() >.map(::SimpleGrantedAuthority) 

You need to pass a lambda expression to map ‘s body. What you did there was creating a new instance of SimpleGrantedAuthority instead of creating a function that returns a new instance of SimpleGrantedAuthority .

Читайте также:  Сумма всех двузначных чисел питон

In order to fix your problems, you can use a reference to the constructor:

.map(::SimpleGrantedAuthority) 

or define a lambda expression by hand:

You do not really need to use Stream API in Kotlin. Native Collections API is powerful enough:

 val auths = claims[AUTHORITIES_KEY].toString() .split(",") .filterNot(String::isEmpty) .map(::SimpleGrantedAuthority) 

How to map multiple objects having two identical values and one, I know this is a Java question but I like to provide a Kotlin answer. It might help you come up with a Java solution or you might even want

Источник

Оцените статью