When using Kotlin on Android etc., there are many people who automatically convert from Java with "Convert Java File to Kotlin File". It actually works, but it's a memo to take it one step further and make better use of Kotlin. 3rd time This time, you will learn how to process List and for statements. Kotlin version: 1.4
An example where there is a process to generate a List from a certain object. There are some conditions for creating a List.
ItemData.java
//Object with 7 Strings
public class ItemData {
private String item1;
private String item2;
private String item3;
private String item4;
private String item5;
private String item6;
private String item7;
public ItemData2(String item1, String item2, String item3, String item4,
String item5, String item6, String item7) {
this.item1 = item1;
this.item2 = item2;
this.item3 = item3;
this.item4 = item4;
this.item5 = item5;
this.item6 = item6;
this.item7 = item7;
}
public String getItem1() {
return item1;
}
public String getItem2() {
return item2;
}
public String getItem3() {
return item3;
}
public String getItem4() {
return item4;
}
public String getItem5() {
return item5;
}
public String getItem6() {
return item6;
}
public String getItem7() {
return item7;
}
}
ListUtil.java
//Util to create List from object
public final class ListUtil {
private ListUtil() {
};
private static final int MAX_SIZE = 4;
//Make a list with item1, item2, item3, item4, item5 of the object
//However, if it is null, it will not be added to the List.
public static List<String> createItemList(final ItemData itemData) {
if (itemData == null) {
return new ArrayList<>();
}
final List<String> list = new ArrayList<>();
for (int i = 0; i <= MAX_SIZE; i++) {
switch (i) {
case 0:
if (itemData.item1 != null) {
list.add(itemData.item1);
}
break;
case 1:
if (itemData.item2 != null) {
list.add(itemData.item2);
}
break;
case 2:
if (itemData.item3 != null) {
list.add(itemData.item3);
}
break;
case 3:
if (itemData.item4 != null) {
list.add(itemData.item4);
}
break;
case 4:
if (itemData.item5 != null) {
list.add(itemData.item5);
}
break;
default:
}
}
return list;
}
}
If this is automatically converted, it will look like this ... For the time being, it still works.
ItemData.kt
//Object with 7 Strings
class ItemData(
val item1: String,
val item2: String,
val item3: String,
val item4: String,
val item5: String,
val item6: String,
val item7: String
)
ListUtil.kt
//Util to create List from object
object ListUtil {
private const val MAX_SIZE = 4
//Make a list with item1, item2, item3, item4, item5 of the object
//However, if it is null, it will not be added to the List.
fun createItemList(itemData: ItemData?): List<String?> {
if (itemData == null) {
return ArrayList()
}
val list: MutableList<String?> = ArrayList()
for (i in 0 until MAX_SIZE) {
when (i) {
0 -> if (itemData.item1 != null) {
list.add(itemData.item1)
}
1 -> if (itemData.item2 != null) {
list.add(itemData.item2)
}
2 -> if (itemData.item3 != null) {
list.add(itemData.item3)
}
3 -> if (itemData.item4 != null) {
list.add(itemData.item4)
}
4 -> if (itemData.item5 != null) {
list.add(itemData.item5)
}
else -> {
}
}
}
return list
}
}
Since it is implemented in Kotlin, I want to make it simpler. I will fix it immediately
ItemData.kt
//Specify data as a data class with data
data class ItemData(
val item1: String,
val item2: String,
val item3: String,
val item4: String,
val item5: String,
val item6: String,
val item7: String
)
ListUtil.kt
//Util to create List from object
object ListUtil {
private const val MAX_SIZE = 4
//Make a list with item1, item2, item3, item4, item5 of the object
//However, if it is null, it will not be added to the List.
//The return value is List<String?>→List<String>Is good
fun createItemList(itemData: ItemData?): List<String> =
itemData?.let {
//If null, don't enter here
mutableListOf<String?>().also { list ->
// for (i in 0..MAX_SIZE)Can also be described as
(0 until MAX_SIZE).forEach { i ->
when (i) {
0 -> it.item1
1 -> it.item2
2 -> it.item3
3 -> it.item4
4 -> it.item5
else -> null
}.also {
list += it
}
}
}.filterNotNull()
//This will eliminate the null in the List and make the String NonNull.
//You don't have to check for null when adding
} ?: emptyList()
//Returns an empty List if null
//Empty List is ArrayList()Not emptyList()return it
}
Very simple!
Recommended Posts