Skip to content

Instantly share code, notes, and snippets.

Avatar

Tagir Valeev amaembo

  • Novosibirsk, Russia
View GitHub Profile
@amaembo
amaembo / PairTest.java
Last active Oct 12, 2021
JMH benchmark for different solutions of http://stackoverflow.com/q/30089761/4856258
View PairTest.java
package org.sample;
import java.util.concurrent.TimeUnit;
import javax.util.streamex.*;
import java.util.stream.*;
import java.util.*;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.annotations.*;
@amaembo
amaembo / stream_chain.svg
Created Sep 4, 2021
Stream Chain visualization
View stream_chain.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@amaembo
amaembo / Circle.java
Created Jul 19, 2021
Bresenham circle algorithm
View Circle.java
public class Circle {
public static void main(String[] args) {
// Preparation
int radius = Integer.parseInt(args[0]);
int rasterSize = radius * 2 + 1;
boolean[][] raster = new boolean[rasterSize][rasterSize];
// Bresenham algorithm
int y = radius;
int err = radius;
@amaembo
amaembo / Test.java
Created Aug 19, 2021
StreamEx reducingWithZero demo
View Test.java
import one.util.streamex.MoreCollectors;
import one.util.streamex.StreamEx;
import java.util.stream.Collector;
class Test {
enum MyLattice {
/*
TOP
/ \
@amaembo
amaembo / InnerClassLambdaMetafactory.java.patch
Created Jul 21, 2021
Patch for InnerClassLambdaMetafactory to generate line number debug info
View InnerClassLambdaMetafactory.java.patch
diff --git a/src/java.base/share/classes/java/lang/invoke/InnerClassLambdaMetafactory.java b/src/java.base/share/classes/java/lang/invoke/InnerClassLambdaMetafactory.java
--- a/src/java.base/share/classes/java/lang/invoke/InnerClassLambdaMetafactory.java (revision 57611b30219191160f7faccb811b41a31c25c0b8)
+++ b/src/java.base/share/classes/java/lang/invoke/InnerClassLambdaMetafactory.java (date 1626887157914)
@@ -315,6 +315,18 @@
return generateInnerClass();
}
+ private static StackTraceElement getCallerFrame() {
+ StackTraceElement[] trace = new Exception().getStackTrace();
+ for (int i = 0; i < trace.length - 1; i++) {
View StringBuilderInHashMap.java
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 Mar 22, 2021
Break HashSet with incorrect compareTo implementation
View BreakHashSet.java
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 Dec 14, 2020
Which collection inherits comparator?
View WhoInheritsComparator.java
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 Sep 21, 2020
Comparison of linear and binary search in Java
View Search.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 Sep 5, 2020
Multi-dimensional arrays
View MyClass.java
import java.lang.annotation.*;
import java.lang.reflect.*;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE_USE)
@interface TA {
int value();
}
public class MyClass {