Tag: Java

替换Kotlin本地解决方案中的流收集

我正在使用Kotlin函数从键值对的Json结构中提取一个映射。 用于构建映射的JSON包含一个标签和一个值: "values": [ { "label": "Email", "value": "email" }, { "label": "Social media", "value": "socialMedia" }, { "label": "Word of mouth", "value": "wordOfMouth" }, { "label": "Newspaper", "value": "newspaper" } ], JSON“标签”应该成为地图的关键和“价值”的价值。 这是用于使用Java 8的流收集方法提取和转换JSON到地图的代码。 fun extractValue(jsonNode: JsonNode?): Map<String, String> { val valuesNode = jsonNode?.get("values") ?: mapper.createArrayNode() return valuesNode.map { Pair(it.get("label")?.asText() ?: "", it.get("value")?.asText() ?: […]

Java 8 stream.collect(Collectors.toMap())类似于kotlin

假设我有一个人员列表,并希望有Map<String, Person> ,其中String是人名。 我应该怎么做在科特林?

等效的Java和Kotlin Stream代码之间意外的类型差异

编辑2016年3月1日:公平的警告:这个问题被问到1.0.0之前的Kotlin。 从Kotlin 1.0.0开始,情况就不一样了。 请参阅下面的@Jayson Minard的Kotlin 1.0.0答案。 在使用Stream的Java 8代码中,我写了类似的东西 public static void main(String… args) { Stream<Integer> integerStream = Stream.of(1,2,3); List<Integer> integerList = integerStream.collect(Collectors.toList()); } 但是在Kotlin编写的类似代码中,我得到了意想不到的结果。 public fun main(args: Array<String>) { val integerStream : Stream<Int> = Stream.of(1, 2, 3) // this is what I expect to write, like the Java 8 code, but is a compilation error: […]

在Kotlin中使用@ EnableNeo4jRepositories(basePackageClasses =“myApp”)

我已经添加了以下注释: @EnableNeo4jRepositories(basePackages = "myApp") 在Java中工作正常,但Kotlin编译器给出: (40, 51): Type mismatch: inferred type is kotlin.String but kotlin.Array<kotlin.reflect.KClass<*>> was expected

在java代码中使用kotlin库

我有一个kotlin库的FlowLayout库为aar , 并希望在我的java代码(android)中使用它,但不能解决kotlin.Pair 。 FlowLayoutManager layoutManager=new FlowLayoutManager(3, RecyclerView.HORIZONTAL, new FlowLayoutManager.Interface() { @Override public kotlin.Pair<Integer, Integer> getProportionalSizeForChild(int i) { return null; } }); 我已经尝试android.support.v4.util.Pair但不兼容。 如何在Java代码中使用kotlin库及其对象依赖关系?

迭代时从列表中移除IndexOutOfBoundsException:Java-> Kotlin

该代码在Java中工作,但转换为Kotlin时,它不再有效。 它抛出一个IndexOutOfBoundsException 。 这是原来的Java: grid_view.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { String selectedItem = ((TextView) view.findViewById(R.id.tag_name)).getText().toString(); for (int j = 0; j < itemList.size(); j++){ String tempString = itemList.get(j); if(tempString.equals(selectedItem)) { Log.d("Update", "Removing: " + selectedItem); itemList.remove(j); } } } }); 这是新的Kotlin: grid_view!!.onItemClickListener = AdapterView.OnItemClickListener { _, […]

在Android Studio Canary中将Kotlin还原为Java代码5

我正在用Java编写一个Android应用程序,就像其他人一样。 自从Android Studio 3.o Canary发布并加入对Kotlin的支持后,我有机会尝试一下。 下载插件并正确设置Gradle文件。 但一旦活动转换成Kotlin并同步,就会发生错误。 下面是我的build.gradle, apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' 和, ext.kotlin_version = '1.1.3' 依赖性, 所以,我想回到Java,直到问题解决。 Kotlin代码是, class Welcome : AppCompatActivity() { internal var rujuk = FirebaseDatabase.getInstance().reference /*3rd step, DB reference*/ /*4th, initially write under onStart method, then CnP here, value inside child() should be same as in […]

在Kotlin中扩展泛型类

比方说,我有这个Java代码示例: public class MyActivityDelegate implements ActivityMvpDelegate 其中ActivityMvpDelegate: interface ActivityMvpDelegate<V extends MvpView, P extends MvpPresenter<V>> 相同的代码转换为Kotlin看起来像这样 class MyActivityDelegate(private val activity: Activity) : ActivityMvpDelegate<MvpView, MvpPresenter<V>> 当然,我得到了V未解决的参考,我不知道这个代码应该看起来,在Java中,我不必指定泛型在这里..任何提示将不胜感激

如何在Kotlin中创建一个空字符?

在Java中,可以使用转义序列\0来表示空字符 \0和\0000在Kotlin中不是有效的转义序列,所以我一直在使用一个静态java char并从Kotlin访问它。 public class StringUtils{ public static char escapeChar = '\0'; } 有没有一个空字符文字,或在Kotlin做这个更好的方法?

Kotlin Gson自定义解串器列表

如何在Gson注册自定义的Json解串器? 当我在java代码注册反序列化所有工作正常,但是当我把Kotlin转换为Java – 反序列化方法不叫。 Kotlin注册码: val listType: Type = object : TypeToken<List<Advert>>() {}.type val gson = GsonBuilder() .registerTypeAdapter(listType, deserializer) val retrofit = Retrofit.Builder() .addConverterFactory(GsonConverterFactory.create(gson)) Java代码: Type listType = new TypeToken<List<Advert>>() {}.getType(); Gson gson = new GsonBuilder() .registerTypeAdapter(listType, deserializer) Retrofit retrofit = new Retrofit.Builder() .addConverterFactory(GsonConverterFactory.create(gson)) 反序列化器声明: class AdvertsDeserializer : JsonDeserializer<List<Advert>> { override fun deserialize(json: JsonElement?, typeOfT: […]