Skip to content

Instantly share code, notes, and snippets.

View amaembo's full-sized avatar

Tagir Valeev amaembo

View GitHub Profile
import java.util.*;
import java.util.stream.*;
public class StringBuilderInHashMap {
public static void main(String[] args) {
List<StringBuilder> list = Stream.generate(() -> {
while (true) {
StringBuilder sb = new StringBuilder("a");
int hc = sb.hashCode();
if (((hc ^ (hc >>> 16)) & 0x3F) == 0) {
@amaembo
amaembo / BreakHashSet.java
Created March 22, 2021 02:30
Break HashSet with incorrect compareTo implementation
import java.util.*;
class BreakHashSet {
public static void main(String[] args) {
Point[] points = new Point[20];
Arrays.setAll(points, idx -> new Point(idx * 500_000_000,
-new Point(idx * 500_000_000, 0).hashCode()));
Set<Point> set = new HashSet<>(Arrays.asList(points));
set.remove(points[1]);
System.out.println(set.contains(points[14])); // Prints false!
@amaembo
amaembo / WhoInheritsComparator.java
Created December 14, 2020 09:55
Which collection inherits comparator?
import java.util.Collection;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.concurrent.PriorityBlockingQueue;
public class WhoInheritsComparator {
public static void main(String[] args) {
SortedSet<Integer> c1 = new TreeSet<>(Comparator.reverseOrder());
@amaembo
amaembo / Search.java
Created September 21, 2020 07:35
Comparison of linear and binary search in Java
package com.com.example;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.CompilerControl;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
@amaembo
amaembo / MyClass.java
Created September 5, 2020 08:57
Multi-dimensional arrays
import java.lang.annotation.*;
import java.lang.reflect.*;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE_USE)
@interface TA {
int value();
}
public class MyClass {
import java.util.Arrays;
import java.util.List;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
public class PairMapSample {
import java.lang.annotation.*;
import java.lang.reflect.*;
import java.util.Arrays;
enum MyEnum {
A(0.0, 0, "");
MyEnum(@Foo("double annotated") double d,
int i,
@Foo("string annotated") String s) { }
import java.util.concurrent.TimeUnit;
public class AbstractEnumConverter<T extends Enum<T>> {
private final Class<T> enumClass;
private final T[] values;
protected AbstractEnumConverter(Class<T> enumClass) {
assert enumClass.isEnum();
this.enumClass = enumClass;
this.values = enumClass.getEnumConstants();
@amaembo
amaembo / BenchmarkStreamConcat.java
Created March 31, 2020 03:06
Stream.concat vs StreamEx
package com.soebes.performance.streams;
import org.openjdk.jmh.annotations.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
@amaembo
amaembo / keybase.md
Created January 23, 2020 09:09
keybase.md

Keybase proof

I hereby claim:

  • I am amaembo on github.
  • I am tagir_valeev (https://keybase.io/tagir_valeev) on keybase.
  • I have a public key ASCH6kfJO8v94QIiMxjf6F-9bUZjiAGg5eoDfSUshwFNmAo

To claim this, I am signing this object: