Kotlin data class: saves a lot of lines of code (KAD 10) and kotlin many lines

Source: Internet
Author: User

Kotlin data class: saves a lot of lines of code (KAD 10) and kotlin many lines

By Antonio Leiva

Time: Jan 25,201 7

Link: https://antonioleiva.com/data-classes-kotlin/

 

 

In the previous article, we have seen classes, and data classes can further help us simplify code.

 

What is a data class?

 

DataIs an action that only contains the status and does not have any executable operations.

 

The benefit of replacing common classes with data classes is that Kotlin helps us generate a large amount of code.

 

In particular, it saves us the following work:

  • Declare attributes in the constructor: this technology is notDataUnique, but apart from constructors, it avoids all getter and setter templates.
  • Equals ()/hashCode ()
  • A group is called comopinentX (). Later we will be able to learn about the great things they allow us to do.
  • The Copy () method is useful when we use an immutable object.

 

Java code and DataHow to compare

 

Here is a wonderful comparison. Although IDE can generate almost all the code, in Java, we need to implement the data class as follows:

 1 public class Person { 2   3     private String name; 4     private String surname; 5     private String id; 6   7     public String getName() { 8         return name; 9     }10  11     public void setName(String name) {12         this.name = name;13     }14  15     public String getSurname() {16         return surname;17     }18  19     public void setSurname(String surname) {20         this.surname = surname;21     }22  23     public String getId() {24         return id;25     }26  27     public void setId(String id) {28         this.id = id;29     }30  31     @Override public boolean equals(Object o) {32         if (this == o) return true;33         if (o == null || getClass() != o.getClass()) return false;34  35         Person person = (Person) o;36  37         if (name != null ? !name.equals(person.name) : person.name != null) return false;38         if (surname != null ? !surname.equals(person.surname) : person.surname != null)39             return false;40         return id != null ? id.equals(person.id) : person.id == null;41  42     }43  44     @Override public int hashCode() {45         int result = name != null ? name.hashCode() : 0;46         result = 31 * result + (surname != null ? surname.hashCode() : 0);47         result = 31 * result + (id != null ? id.hashCode() : 0);48         return result;49     }50  51     @Override public String toString() {52         return "Person{" +53                 "name='" + name + ''' +54                 ", surname='" + surname + ''' +55                 ", id='" + id + ''' +56                 '}';57     }58 }

 

In addition, we still haven't implemented the same functions as the Kotlin code:

1 data class Person(var name: String, var surname: String, var id: String)

 

Here we can see the potential of Kotlin, saving us a lot of useless code.

 

Class Reconstruction

 

This is the usage of componentX. Thanks to them, you can use this method to parse the variables in the data class:

1 val person = Person("x", "y", "z")2 val (n, s, i) = person

 

Thanks to all of this, you can parse the map key-value pairs in the Loop:

1 val map = mapOf(1 to "a", 2 to "b")2  3 for ((key, value) in map) {4     toast("key: $key, value: $value")5 }

 

Object Replication

 

As we mentioned earlier, it is a good practice under every possible immutability. If we do not change the implementation of the prefix class:

1 data class Person(val name: String, val surname: String, val id: String)

 

You cannot change the last name.

 

To change the object state, you need to copy the object to a new value. This requires the copy function:

1 val person = Person("John", "Smith", "123abc")2 val person2 = person.copy(surname="Rogers")

 

The Copy function can obtain as many parameter values as possible. As you can see, function parameters can be named, so that you can specify which one you want to change.

 

Conclusion

 

DataIt can save a lot of Java to force us to generate sample code, so that the final code is easier to understand and maintain.

 

If you like this article, you can get my free guide and continue to learn Kotlin. In this guide, I show you how to create your first Kotlin project.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.