当前位置:   article > 正文

hashmap 遍历操作 基于 JDK 1.8 一共11个版本_jdk1.8需要用commons-collections4哪个版本

jdk1.8需要用commons-collections4哪个版本

hashmap 遍历操作 基于 JDK 1.8 一共11个版本

HashMap 的遍历操作 结合网上总结11种

  • 使用迭代器Map.Entry

    • long i = 0;
      Iterator<Map.Entry<Integer, Integer>> it = map.entrySet().iterator();
      while (it.hasNext()) {
          Map.Entry<Integer, Integer> pair = it.next();
          i += pair.getKey() + pair.getValue();
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
  • 使用foreachMap.Entry

    • long i = 0;
      for (Map.Entry<Integer, Integer> pair : map.entrySet()) {
          i += pair.getKey() + pair.getValue();
      }
      
      • 1
      • 2
      • 3
      • 4
  • 从Java 8使用forEach

  • final long[] i = {0};
    map.forEach((k, v) -> i[0] += k + v);
    
    • 1
    • 2
  • 使用keySetforeach

  • long i = 0;
    for (Integer key : map.keySet()) {
        i += key + map.get(key);
    }
    
    • 1
    • 2
    • 3
    • 4
  • 使用keySet迭代器

  • long i = 0;
    Iterator<Integer> itr2 = map.keySet().iterator();
    while (itr2.hasNext()) {
        Integer key = itr2.next();
        i += key + map.get(key);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 使用forMap.Entry

  • long i = 0;
    for (Iterator<Map.Entry<Integer, Integer>> entries = map.entrySet().iterator(); entries.hasNext(); ) {
        Map.Entry<Integer, Integer> entry = entries.next();
        i += entry.getKey() + entry.getValue();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
  • 使用Java 8 Stream API

  • final long[] i = {0};
    map.entrySet().stream().forEach(e -> i[0] += e.getKey() + e.getValue());
    
    • 1
    • 2
  • 并行使用Java 8 Stream API

  • final long[] i = {0};
    map.entrySet().stream().parallel().forEach(e -> i[0] += e.getKey() + e.getValue());
    
    • 1
    • 2
  • 使用IterableMapApache Collections

  • long i = 0;
    MapIterator<Integer, Integer> it = iterableMap.mapIterator();
    while (it.hasNext()) {
        i += it.next() + it.getValue();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
  • 使用Eclipse MutableMap(CS)集合

  • final long[] i = {0};
    mutableMap.forEachKeyValue((key, value) -> {
        i[0] += key + value;
    });
    
    • 1
    • 2
    • 3
    • 4
  • 在Java 8中,使用新的lambdas功能来快速

  • Map<String,String> map = new HashMap<>();
     map.put("SomeKey", "SomeValue");
     map.forEach( (k,v) -> [do something with key and value] );
    
     // such as
     map.forEach( (k,v) -> System.out.println("Key: " + k + ": Value: " + v));
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  • 测试用例代码

    • package other_examples;
      
      import org.apache.commons.collections4.IterableMap;
      import org.apache.commons.collections4.MapIterator;
      import org.apache.commons.collections4.map.HashedMap;
      import org.eclipse.collections.api.map.MutableMap;
      import org.eclipse.collections.impl.map.mutable.UnifiedMap;
      import org.openjdk.jmh.annotations.*;
      import org.openjdk.jmh.runner.Runner;
      import org.openjdk.jmh.runner.RunnerException;
      import org.openjdk.jmh.runner.options.Options;
      import org.openjdk.jmh.runner.options.OptionsBuilder;
      
      import java.io.IOException;
      import java.util.HashMap;
      import java.util.Iterator;
      import java.util.Map;
      import java.util.concurrent.TimeUnit;
      import java.util.stream.Collector;
      import java.util.stream.Collectors;
      
      /**
       * Created by vvedenin on 2/21/2016.
       */
      @State(Scope.Benchmark)
      public class IterateThroughHashMapTest {
          private final static Integer SIZE = 100;
      
          @Param({"100","1000"})
          public int size;
      
          private Map<Integer, Integer> map = new HashMap<>(SIZE);
      
          /** 1. Using iterator and Map.Entry **/
          @Benchmark
          public long test1_UsingWhileAndMapEntry() throws IOException {
              long i = 0;
              Iterator<Map.Entry<Integer, Integer>> it = map.entrySet().iterator();
              while (it.hasNext()) {
                  Map.Entry<Integer, Integer> pair = it.next();
                  i += pair.getKey() + pair.getValue();
              }
              return i;
          }
      
          /** 2. Using foreach and Map.Entry **/
          @Benchmark
          public long test2_UsingForEachAndMapEntry() throws IOException {
              long i = 0;
              for (Map.Entry<Integer, Integer> pair : map.entrySet()) {
                  i += pair.getKey() + pair.getValue();
              }
              return i;
          }
      
          /** 3. Using foreach from Java 8 **/
          @Benchmark
          public long test3_UsingForEachAndJava8() throws IOException {
              final long[] i = {0};
              map.forEach((k, v) -> i[0] += k + v);
              return i[0];
          }
      
          /** 4. Using keySet and foreach **/
          @Benchmark
          public long test4_UsingKeySetAndForEach() throws IOException {
              long i = 0;
              for (Integer key : map.keySet()) {
                  i += key + map.get(key);
              }
              return i;
          }
      
          /** 5. Using keySet and iterator **/
          @Benchmark
          public long test5_UsingKeySetAndIterator() throws IOException {
              long i = 0;
              Iterator<Integer> itr2 = map.keySet().iterator();
              while (itr2.hasNext()) {
                  Integer key = itr2.next();
                  i += key + map.get(key);
              }
              return i;
          }
      
          /** 6. Using for and Map.Entry **/
          @Benchmark
          public long test6_UsingForAndIterator() throws IOException {
              long i = 0;
              for (Iterator<Map.Entry<Integer, Integer>> entries = map.entrySet().iterator(); entries.hasNext(); ) {
                  Map.Entry<Integer, Integer> entry = entries.next();
                  i += entry.getKey() + entry.getValue();
              }
              return i;
          }
      
          /** 7. Using Java 8 Stream Api **/
          @Benchmark
          public long test7_UsingJava8StreamApi() throws IOException {
              final long[] i = {0};
              map.entrySet().stream().forEach(e -> i[0] += e.getKey() + e.getValue());
              return i[0];
          }
      
          /** 8. Using Java 8 Stream Api parallel **/
          @Benchmark
          public long test8_UsingJava8StreamApiParallel() throws IOException {
              final long[] i = {0};
              map.entrySet().stream().parallel().forEach(e -> i[0] += e.getKey() + e.getValue());
              return i[0];
          }
      
          /** 9. Using Apache IterableMap **/
          private IterableMap<Integer, Integer> iterableMap = new HashedMap<>(SIZE);
          @Benchmark
          public long test9_UsingApacheIterableMap() throws IOException {
              long i = 0;
              MapIterator<Integer, Integer> it = iterableMap.mapIterator();
              while (it.hasNext()) {
                  i += it.next() + it.getValue();
              }
              return i;
          }
      
          /** 10. Using MutableMap of Eclipse (CS) collections **/
          private MutableMap<Integer, Integer> mutableMap = UnifiedMap.newMap(SIZE);
          @Benchmark
          public long test10_UsingEclipseMap() throws IOException {
              final long[] i = {0};
              mutableMap.forEachKeyValue((key, value) -> {
                  i[0] += key + value;
              });
              return i[0];
          }
      
          /** 11. Using Java 8 Stream Api 2 **/
          @Benchmark
          public long test11_UsingJava8StreamApi2() throws IOException {
              return map.entrySet().stream().mapToLong(e -> e.getKey() + e.getValue()).sum();
          }
      
          /** 12. Using Java 8 Stream Api parallel 2  **/
          @Benchmark
          public long test12_UsingJava8StreamApiparallel2() throws IOException {
              return map.entrySet().parallelStream().mapToLong(e -> e.getKey() + e.getValue()).sum();
          }
      
          @TearDown(Level.Iteration)
          public void tearDown() {
              map = new HashMap<>(size);
              iterableMap = new HashedMap<>(size);
              mutableMap = UnifiedMap.newMap(size);
              for (int i = 0; i < size; i++) {
                  map.put(i, i);
                  mutableMap.put(i, i);
                  iterableMap.put(i, i);
              }
          }
      
          public static void main(String[] args) throws RunnerException {
              Options opt = new OptionsBuilder()
                      .include(IterateThroughHashMapTest.class.getSimpleName())
                      .timeUnit(TimeUnit.MICROSECONDS)
                      .warmupIterations(3)
                      .measurementIterations(5)
                      .param("size","100",/*"500","900","1300","1700","2100","2500","5000","10000","15000","20000","25000" ,*/ "30000")
                      .forks(1)
                      .mode(Mode.AverageTime)
                      .build();
      
              new Runner(opt).run();
          }
      }
      
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
      • 46
      • 47
      • 48
      • 49
      • 50
      • 51
      • 52
      • 53
      • 54
      • 55
      • 56
      • 57
      • 58
      • 59
      • 60
      • 61
      • 62
      • 63
      • 64
      • 65
      • 66
      • 67
      • 68
      • 69
      • 70
      • 71
      • 72
      • 73
      • 74
      • 75
      • 76
      • 77
      • 78
      • 79
      • 80
      • 81
      • 82
      • 83
      • 84
      • 85
      • 86
      • 87
      • 88
      • 89
      • 90
      • 91
      • 92
      • 93
      • 94
      • 95
      • 96
      • 97
      • 98
      • 99
      • 100
      • 101
      • 102
      • 103
      • 104
      • 105
      • 106
      • 107
      • 108
      • 109
      • 110
      • 111
      • 112
      • 113
      • 114
      • 115
      • 116
      • 117
      • 118
      • 119
      • 120
      • 121
      • 122
      • 123
      • 124
      • 125
      • 126
      • 127
      • 128
      • 129
      • 130
      • 131
      • 132
      • 133
      • 134
      • 135
      • 136
      • 137
      • 138
      • 139
      • 140
      • 141
      • 142
      • 143
      • 144
      • 145
      • 146
      • 147
      • 148
      • 149
      • 150
      • 151
      • 152
      • 153
      • 154
      • 155
      • 156
      • 157
      • 158
      • 159
      • 160
      • 161
      • 162
      • 163
      • 164
      • 165
      • 166
      • 167
      • 168
      • 169
      • 170
      • 171
      • 172
      • 173
      • 174
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/564553
推荐阅读
相关标签
  

闽ICP备14008679号