kotlin

Kotlin - how to find number of repeated values in a list?

↘锁芯ラ 提交于 2020-08-21 05:04:02
问题 I have a list, e.g like: val list = listOf("orange", "apple", "apple", "banana", "water", "bread", "banana") How can i check how many times apple is duplicated in this list? 回答1: list.count { it == "apple" } Documentation: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/, https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/count.html 回答2: One way to find all the repeated values in a list is using groupingBy and then filter the values which are > 1 . E.g. val

Kotlin - how to find number of repeated values in a list?

╄→гoц情女王★ 提交于 2020-08-21 05:02:21
问题 I have a list, e.g like: val list = listOf("orange", "apple", "apple", "banana", "water", "bread", "banana") How can i check how many times apple is duplicated in this list? 回答1: list.count { it == "apple" } Documentation: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/, https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/count.html 回答2: One way to find all the repeated values in a list is using groupingBy and then filter the values which are > 1 . E.g. val

What is out keyword in kotlin

心已入冬 提交于 2020-08-20 18:07:17
问题 I am not able to understand and I couldn't find the meaning of out keyword in kotlin. You can check example here: List<out T> If any one can explain the meaning of this. It would be really appreciated. 回答1: With this signature: List<out T> you can do this: val doubleList: List<Double> = listOf(1.0, 2.0) val numberList: List<Number> = doubleList which means T is covariant : when a type parameter T of a class C is declared out , C<Base> can safely be a supertype of C<Derived> . This is contrast

kotlin.TypeCastException: null cannot be cast to non-null type kotlin.collections.MutableList

