File tree Expand file tree Collapse file tree
java/com/baeldung/dataclass
kotlin/com/baeldung/dataclass Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .baeldung .dataclass ;
2+
3+ public class Movie {
4+
5+ private String name ;
6+ private String studio ;
7+ private float rating ;
8+
9+ public Movie (String name , String studio , float rating ) {
10+ this .name = name ;
11+ this .studio = studio ;
12+ this .rating = rating ;
13+ }
14+
15+ public String getName () {
16+ return name ;
17+ }
18+
19+ public void setName (String name ) {
20+ this .name = name ;
21+ }
22+
23+ public String getStudio () {
24+ return studio ;
25+ }
26+
27+ public void setStudio (String studio ) {
28+ this .studio = studio ;
29+ }
30+
31+ public float getRating () {
32+ return rating ;
33+ }
34+
35+ public void setRating (float rating ) {
36+ this .rating = rating ;
37+ }
38+
39+ @ Override
40+ public int hashCode () {
41+ final int prime = 31 ;
42+ int result = 1 ;
43+
44+ result = prime * result + ((name == null ) ? 0 : name .hashCode ());
45+ result = prime * result + Float .floatToIntBits (rating );
46+ result = prime * result + ((studio == null ) ? 0 : studio .hashCode ());
47+
48+ return result ;
49+ }
50+
51+ @ Override
52+ public boolean equals (Object obj ) {
53+ if (this == obj )
54+ return true ;
55+
56+ if (obj == null )
57+ return false ;
58+
59+ if (getClass () != obj .getClass ())
60+ return false ;
61+
62+ Movie other = (Movie ) obj ;
63+
64+ if (name == null ) {
65+ if (other .name != null )
66+ return false ;
67+
68+ } else if (!name .equals (other .name ))
69+ return false ;
70+
71+ if (Float .floatToIntBits (rating ) != Float .floatToIntBits (other .rating ))
72+ return false ;
73+
74+ if (studio == null ) {
75+ if (other .studio != null )
76+ return false ;
77+
78+ } else if (!studio .equals (other .studio ))
79+ return false ;
80+
81+ return true ;
82+ }
83+
84+ @ Override
85+ public String toString () {
86+ return "Movie [name=" + name + ", studio=" + studio + ", rating=" + rating + "]" ;
87+ }
88+ }
Original file line number Diff line number Diff line change 1+ package com.baeldung.dataclass
2+
3+ data class Movie (val name : String , val studio : String , var rating : Float )
Original file line number Diff line number Diff line change 1+ package com.baeldung.dataclass
2+
3+ fun main (args : Array <String >) {
4+
5+ val movie = Movie (" Whiplash" , " Sony Pictures" , 8.5F )
6+
7+ println (movie.name) // Whiplash
8+ println (movie.studio) // Sony Pictures
9+ println (movie.rating) // 8.5
10+
11+ movie.rating = 9F
12+
13+ println (movie.toString()) // Movie(name=Whiplash, studio=Sony Pictures, rating=9.0)
14+
15+ val betterRating = movie.copy(rating = 9.5F )
16+ println (betterRating.toString()) // Movie(name=Whiplash, studio=Sony Pictures, rating=9.5)
17+
18+ movie.component1() // name
19+ movie.component2() // studio
20+ movie.component3() // rating
21+
22+ val (name, studio, rating) = movie
23+
24+ fun getMovieInfo () = movie
25+ val (namef, studiof, ratingf) = getMovieInfo()
26+ }
You can’t perform that action at this time.
0 commit comments