入坑kotlin

*** kotlin连jpa

始终报反序列化错误,找了最后发现是日期类型搞的鬼,OffsetDateTime  引入

compile 'org.hibernate:hibernate-java8:5.0.12.Final'

大家在使用JPA 的时候,如果你的 Entity 中有 OffsetDateTime 类型的字段,那么你需要加入hibernate-java8 这个依赖

如果你用 Springboot 是1.5.9.RELEASE 那么使用 compile 'org.hibernate:hibernate-java8:5.2.12.Final'
如果你用 Springboot 是1.5.2.RELEASE 那么使用 compile 'org.hibernate:hibernate-java8:5.0.12.Final'




*** kotlin连ObjectMapper解析 json 字符串 为对对象

示例1:
var javaType : JavaType = objectMapper.getTypeFactory().constructParametricType(HashMap::class.java, String::class.java, String::class.java)
var mapPara : HashMap<String,String> = objectMapper.readValue(remark,javaType)
   mapPara["x"]

示例2:
    var javaType : JavaType =objectMapper.getTypeFactory().constructParametricType(List::class.java, String::class.java)
val brandList: List<String> = objectMapper.readValue(brands, javaType)

示例3:
    var xDto : XtDTO = objectMapper.readValue(str, object : TypeReference<XtDTO>() {})


*** List<String> to String || String to List<String>
class StringListConverter : TypeConverter<String, MutableList<String>>() {
    val separator = ","

    override fun getDBValue(model: MutableList<String>?): String =
            if (model == null || model.isEmpty())
                ""
            else
                model.joinToString(separator = separator) { it }

    override fun getModelValue(data: String?): MutableList<String> {
        return data?.split(separator)?.toMutableList() ?: mutableListOf()
    }
}

OffsetDateTime工具类


https://blog.csdn.net/chinoukin/article/details/78875458
 public static void main(String[] args) {
         DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime time = LocalDateTime.now();
         String localTime = df.format(time);
         LocalDateTime ldt = LocalDateTime.parse("2018-01-12 17:07:05",df);
         System.out.println("LocalDateTime转成String类型的时间:"+localTime);
         System.out.println("String类型的时间转成LocalDateTime:"+ldt);
     }

LocalDateTime转成String类型的时间:2018-01-12 17:36:52
String类型的时间转成LocalDateTime:2018-01-12T17:07:05

https://www.cnblogs.com/skywang12345/p/string01.html

Kotlin List:

https://www.jianshu.com/p/712e87f83c31?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

List是有顺序的数据结构,在Kotlin中提供了类似数组的访问方式:

// List<out E>
operator fun get(index: Int): E

// MutableList<out E>
operator fun set(index: Int, element: E): E

get()和set()函数重载了操作符后,就可以操作List了:

val list = mutableListOf("Hello", "World")
println(list[0])
// 等价于 println(list.get(0))
list[1] = ""
// 等价于 list.set(1, "")

在Kotlin中,kotlin.collections.Collections.kt文件里定义了几个常用的集合工具函数,主要涉及List部分,如下介绍:

一、listOf()函数

Collection.kt定义了三个参数不同的listOf()函数,可以方便地创建一个不可变的List:

fun <T> listOf(): List<T>
fun <T> listOf(T): List<T>
fun <T> listOf(vararg T): List<T>
  • 第一个无参的listOf()函数返回空的List,它直接调用同一文件内定义的emptyList()函数,该函数返回一个单例对象EmptyList。EmptyList是List<Nothing>接口的实现类,可以代表所有类型的空列表。
  • 第二个listOf()函数产生一个只有一个元素的不可变列表,它直接调用java.util.Collections类的singletonList()方法。
    -第三个lisOf(),接受可变数量的T类型元素并生成包含这些元素的列表。传入一个T类型的数组,如果长度为0就返回空列表,否则 就将数组转换为列表。

二、mutableListOf()函数

listOf()函数可以创建不变的List,要想创建可变的List,可以使用mutableList()函数:

inline fun mutableListOf(): MutableList<T> = ArrayList()

fun mutableListOf(vararg elements: T): MutableList<T> {
  if(elements.size == 0) {
    return ArrayList()
  } else {
    return ArrayList(ArrayAsCollection(elements, isVarargs = true))
  }
}
  • inline关键字定义了内联函数,会在编译时展开到调用处,提高性能。
  • Kotlin的集合框架只定义了接口,具体实现类都是直接调用Java类。mutableListOf()返回一个ArrayList对象。

三、arrayListOf()函数

arrayListOf()函数用来创建可变的ArrayList,有两个参数不同的重载形式:

inline fun <T> arrayListOf(): ArrayList<T> = ArrayList()

fun <T> arrayListOf(vararg elements: T): ArrayList<T> {
  if(elements.size == 0) {
    return ArrayList()
  } else {
    return ArrayList(ArrayAsCollection(elements, isVarargs = true))
  }
}
  • 这个函数的实现与mutableListOf()完全一样,唯一的不同在于返回的类型不同。arrayListOf()返回ArrayList<T>,是一个Java的ArrayList类型,所以是可变的。

四、listOfNotNull()函数

以上的函数都是可以接受null作为元素的,这与Java中的List是不同的。而这个方法会把null全部剔除掉,返回包含所有非null值的List:

fun listOfNotNull(element: T?): List<T> 
    = if(element != null) listOf(element) else emptyList()

fun listOfNotNull(vararg elements: T?): List<T>
    = elements.filterNotNull()
  • 这两个函数都接受可空的T?类型参数,返回剔除所有null值的List
  • 第二个函数调用的Array<out T?>.filterNotNull()函数,会先新建一个ArrayList<T>,然后遍历数组内的所有元素,如果不是null就添加到ArrayList里,最后返回ArrayList。

五、binarySearch()函数

Collections.kt文件中提供了四个针对List的binarySearch()函数,它们可以对列表进行二分查找:

fun <T> List<T>.binarySearch(element: T, comparator: Comparator<in T>, fromIndex: Int = 0, toIndex: Int = size): Int
fun <T> List<T>.binarySearch(fromIndex: Int = 0, toIndex: Int = size, comparison: (T) -> Int): Int
fun <T: Comparable<T>> List<T?>.binarySearch(element: T?, fromIndex: Int = 0, toIndex: Int = size): Int

inline fun <T, K : Comparable<K>> List<T>.binarySearchBy(key: K?, fromIndex: Int = 0, toIndex: Int = size, crossinline selector: (T) -> K?): Int 
    = binarySearch(fromIndex, toIndex) { compareValues(selector(it), key) }
  1. 函数需要提供一个Comparator比较器,与java.util.Arrays.binarySearch()相同;
  2. 函数需要提供一个(T)->Int类型的函数,用来比较列表内的元素,可以视为传入一个compareTo()函数。
  3. 函数提供了可空元素的二分查找。
  4. 函数需要提供一个(T)-K?类型的函数和一个K?类型的值,它会在列表中用二分查找法寻找符合这个值的元素的索引。


 

 
原文地址:https://www.cnblogs.com/pokay/p/8423856.html