Skip to content

Instantly share code, notes, and snippets.

@fajran fajran/

Last active Dec 18, 2015
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;
public int hashCode() {
return posisi;
public boolean equals(Object other) {
if (other instanceof M4) {
M4 m = (M4)other;
return posisi == m.getPosisi() && asamAmino.equals(m.getAsamAmino());
return false;
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("-- 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("-- 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("-- M4")
val list4 = List(new M4(1, "a"), new M4(2, "b"), new M4(1, "c"), new M4(3, "b"), new M4(1, "a"))
Jalankan perintah berikut untuk mencoba:
$ javac
$ scalac TesUnik.scala
$ scala TesUnik
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.