Skip to content

Instantly share code, notes, and snippets.

View starblood's full-sized avatar

mong starblood

View GitHub Profile
Interface Common Implementation(s) Comparison Method
Set HashSet hashCode → equals
Map HashMap hashCode → equals (for keys)
List ArrayList, LinkedList equals only (sequential)
SortedSet TreeSet compareTo / Comparator
SortedMap TreeMap compareTo / Comparator
LinkedHashSet Linked hash table (ordered) hashCode → equals
LinkedHashMap Linked hash table (ordered) hashCode → equals
public class Person {
private final String firstName;
private final String lastName;
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
@Override
class Person(val firstName: String, val lastName: String) {
override fun equals(other: Any?): Boolean {
val otherPerson = other as? Person ?: return false
return otherPerson.firstName == firstName &&
otherPerson.lastName == lastName
}
override fun hashCode(): Int =
firstName.hashCode() * 31 + lastName.hashCode()
override fun hashCode(): Int = firstName.hashCode() * 37 + lastName.hashCode()
override fun equals(other: Any?): Boolean {
val otherPerson = other as? Person ?: return false
return otherPerson.firstName == firstName &&
otherPerson.lastName == lastName
}
@starblood
starblood / SafeCastExample.kt
Last active August 11, 2025 12:53
safe cast in Kotlin
class Person(val firstName: String, val lastName: String) {
override fun equals(other: Any?): Boolean {
val otherPerson = other as? Person ?: return false
return otherPerson.firstName == firstName &&
otherPerson.lastName == lastName
}
override fun hashCode(): Int = firstName.hashCode() * 37 + lastName.hashCode()
}
@starblood
starblood / zio-example.scala
Created May 11, 2025 06:10
ZIO Effect Staging example
val program =
for {
_ <- Console.printLine("Starting…")
_ <- Console.printLine("Doing work…")
_ <- Console.printLine("Finished!")
} yield ()
Runtime.default.unsafeRun(program) // single execution point
@starblood
starblood / effect-example.kt
Created May 11, 2025 06:01
Using Staged Architecture for Effects example code
// (1) Pure computation stage
fun calculateSomething(input: Int): Int =
input * 2 + 1
// (2) Stage that *describes* an effect
data class WriteToFileCommand(val path: String, val content: String)
// (3) Stage that *performs* the effect
fun executeCommand(cmd: WriteToFileCommand) {
File(cmd.path).writeText(cmd.content)
@starblood
starblood / KafkaTopicTest.scala
Created September 1, 2017 09:08
find topic and partitions info from ZkUtils
import kafka.utils.ZkUtils
object KafkaTopicTest {
def createZkUtils(): ZkUtils = {
val zookeepers = "localhost:2181"
val timeout = 10000
ZkUtils(zookeepers, timeout, timeout, isZkSecurityEnabled = false)
}
def getPartitionsForTopics(topics: Seq[String]): Map[String, Seq[Int]] = {
@starblood
starblood / check_kafka_lag.sh
Last active November 28, 2016 08:44
check lag for specified topic from kafka
#!/bin/bash
kafka_home=$1
zookeeper=$2
group_name=$3
lag_threshold=$4
lag_data_sampling_count=$5
if [ ! -z $6 ] ; then
lag_data_file=$6
else