Instantly share code, notes, and snippets.

@fajran /M4.java
Last active Dec 18, 2015

Embed
What would you like to do?
Himpunan unik di Scala
public class M4 {
private int posisi;
private String asamAmino;
public M4(int posisi, String asamAmino) {
this.posisi = posisi;
this.asamAmino = asamAmino;
}
public int getPosisi() {
return posisi;
}
public String getAsamAmino() {
return asamAmino;
}
@Override
public int hashCode() {
return posisi;
}
@Override
public boolean equals(Object other) {
if (other instanceof M4) {
M4 m = (M4)other;
return posisi == m.getPosisi() && asamAmino.equals(m.getAsamAmino());
}
return false;
}
@Override
public String toString() {
return posisi + asamAmino;
}
}
case class M1(val posisi: Int, val asamAmino: String)
class M2(val posisi: Int, val asamAmino: String) {
override def toString() = "%d%s".format(posisi, asamAmino)
}
class M3(val posisi: Int, val asamAmino: String) {
override def toString() = "%d%s".format(posisi, asamAmino)
override def hashCode = posisi
override def equals(other: Any) =
if (other.isInstanceOf[M3]) {
val m = other.asInstanceOf[M3]
m.posisi == posisi && m.asamAmino == asamAmino
} else false
}
object TesUnik extends App {
println("-- M1")
val list1 = List(M1(1, "a"), M1(2, "b"), M1(1, "c"), M1(3, "b"), M1(1, "a"))
println(list1)
println(list1.distinct)
println("-- M2")
val list2 = List(new M2(1, "a"), new M2(2, "b"), new M2(1, "c"), new M2(3, "b"), new M2(1, "a"))
println(list2)
println(list2.distinct)
println("-- M3")
val list3 = List(new M3(1, "a"), new M3(2, "b"), new M3(1, "c"), new M3(3, "b"), new M3(1, "a"))
println(list3)
println(list3.distinct)
println("-- M4")
val list4 = List(new M4(1, "a"), new M4(2, "b"), new M4(1, "c"), new M4(3, "b"), new M4(1, "a"))
println(list4)
println(list4.distinct)
}
/*
Jalankan perintah berikut untuk mencoba:
$ javac M4.java
$ scalac TesUnik.scala
$ scala TesUnik
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment