Scala mixes Java's collection class to call Scala's foreach traversal problem

Scala mixes Java's collection class to call Scala's foreach traversal problem

Problem Description

[ERROR] /Users/jack/book/lightsword/src/main/scala/com/springboot/in/action/service/LightSwordUserDetailService.scala:31: error: value foreach is not a member of java.util.List[com.springboot.in.action.entity.UserRole]
[INFO]     for (userRole <- userRoles) {
[INFO]                      ^
[ERROR] one error found

Cause Analysis

No implicit conversion between Scala and Java collection libraries has been added. as follows:

import scala.collection.JavaConversions._

Because they all run on the JVM, Java and Scala can basically achieve seamless integration. The main difference lies in their respective APIs. Since Scala provides more convenient functions for collections, the interoperability between collections between Java and Scala is perhaps the most frequently used in this multi-language platform.

Manipulating Java collections
in Scala There are two situations where you need to manipulate Java collections in Scala.

  • One is that Scala calls other Java libraries, and Java collections need to be converted to Scala collections, so that they can enjoy the benefits provided by Scala collections;
  • The other is to write a Scala program, but it needs to be provided to the Java library. In order to better integrate seamlessly, the Java library must not experience the existence of Scala.

Scala calls the Java library
In order to enjoy the collection features provided by Scala, if you want to call the Java library in a Scala program, you usually need to convert it.
Our UserRoleDao code is as follows:

package com.springboot.in.action.dao
import java.util.List
import com.springboot.in.action.entity.UserRole
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.CrudRepository
import scala.language.implicitConversions
 
  trait UserRoleDao extends CrudRepository[UserRole, Integer] {
    
    
  def findAll(): List[UserRole] // JavaConversions
  
  def save(u: UserRole): UserRole
  
  def findOne(id: Integer): UserRole
  
  @Query(value = "SELECT * FROM user_role where user_id = ?1", nativeQuery = true)
  def listByUserId(userId: Integer): List[UserRole]
 
}

Among them, def listByUserId(userId: Integer): List[UserRole], in order to use Jpa's CrudRepository, we return a java.util.List type.
Then we should call in the service code like this:

val userRoles = userRoleDao.listByUserId(user.id)
// Scala中调用java的collection类,使用scala的foreach,编译器会提示无法找到result的foreach方法。因为这里的userRoles的类型为java.util.List。若要将其转换为Scala的集合,就需要增加如下语句:
import scala.collection.JavaConversions._
for (userRole <- userRoles) {
    
    
val roleId = userRole.roleId
val roleName = roleDao.findOne(roleId).role
      authorities.add(new SimpleGrantedAuthority(roleName))
System.err.println("username is " + username + ", " + roleName)
     }

If this sentence is not added:

import scala.collection.JavaConversions._

The error at the beginning will be reported.

solution

Add this sentence:

import scala.collection.JavaConversions._

Guess you like

Origin blog.csdn.net/qq_32727095/article/details/113740841