当前位置:网站首页>Kotlin basic learning 17
Kotlin basic learning 17
2022-07-02 03:24:00 【Never stop learning】
Catalog
1.Kotlin Filter function of language -filter
2.Kotlin Merge function of language -zip
3.Kotlin Functional programming of language
4.Kotlin Interoperability and nullability of languages
6. annotation @Jvmname And Kotlin
7. annotation @JvmField And Kotlin
8. annotation @JvmOverloads And Kotlin
9. annotation @JvmStatic And Kotlin
10. Handwriting RxJava, All use KT Based on
1.Kotlin Filter function of language -filter
// TODO 126.Kotlin Filter function of language -filter
fun main(){
val nameLists = listOf(
listOf(" Huang Xiaoming "," Jet Li "," Bruce Lee "),
listOf(" Liu Jun "," Li Yuan "," Liu Ming "),
listOf(" Liu Jun "," Huangjiaju "," huang feihong ")
)
nameLists.map {
println(it)
}
println()
nameLists.flatMap {
println(it)
listOf("")
}
println()
nameLists.flatMap {
// Come in 3 Time
it -> it.filter {
println("$it filter") // Come in 9 Time
true
// principle :filter {true,false} true He will join the new collection Assemble a new set return , Otherwise return to false, To filter out , Do not join , Return empty set
}
}.map {
print("$it ")
}
println()
nameLists.flatMap {
it -> it.filter {
true
}
}.map{
print("$it ")
}
println()
//List<T> Return to map After the effect of :[ Huang Xiaoming , Jet Li , Bruce Lee ] [ Liu Jun , Li Yuan , Liu Ming ] [ Liu Jun , Huangjiaju , huang feihong ]
//List<T> Return to flatMap After the effect of : Huang Xiaoming Jet Li Bruce Lee Liu Jun Li Yuan Liu Ming Liu Jun Huangjiaju huang feihong
nameLists.flatMap {
it -> it.filter {
it.contains(' yellow ')
}
}.map {
print("$it ")
}
}2.Kotlin Merge function of language -zip
// TODO 126.Kotlin Merge function of language -zip
fun main(){
val names = listOf(" Zhang San "," Li Si "," Wang Wu ")
val ages = listOf(20,21,22)
//RxJava zip Merge operators
//KT Bring it with you zip Merge operation
// principle : Is to merge the first set and the second set , Create a new collection , And back to
// Create a new collection ( Elements , Elements , Elements ) Elements Pair(K,V) K The element representing the first set V Represents the second element of the set
val zip:List<Pair<String,Int>> = names.zip(ages)
println(zip)
println(zip.toMap())
println(zip.toMutableSet())
println(zip.toMutableList())
println()
// Traverse
zip.forEach{
println(" Name is :${it.first}, Age is :${it.second}")
}
println()
//map The ordinary way
zip.toMap().forEach { k,v ->
println(" Name is :${k}, Age is :${v}")
}
println()
//map The way of structure
zip.toMap().forEach { (k,v) ->
println(" Name is :${k}, Age is :${v}")
}
println()
zip.toMap().forEach {
println(" Name is :${it.key}, Age is :${it.value}")
}
}3.Kotlin Functional programming of language
// TODO 128.Kotlin Functional programming of language
fun main(){
val names = listOf(" Zhang San "," Li Si "," Wang Wu ")
val ages = listOf(20,21,22)
names.zip(ages).toMap().map { " Your name is ${it.key}, Your age is ${it.value}" }.map { println(it) }
}4.Kotlin Interoperability and nullability of languages
// TODO 129.Kotlin Interoperability and nullability of languages
fun main(){
// Here is java And KT Interaction , Wrong case
/**println(KtBase129().info1.length)
println(KtBase129().info2.length)*/
// Here is java And KT Interaction , Wrong case
//: String! Java And KT When interacting ,Java to KT It's worth , All are :String! This type of
/**val info1 = KtBase129().info1
val info2 = KtBase129().info2
println(info1.length)
println(info2.length)*/
// Here is Java And KT Interact with correct cases
// As long as you see String! The type of , In use , must ?.xxx This is the rule 1, But not good , Forgetting to write is risky
val info1s = KtBase129().info1
val info2s = KtBase129().info2
println(info1s?.length)
println(info2s?.length)
// Here is Java And KT Interact with correct cases 2
val info1ss:String? = KtBase129().info1
val info2ss:String?= KtBase129().info2
println(info1ss?.length)
println(info2ss?.length)
}
5. The singleton pattern
//1. Hungry han type of implementation Java edition
public class SingletonDemo {
private static SingletonDemo instance = new SingletonDemo();
private SingletonDemo(){}
public static SingletonDemo getInstance(){
return instance;
}
}
//2. Lazy implementation Java edition
public class SingletonDemo2 {
private static SingletonDemo2 instance ;
private SingletonDemo2(){}
public static SingletonDemo2 getInstance(){
if(instance == null){
instance = new SingletonDemo2();
}
return instance;
}
}
//3. Lazy implementation Java edition Security
public class SingletonDemo3 {
private static SingletonDemo3 instance ;
private SingletonDemo3(){}
public static synchronized SingletonDemo3 getInstance(){
if(instance == null){
instance = new SingletonDemo3();
}
return instance;
}
}
//4. Lazy implementation Java edition Double check security
public class SingletonDemo4 {
private volatile static SingletonDemo4 instance;
private SingletonDemo4() {
}
public static synchronized SingletonDemo4 getInstance() {
if (instance == null) {
synchronized (SingletonDemo4.class){
instance = new SingletonDemo4();
}
}
return instance;
}
}//1. Hungry han type of implementation KT edition
object SingletonDemoKt
//2. Lazy implementation
class SingletonDemo2Kt {
companion object{
private var instance : SingletonDemo2Kt ? = null
get(){
if(field == null){
field = SingletonDemo2Kt()
}
return field
}
fun getInstanceAction() = instance!!
}
fun show(){
println("show")
}
}
fun main(){
SingletonDemo2Kt.getInstanceAction().show()
}
//3. Lazy implementation KT edition Security
class SingletonDemo3Kt {
companion object{
private var instance : SingletonDemo3Kt ? = null
get(){
if(field == null){
field = SingletonDemo3Kt()
}
return field
}
@Synchronized
fun getInstanceAction() = instance!!
}
fun show(){
println("show")
}
}
fun main(){
SingletonDemo3Kt.getInstanceAction().show()
}
//4. Lazy implementation KT edition Double check security
class SingletonDemo4Kt {
companion object{
val instance : SingletonDemo4Kt by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED){SingletonDemo4Kt()}
}
fun show(){
println("show")
}
}
fun main(){
SingletonDemo4Kt.instance.show()
}6. annotation @Jvmname And Kotlin
@file:JvmName("Stu") // In the compiler section , Modify our class name , Let's have Java End calls are simpler
package com.bxb.s7
// TODO 131. annotation @Jvmname And Kotlin
fun getStudentNameValueInfo(str:String) = println(str)
fun main(){
}public class KtBase131 {
public static void main(String[] args) {
Stu.getStudentNameValueInfo("Bxb is ok");
}
}7. annotation @JvmField And Kotlin
// TODO 132. annotation @JvmField And Kotlin
class Person{
@JvmField
val names = listOf(" Zhang San "," Li Si "," Wang Wu ")
}
public class KtBase132 {
public static void main(String[] args) {
Person person = new Person();
for(String name : person.names){
System.out.println(name);
}
}
}8. annotation @JvmOverloads And Kotlin
// TODO 133. annotation @JvmOverloads And Kotlin
@JvmOverloads
fun toast(name:String,sex:Char = 'M'){
println("name:$name,sex:$sex")
}
fun main(){
toast(" Li Si ")
}
public class KtBase133 {
public static void main(String[] args) {
KtBase133Kt.toast("zhangsan");//Java enjoy KT Default parameters
}
}9. annotation @JvmStatic And Kotlin
// TODO 134. annotation @JvmStatic And Kotlin
class MyObject{
companion object{
@JvmField
val TARGET = " Yellowstone National Park "
@JvmStatic
fun showAction(name:String) = println("$name Want to go $TARGET play ")
}
}
fun main(){
MyObject.TARGET
MyObject.showAction("Bxb")
}public class KtBase134 {
public static void main(String[] args) {
System.out.println(MyObject.TARGET);
MyObject.showAction("Bxb");
}
}10. Handwriting RxJava, All use KT Based on
// Handwriting RxJava, All use KT Based on
fun main() {
//creat Input source , There are no parameters for you output source : Just output ( All types , Universal type )
create {
"Bxb"
123
true
"AAAAAAAA"
4563.54f
}.map {
" Your value is $this"
}.map {
"[$this]"
}.map {
"@@[email protected]@"
}.observer {
// Just put the input above , Just print it out , So there is no need for tube output
println(this)
}
}
// Transfer station , Keep our records //valueItem == create The return value of the last line of the operator It flows here
class RxJavaCoreClassObject<T>(var valueItem: T) // Main structure , Accept your message , This message is create The last line returned
inline fun<I> RxJavaCoreClassObject<I>.observer(observerAction:I.()->Unit) = observerAction(valueItem)
inline fun <I, O> RxJavaCoreClassObject<I>.map(mapAction: I.() -> O) = RxJavaCoreClassObject(mapAction(valueItem))
inline fun <OUTPUT> create(action: () -> OUTPUT) = RxJavaCoreClassObject((action()))边栏推荐
- ORA-01547、ORA-01194、ORA-01110
- Principle of computer composition - interview questions for postgraduate entrance examination (review outline, key points and reference)
- Eight steps of agile development process
- Find duplicates [Abstract binary / fast and slow pointer / binary enumeration]
- Grpc快速实践
- [JVM] detailed description of the process of creating objects
- 焱融看 | 混合云时代下,如何制定多云策略
- C # joint Halcon's experience of breaking away from Halcon environment and various error reporting solutions
- 终日乾乾,夕惕若厉
- Gradle foundation | customize the plug-in and upload it to jitpack
猜你喜欢

