This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Main { | |
public static void main(String[] args) { | |
Integer a = new Integer(10); | |
Integer b = new Integer(10); | |
if (a.equals(b)) { | |
System.out.println("true"); | |
} else { | |
System.out.println("false"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DoInBackground(. . . ) { | |
//Doing some work | |
if (isCancelled()) { | |
// we are cancelled, no need to do any work now | |
. . . | |
// Did we override implement onCancelled() or onCancelled(Object)? | |
} | |
for (CurrentWork : ListOfWorks) { | |
if (isCancelled()) { | |
// we are cancelled, no need to do any work now |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#region Using declarations | |
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.ComponentModel.DataAnnotations; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Windows; | |
using System.Windows.Input; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun testGist() { | |
println("Hello world!") | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kotlinx.coroutines.* | |
class Debouncer(private val delayMs: Long = 300) { | |
private var debounceJob: Job? = null | |
fun submit(scope: CoroutineScope, action: () -> Unit) { | |
debounceJob?.cancel() | |
debounceJob = scope.launch { | |
delay(delayMs) | |
action() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun <T> Flow<T>.distinctUntilChanged(comparator: (T, T) -> Boolean): Flow<T> = flow { | |
var previous: T? = null | |
var isFirst = true | |
collect { value -> | |
if (isFirst || !comparator(previous!!, value)) { | |
emit(value) | |
previous = value | |
isFirst = false | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun <T, R> Flow<T>.flatMapLatest(transform: suspend (T) -> Flow<R>): Flow<R> = flow { | |
// This coroutineScope is key to managing cancellation of the inner flow | |
coroutineScope { | |
var previousJob: Job? = null | |
collect { value -> | |
// Cancel any ongoing inner flow collection | |
previousJob?.cancelAndJoin() | |
// Launch new inner flow collector in a new coroutine |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kotlinx.coroutines.* | |
import kotlinx.coroutines.flow.* | |
/** | |
* A ViewModel to manage the logic of a single item in our shopping cart. | |
*/ | |
class ShoppingCartItemViewModel { | |
// A private MutableStateFlow to hold the current quantity of the item. | |
// This will be updated by the UI when the user clicks the '+' or '-' buttons. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kotlinx.coroutines.* | |
import kotlinx.coroutines.flow.* | |
/** | |
* A ViewModel to manage the logic of a single item in our shopping cart. | |
*/ | |
class ShoppingCartItemViewModel { | |
// A private MutableStateFlow to hold the current quantity of the item. | |
// This will be updated by the UI when the user clicks the '+' or '-' buttons. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.stream.Collectors; | |
int[] primitiveArray = {1, 2, 3, 4, 5}; | |
// Use Arrays.stream() to convert the int[] to an IntStream, then collect it | |
ArrayList<Integer> arrayList = Arrays.stream(primitiveArray) | |
.boxed() // Converts int to Integer | |
.collect(Collectors.toCollection(ArrayList::new)); |