落花浮王杯 提交于 2020-08-20 13:23:57
问题 please dont marked as duplicate , as the question is slightly different ---> null cannot be cast to non-null type kotlin.collections. MutableList Scenerios :- i have been performing delete cart using retrofit.. if atleast one item is present ,it displays in recyclerview 2.if cart is empty ,it crashes with a above error here is my adapter code:- class CartAdapter(context: Context, dataList: MutableList<DataCart?>) : RecyclerSwipeAdapter<CartAdapter.CustomViewHolder>() { //added

Why Kotlin submission is significantly slow compared to Java?

二次信任 提交于 2020-08-20 07:37:31
问题 I am very new to Kotlin. So to practice, I tried using it to solve problems in LeetCode. Here is one problem that I solved today: Distinct Subsequence(Leetcode) First I tried solving the problem in Java: class Solution { public int numDistinct(String s, String t) { int[][] dp = new int[t.length() + 1][s.length() + 1]; for (int i = 0; i <= s.length(); i++) { dp[0][i] = 1; } for (int i = 1; i <= t.length(); i++) { for (int j = 1; j <= s.length(); j++) { if (t.charAt(i - 1) != s.charAt(j - 1)) {

【7】kotlin 类成员变量 set get方法

烂漫一生 提交于 2020-08-19 22:04:07
什么是类成员 属性:或者说成员变量类范围内的变量 方法:或者说成员函数,类范围内的函数 函数和方法的区别 函数强调功能本身,不考虑从属 方法的称呼通常是从类的角度出发 叫法不同而已,不用纠结 定义方法 写法与普通函数完全一致,就是写在类里面 class Hello{ fun sayHello(name: String) = print("Hello,$name") } 定义属性 构造方法中 val/var修饰的都是属性 类内部也可以定义属性 //aField 是属性,, notAField就不是了 是构造参数 class Hello2(val aField: Int, notAField: Int) { //这个也是属性 var anotherField: Float = 3f } 属性访问控制 属性可以定义getter/setter 属性初始化 属性的初始化尽量再构造方法中完成 无法再构造方法中完成,尝试降级为局部变量 无法降级 var 可以用lateinit 延迟 val 用lazy 可空类型谨慎用null 直接初始化 最好是不用哦 相关代码 java 中的get set package com.yzdzy.kotlin.member; public class JavaMember { private int b = 0; public int getB() { System

运维救火必备:问题排查与系统优化手册(结合惨案现身说法)

与世无争的帅哥 提交于 2020-08-19 17:32:11
软件工程领域存在一个共识:维护代码所花费的时间要远多于写代码。而整个代码维护过程中,最惊心动魄与扣人心弦的部分,莫过于问题排查(Trouble-shooting)了。特别是那些需要 7x24 小时不间断维护在线业务的一线服务端程序员们,大大小小的问题排查线上救火早已成为家常便饭,一不小心可能就吃成了自助餐 —— 竖着进躺着出,吃不了也兜不住。 本文分享作者在服务端问题排查方面的一些经验,包括常见问题、排查流程、排查工具,结合实际项目中发生过的惨痛案例进行现身说法。 一 、问题排查 1、常见问题 Know Your Enemy:知己知彼,百战不殆。 日常遇到的大部分问题,大致可以归到如下几类: 逻辑缺陷:e.g. NPE、死循环、边界情况未覆盖。 性能瓶颈:e.g. 接口 RT 陡增、吞吐率上不去。 内存异常:e.g. GC 卡顿、频繁 FGC、内存泄露、OOM。 并发/分布式:e.g. 存在竞争条件、时钟不同步。 数据问题:e.g. 出现脏数据、序列化失败。 安全问题:e.g. DDoS 攻击、数据泄露。 环境故障:e.g. 宿主机宕机、网络不通、丢包。 操作失误:e.g. 配置推错、删库跑路(危险动作,请勿尝试..)。 上述分类可能不太完备和严谨,想传达的点是:你也可以积累一个这样的 checklist,当遇到问题百思不得其解时,耐心过一遍,也许很快就能对号入座。 2、排查流程

高级安卓工程师(广告联盟商业化方向)招聘--30K~40K--14薪--技术负责人直招

拟墨画扇 提交于 2020-08-19 12:59:28
工作地址:西二旗--环洋大厦 深度参与过广告联盟/SDK的接入、模块化与商业化。 技术负责人直招(技术负责人的微信技术公众号:千里行走)。 岗位描述:
 1.负责DAU数百万APP的广告模块,包含但不限于架构,业务,重构;勇于承担。 2.有owner意识,和团队积极沟通,积极推动广告业务。 3.有如下愿景:可以给DAU百万/千万级的APP产品提供商业级别的广告业务技术解决方案并落地。 
岗位要求:
 1.5年以上Android开发经验,最好有日活百万以上的APP广告联盟业务经验。
 2.了解Android框架对Android系统应用管理、进程管理、内存管理等机制有深入理解;精通java和Kotlin开发Android应用。 3.在内存优化、APK大小优化、混淆加密、网络优化、UI绘制优化等性能问题上具有丰富的经验。 
4.有良好的问题理解能力,能够理解以及处理复杂逻辑; 
5.能适应一定的工作压力,团队协作能力强; 加分项: 1.输出高质量的价值文档(具体参见微信技术公众号:千里行走)。 2.注重代码质量,设计经验丰富,能够产出高质量的设计和代码 。 3.从0到1完整的经历过广告联盟SDK的上线优先。 4.对广告联盟SDK有商业级别的理解和落地优先。 本文分享自微信公众号 - 千里行走(a_thousands_of_miles)。 如有侵权,请联系 support@oschina

How does kotlin use this by delegate to instantiate the viewmodel

时光总嘲笑我的痴心妄想 提交于 2020-08-19 07:03:53
问题 I was reading through the google android architecture example and came across this.Can someone explain to me how this delegate works? private val viewModel by viewModels<TasksViewModel> { getViewModelFactory() } where getViewModelFactory is an extension method that returns a ViewModelFactory and TasksViewModel is an instance of ViewModel() The way that I read this is similar to: private val viewModel: TasksViewModel by Fragment.ViewModel(ViewModelFactory) can someone elaborate on if my

What is difference between “as” and “is” operator in Kotlin?

与世无争的帅哥 提交于 2020-08-19 03:15:58
问题 In Java, I can write code like: void cast(A a) { if(a instanceof Person) { Person p = (Person) a; } } In Kotlin, what should I do? Use as operator or is operator? 回答1: is X is the equivalent of instanceof X foo as X is the equivalent of ((X) foo) Additionally, Kotlin performs smart casting where possible, so no additional cast needed after you check the type using is : open class Person : A() { val foo: Int = 42 } open class A and then: if (p is Person) { println(p.foo) // look, no cast