HashSet and TreeSet performance test

后端 未结 3 394
执念已碎
执念已碎 2021-01-18 14:50

I read about how TreeSet is slower than HashSet, (that adding elements into a TreeSet is slower) so i made a performance test, i\'m trying to find out if it\'s better to add

3条回答
  •  北恋
    北恋 (楼主)
    2021-01-18 15:49

    0) JVM benchmarking is really complicated. Almost always you're measuring not what you're thinking you are measuring. There's http://openjdk.java.net/projects/code-tools/jmh/ for microbenchmarking from guys from Oracle. And you may try some benchmarking frameworks and guides.

    JIT compiler warmup, initial memory allocation, garbage collector and a LOT of other things may invalidate your benchmark.

    1) See also Hashset vs Treeset regarding your first question

    2) Set treeSet = new TreeSet(); //cant type treeSet.lower(e: E)

    That's how it works. You declare treeSet as Set. Set does not extends NavigableSet. You may explicitly cast if you want to. But if you want to access NavigableSet methods why wouldn't you declare treeSet as NavigableSet

    Set treeSet = new TreeSet(); 
    Integer lower = ((NavigableSet) treeSet).lower(); // thus should work
    

提交回复
热议问题