Kotlinlearncs.online LogoJava

    ← Prev

    Index

    Next →

    Kotlin
    Java
    • Implementing a Map : 56

    • Hashing : 55

    • Binary Search : 54

    • Quicksort : 53

    • Merge Sort : 52

    • Sorting Algorithms : 51

    • Practice with Recursion : 50

    • Trees and Recursion : 49

    • Trees : 48

    • Recursion : 47

    • Lists Review and Performance : 46

    • Linked Lists : 45

    • Algorithms and Lists : 44

    • Lambda Expressions : 43

    • Anonymous Classes : 42

    • Practice with Interfaces : 41

    • Implementing Interfaces : 40

    • Using Interfaces : 39

    • Working with Exceptions : 38

    • Throwing Exceptions : 37

    • Catching Exceptions : 36

    • References and Polymorphism : 35

    • References : 34

    • Data Modeling 2 : 33

    • Equality and Object Copying : 32

    • Polymorphism : 31

    • Inheritance : 30

    • Data Modeling 1 : 29

    • Companion Objects : 28

    • Encapsulation : 27

    • Constructors : 26

    • Objects, Continued : 25

    • Introduction to Objects : 24

    • Compilation and Immutability : 23

    • Practice with Collections : 22

    • Maps and Sets : 21

    • Lists and Type Parameters : 20

    • Imports and Libraries : 19

    • Multidimensional Arrays : 18

    • Practice with Strings : 17

    • null : 16

    • Algorithms and Strings : 15

    • Strings : 14

    • Functions and Algorithms : 13

    • Practice with Functions : 12

    • More About Functions : 11

    • Errors and Debugging : 10

    • Functions : 9

    • Practice with Loops and Algorithms : 8

    • Algorithms I : 7

    • Loops : 6

    • Arrays : 5

    • Compound Conditionals : 4

    • Conditional Expressions and Statements : 3

    • Operations on Variables : 2

    • Variables and Types : 1

    • Hello, world! : 0

    Hashing

    println("you".hashCode())
    println("you are not".hashCode())
    println("you are not alone!".hashCode())

    Welcome back! This lesson explores hashing, a mysterious and yet incredibly useful idea that is even included directly in our programming language!

    Our next lesson covers some material that is both extremely cool and very useful for real-world programming. So let’s get started! Up first—the mysterious and useful power of hashing…

    What is Hashing?
    What is Hashing?

    Wikipedia defines a hash function:

    A hash function is any function that can be used to map data of arbitrary size to fixed-size values. The values returned by a hash function are called hash values, hash codes, digests, or simply hashes.

    Let’s unpack that definition together…

    Kotlin’s hashCode
    Kotlin’s hashCode

    Hashing turns out to be so useful that a hash function is one of the built-in methods that every Kotlin object provides. Let’s see how that works, and how to develop hash codes for our own classes:

    // hashCode Examples

    How Are Hashes Used?
    How Are Hashes Used?

    Are hash functions useful? Yes! Extremely! And, they are also very widely used! Let’s go through a few examples.

    Download Verification
    Download Verification

    Hash functions are commonly used to verify downloaded files. Let’s see that in action.

    Content Fingerprinting
    Content Fingerprinting

    Download verification is one example of an application of hash functions known as content fingerprinting. Let’s look at another: Git!

    Proof of Work
    Proof of Work

    Hash functions also appear in another perhaps-unexpected place: Bitcoin mining! Let’s explore the fundamental concept of proof of work that is used to add blocks to the blockchain:

    // Proof-of-Work Example

    Practice: Course Hash Code

    Created By: Geoffrey Challen
    / Version: 2020.10.0

    Create a public class named Course providing a single constructor setting three private String? properties: department, number, and title, in that order. Implement hashCode using java.util.Objects by hashing the department and number, only, in that order.

    Hash Collisions
    Hash Collisions

    If a hash function produces the same output for two different inputs, this is known as a collision. Whether or not a hash collision is a problem depends a lot on the application. But, it turns out that hash collisions are much more common than we might imagine! Let’s see why.

    Homework: Restaurant Hash Code

    Created By: Geoffrey Challen
    / Version: 2021.11.0

    Create a public class named Restaurant providing a single primary constructor setting three private String properties: id, name, and cuisine, in that order. Implement hashCode using java.util.Objects by hashing the restaurant's id and name, only, in that order.

    More Practice

    Need more practice? Head over to the practice page.