当前位置:网站首页>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()))边栏推荐
- 表单自定义校验规则
- Learn PWN from CTF wiki - ret2shellcode
- GSE104154_ scRNA-seq_ fibrotic MC_ bleomycin/normalized AM3
- Verilog parallel block implementation
- Docker安装canal、mysql进行简单测试与实现redis和mysql缓存一致性
- MySQL advanced (Advanced) SQL statement (II)
- KL divergence is a valuable article
- Just a few simple steps - start playing wechat applet
- Go执行shell命令
- Yan Rong looks at how to formulate a multi cloud strategy in the era of hybrid cloud
猜你喜欢

Force deduction daily question 540 A single element in an ordered array
![[golang] leetcode intermediate bracket generation & Full Permutation](/img/93/ca38d97c721ccba2505052ef917788.jpg)
[golang] leetcode intermediate bracket generation & Full Permutation

Detailed explanation of the difference between Verilog process assignment

Download and use of the super perfect screenshot tool snipaste

Delphi xe10.4 installing alphacontrols15.12

Detailed explanation of ThreadLocal

汇率的查询接口

Verilog timing control

GB/T-2423. XX environmental test documents, including the latest documents

Docker安装canal、mysql进行简单测试与实现redis和mysql缓存一致性
随机推荐
创业了...
Find duplicates [Abstract binary / fast and slow pointer / binary enumeration]
Failed to upgrade schema, error: “file does not exist
The capacity is upgraded again, and the new 256gb large capacity specification of Lexar rexa 2000x memory card is added
Mathematical calculation in real mode addressing
3124. Word list
Qualcomm platform WiFi -- Native crash caused by WiFi
Kotlin基础学习 16
JDBC details
One of the future trends of SAP ui5: embrace typescript
Possible causes of runtime error
SAML2.0 笔记(一)
Work hard all day long and be alert at sunset
小米青年工程师,本来只是去打个酱油
Eight steps of agile development process
Global and Chinese markets for electronic laryngoscope systems 2022-2028: Research Report on technology, participants, trends, market size and share
ORA-01547、ORA-01194、ORA-01110
Merge interval, linked list, array
Learn PWN from CTF wiki - ret2shellcode
Global and Chinese markets for hand hygiene monitoring systems 2022-2028: Research Report on technology, participants, trends, market size and share