Force deduction daily question 540 A single element in an ordered array

Verilog 过程赋值 区别 详解

How to establish its own NFT market platform in 2022

表单自定义校验规则

Verilog state machine

小米青年工程师,本来只是去打个酱油
![寻找重复数[抽象二分/快慢指针/二进制枚举]](/img/9b/3c001c3b86ca3f8622daa7f7687cdb.png)
寻找重复数[抽象二分/快慢指针/二进制枚举]

Verilog 状态机
![[golang] leetcode intermediate bracket generation & Full Permutation](/img/93/ca38d97c721ccba2505052ef917788.jpg)
[golang] leetcode intermediate bracket generation & Full Permutation

Large screen visualization from bronze to the advanced king, you only need a "component reuse"!
随机推荐
【JVM】创建对象的流程详解
Xiaomi, a young engineer, was just going to make soy sauce
Principle of computer composition - interview questions for postgraduate entrance examination (review outline, key points and reference)
Kotlin基础学习 15
Competition and adventure burr
What is hybrid web containers for SAP ui5
On redis (II) -- cluster version
Verilog wire type
Comment élaborer une stratégie nuageuse à l'ère des nuages mixtes
aaaaaaaaaaaaa
Use blocking or non blocking for streamline
Verilog 线型wire 种类
Discrimination between sap Hana, s/4hana and SAP BTP
js生成随机数
KL divergence is a valuable article
[golang] leetcode intermediate bracket generation & Full Permutation
In depth interpretation of pytest official documents (26) customized pytest assertion error information
Load different fonts in QML
Verilog 状态机
GB/T-2423.xx 环境试验文件,整理包括了最新的文件里面