`
YuHuang.Neil
  • 浏览: 187766 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Java中WeakHashMap类型使用示例

阅读更多
在Java集合中有一种特殊的Map类型——WeakHashMap,在这种Map中存放了键对象的弱引用,当一个键对象被垃圾回收器回收时,那么相应的值对象的引用会从Map中删除。WeakHashMap能够节省存储空间,可用来缓存那些非必须存在的数据。


下面程序段测试了垃圾回收器对WeakHashMap中值对象的回收过程,其中能够被3整除的键不会被垃圾回收器回收是由于其引用是强引用。



import java.util.*;
import java.lang.ref.*;


class Key {
        String id;

        public Key(String id) {
                this.id = id;
        }

        public String toString() {
                return id;
        }

        public int hashCode() {
                return id.hashCode();
        }

        public boolean equals(Object r) {
                return (r instanceof Key)
                        && id.equals(((Key)r).id );
        }

        public void finalize() {
                System.out.println("Finalizing Key "+id);
        }
}

class Value {
        String id;

        public Value(String id) {
                this.id = id;
        }

        public String toString() {
                return id;
        }

        public void finalize(){
                System.out.println("Finalizing Value "+id);
        }

}


public class MapCache {
        public static void main(String[] args) throws Exception {
                int size = 1000;

                if(args.length > 0) {
                        size = Integer.parseInt(args[0]);
                }

                Key[] keys = new Key[size];
                WeakHashMap<Key,Value> whm = new WeakHashMap<Key,Value>();

                for(int i=0;i<size;i++) {
                        Key k = new Key(Integer.toString(i));
                        Value v = new Value(Integer.toString(i));
                        if(i%3==0) {
                                keys[i]=k;
                        }

                        whm.put(k,v);
                }

                System.gc();

                Thread.sleep(8000);  //把处理器的时间让给垃圾回收器进行垃圾回收
        }
}





代码运行的结果:
Lab-Computer-0db2f6:JavaExercises labuser$ java MapCache
Finalizing Key 250
Finalizing Key 998
Finalizing Key 997
Finalizing Key 995
Finalizing Key 994
Finalizing Key 992
Finalizing Key 991
Finalizing Key 989
Finalizing Key 988
Finalizing Key 986
Finalizing Key 985
Finalizing Key 983
Finalizing Key 982
Finalizing Key 980
Finalizing Key 979
Finalizing Key 977
Finalizing Key 976
Finalizing Key 974
Finalizing Key 973
Finalizing Key 971
Finalizing Key 970
Finalizing Key 968
Finalizing Key 967
Finalizing Key 965
Finalizing Key 964
Finalizing Key 962
Finalizing Key 961
Finalizing Key 959
Finalizing Key 958
Finalizing Key 956
Finalizing Key 955
Finalizing Key 953
Finalizing Key 952
Finalizing Key 950
Finalizing Key 949
Finalizing Key 947
Finalizing Key 946
Finalizing Key 944
Finalizing Key 943
Finalizing Key 941
Finalizing Key 940
Finalizing Key 938
Finalizing Key 937
Finalizing Key 935
Finalizing Key 934
Finalizing Key 932
Finalizing Key 931
Finalizing Key 929
Finalizing Key 928
Finalizing Key 926
Finalizing Key 925
Finalizing Key 923
Finalizing Key 922
Finalizing Key 920
Finalizing Key 919
Finalizing Key 917
Finalizing Key 916
Finalizing Key 914
Finalizing Key 913
Finalizing Key 911
Finalizing Key 910
Finalizing Key 908
Finalizing Key 907
Finalizing Key 905
Finalizing Key 904
Finalizing Key 902
Finalizing Key 901
Finalizing Key 899
Finalizing Key 898
Finalizing Key 896
Finalizing Key 895
Finalizing Key 893
Finalizing Key 892
Finalizing Key 890
Finalizing Key 889
Finalizing Key 887
Finalizing Key 886
Finalizing Key 884
Finalizing Key 883
Finalizing Key 881
Finalizing Key 880
Finalizing Key 878
Finalizing Key 877
Finalizing Key 875
Finalizing Key 874
Finalizing Key 872
Finalizing Key 871
Finalizing Key 869
Finalizing Key 868
Finalizing Key 866
Finalizing Key 865
Finalizing Key 863
Finalizing Key 862
Finalizing Key 860
Finalizing Key 859
Finalizing Key 857
Finalizing Key 856
Finalizing Key 854
Finalizing Key 853
Finalizing Key 851
Finalizing Key 850
Finalizing Key 848
Finalizing Key 847
Finalizing Key 845
Finalizing Key 844
Finalizing Key 842
Finalizing Key 841
Finalizing Key 839
Finalizing Key 838
Finalizing Key 836
Finalizing Key 835
Finalizing Key 833
Finalizing Key 832
Finalizing Key 830
Finalizing Key 829
Finalizing Key 827
Finalizing Key 826
Finalizing Key 824
Finalizing Key 823
Finalizing Key 821
Finalizing Key 820
Finalizing Key 818
Finalizing Key 817
Finalizing Key 815
Finalizing Key 814
Finalizing Key 812
Finalizing Key 811
Finalizing Key 809
Finalizing Key 808
Finalizing Key 806
Finalizing Key 805
Finalizing Key 803
Finalizing Key 802
Finalizing Key 800
Finalizing Key 799
Finalizing Key 797
Finalizing Key 796
Finalizing Key 794
Finalizing Key 793
Finalizing Key 791
Finalizing Key 790
Finalizing Key 788
Finalizing Key 787
Finalizing Key 785
Finalizing Key 784
Finalizing Key 782
Finalizing Key 781
Finalizing Key 779
Finalizing Key 778
Finalizing Key 776
Finalizing Key 775
Finalizing Key 773
Finalizing Key 772
Finalizing Key 770
Finalizing Key 769
Finalizing Key 767
Finalizing Key 766
Finalizing Key 764
Finalizing Key 763
Finalizing Key 761
Finalizing Key 760
Finalizing Key 758
Finalizing Key 757
Finalizing Key 755
Finalizing Key 754
Finalizing Key 752
Finalizing Key 751
Finalizing Key 749
Finalizing Key 748
Finalizing Key 746
Finalizing Key 745
Finalizing Key 743
Finalizing Key 742
Finalizing Key 740
Finalizing Key 739
Finalizing Key 737
Finalizing Key 736
Finalizing Key 734
Finalizing Key 733
Finalizing Key 731
Finalizing Key 730
Finalizing Key 728
Finalizing Key 727
Finalizing Key 725
Finalizing Key 724
Finalizing Key 722
Finalizing Key 721
Finalizing Key 719
Finalizing Key 718
Finalizing Key 716
Finalizing Key 715
Finalizing Key 713
Finalizing Key 712
Finalizing Key 710
Finalizing Key 709
Finalizing Key 707
Finalizing Key 706
Finalizing Key 704
Finalizing Key 703
Finalizing Key 701
Finalizing Key 700
Finalizing Key 698
Finalizing Key 697
Finalizing Key 695
Finalizing Key 694
Finalizing Key 692
Finalizing Key 691
Finalizing Key 689
Finalizing Key 688
Finalizing Key 686
Finalizing Key 685
Finalizing Key 683
Finalizing Key 682
Finalizing Key 680
Finalizing Key 679
Finalizing Key 677
Finalizing Key 676
Finalizing Key 674
Finalizing Key 673
Finalizing Key 671
Finalizing Key 670
Finalizing Key 668
Finalizing Key 667
Finalizing Key 665
Finalizing Key 664
Finalizing Key 662
Finalizing Key 661
Finalizing Key 659
Finalizing Key 658
Finalizing Key 656
Finalizing Key 655
Finalizing Key 653
Finalizing Key 652
Finalizing Key 650
Finalizing Key 649
Finalizing Key 647
Finalizing Key 646
Finalizing Key 644
Finalizing Key 643
Finalizing Key 641
Finalizing Key 640
Finalizing Key 638
Finalizing Key 637
Finalizing Key 635
Finalizing Key 634
Finalizing Key 632
Finalizing Key 631
Finalizing Key 629
Finalizing Key 628
Finalizing Key 626
Finalizing Key 625
Finalizing Key 623
Finalizing Key 622
Finalizing Key 620
Finalizing Key 619
Finalizing Key 617
Finalizing Key 616
Finalizing Key 614
Finalizing Key 613
Finalizing Key 611
Finalizing Key 610
Finalizing Key 608
Finalizing Key 607
Finalizing Key 605
Finalizing Key 604
Finalizing Key 602
Finalizing Key 601
Finalizing Key 599
Finalizing Key 598
Finalizing Key 596
Finalizing Key 595
Finalizing Key 593
Finalizing Key 592
Finalizing Key 590
Finalizing Key 589
Finalizing Key 587
Finalizing Key 586
Finalizing Key 584
Finalizing Key 583
Finalizing Key 581
Finalizing Key 580
Finalizing Key 578
Finalizing Key 577
Finalizing Key 575
Finalizing Key 574
Finalizing Key 572
Finalizing Key 571
Finalizing Key 569
Finalizing Key 568
Finalizing Key 566
Finalizing Key 565
Finalizing Key 563
Finalizing Key 562
Finalizing Key 560
Finalizing Key 559
Finalizing Key 557
Finalizing Key 556
Finalizing Key 554
Finalizing Key 553
Finalizing Key 551
Finalizing Key 550
Finalizing Key 548
Finalizing Key 547
Finalizing Key 545
Finalizing Key 544
Finalizing Key 542
Finalizing Key 541
Finalizing Key 539
Finalizing Key 538
Finalizing Key 536
Finalizing Key 535
Finalizing Key 533
Finalizing Key 532
Finalizing Key 530
Finalizing Key 529
Finalizing Key 527
Finalizing Key 526
Finalizing Key 524
Finalizing Key 523
Finalizing Key 521
Finalizing Key 520
Finalizing Key 518
Finalizing Key 517
Finalizing Key 515
Finalizing Key 514
Finalizing Key 512
Finalizing Key 511
Finalizing Key 509
Finalizing Key 508
Finalizing Key 506
Finalizing Key 505
Finalizing Key 503
Finalizing Key 502
Finalizing Key 500
Finalizing Key 499
Finalizing Key 497
Finalizing Key 496
Finalizing Key 494
Finalizing Key 493
Finalizing Key 491
Finalizing Key 490
Finalizing Key 488
Finalizing Key 487
Finalizing Key 485
Finalizing Key 484
Finalizing Key 482
Finalizing Key 481
Finalizing Key 479
Finalizing Key 478
Finalizing Key 476
Finalizing Key 475
Finalizing Key 473
Finalizing Key 472
Finalizing Key 470
Finalizing Key 469
Finalizing Key 467
Finalizing Key 466
Finalizing Key 464
Finalizing Key 463
Finalizing Key 461
Finalizing Key 460
Finalizing Key 458
Finalizing Key 457
Finalizing Key 455
Finalizing Key 454
Finalizing Key 452
Finalizing Key 451
Finalizing Key 449
Finalizing Key 448
Finalizing Key 446
Finalizing Key 445
Finalizing Key 443
Finalizing Key 442
Finalizing Key 440
Finalizing Key 439
Finalizing Key 437
Finalizing Key 436
Finalizing Key 434
Finalizing Key 433
Finalizing Key 431
Finalizing Key 430
Finalizing Key 428
Finalizing Key 427
Finalizing Key 425
Finalizing Key 424
Finalizing Key 422
Finalizing Key 421
Finalizing Key 419
Finalizing Key 418
Finalizing Key 416
Finalizing Key 415
Finalizing Key 413
Finalizing Key 412
Finalizing Key 410
Finalizing Key 409
Finalizing Key 407
Finalizing Key 406
Finalizing Key 404
Finalizing Key 403
Finalizing Key 401
Finalizing Key 400
Finalizing Key 398
Finalizing Key 397
Finalizing Key 395
Finalizing Key 394
Finalizing Key 392
Finalizing Key 391
Finalizing Key 389
Finalizing Key 388
Finalizing Key 386
Finalizing Key 385
Finalizing Key 383
Finalizing Key 382
Finalizing Key 380
Finalizing Key 379
Finalizing Key 377
Finalizing Key 376
Finalizing Key 374
Finalizing Key 373
Finalizing Key 371
Finalizing Key 370
Finalizing Key 368
Finalizing Key 367
Finalizing Key 365
Finalizing Key 364
Finalizing Key 362
Finalizing Key 361
Finalizing Key 359
Finalizing Key 358
Finalizing Key 356
Finalizing Key 355
Finalizing Key 353
Finalizing Key 352
Finalizing Key 350
Finalizing Key 349
Finalizing Key 347
Finalizing Key 346
Finalizing Key 344
Finalizing Key 343
Finalizing Key 341
Finalizing Key 340
Finalizing Key 338
Finalizing Key 337
Finalizing Key 335
Finalizing Key 334
Finalizing Key 332
Finalizing Key 331
Finalizing Key 329
Finalizing Key 328
Finalizing Key 326
Finalizing Key 325
Finalizing Key 323
Finalizing Key 322
Finalizing Key 320
Finalizing Key 319
Finalizing Key 317
Finalizing Key 316
Finalizing Key 314
Finalizing Key 313
Finalizing Key 311
Finalizing Key 310
Finalizing Key 308
Finalizing Key 307
Finalizing Key 305
Finalizing Key 304
Finalizing Key 302
Finalizing Key 301
Finalizing Key 299
Finalizing Key 298
Finalizing Key 296
Finalizing Key 295
Finalizing Key 293
Finalizing Key 292
Finalizing Key 290
Finalizing Key 289
Finalizing Key 287
Finalizing Key 286
Finalizing Key 284
Finalizing Key 283
Finalizing Key 281
Finalizing Key 280
Finalizing Key 278
Finalizing Key 277
Finalizing Key 275
Finalizing Key 274
Finalizing Key 272
Finalizing Key 271
Finalizing Key 269
Finalizing Key 268
Finalizing Key 266
Finalizing Key 265
Finalizing Key 263
Finalizing Key 262
Finalizing Key 260
Finalizing Key 259
Finalizing Key 257
Finalizing Key 256
Finalizing Key 254
Finalizing Key 253
Finalizing Key 251
Finalizing Key 248
Finalizing Key 247
Finalizing Key 245
Finalizing Key 244
Finalizing Key 242
Finalizing Key 241
Finalizing Key 239
Finalizing Key 238
Finalizing Key 236
Finalizing Key 235
Finalizing Key 233
Finalizing Key 232
Finalizing Key 230
Finalizing Key 229
Finalizing Key 227
Finalizing Key 226
Finalizing Key 224
Finalizing Key 223
Finalizing Key 221
Finalizing Key 220
Finalizing Key 218
Finalizing Key 217
Finalizing Key 215
Finalizing Key 214
Finalizing Key 212
Finalizing Key 211
Finalizing Key 209
Finalizing Key 208
Finalizing Key 206
Finalizing Key 205
Finalizing Key 203
Finalizing Key 202
Finalizing Key 200
Finalizing Key 199
Finalizing Key 197
Finalizing Key 196
Finalizing Key 194
Finalizing Key 193
Finalizing Key 191
Finalizing Key 190
Finalizing Key 188
Finalizing Key 187
Finalizing Key 185
Finalizing Key 184
Finalizing Key 182
Finalizing Key 181
Finalizing Key 179
Finalizing Key 178
Finalizing Key 176
Finalizing Key 175
Finalizing Key 173
Finalizing Key 172
Finalizing Key 170
Finalizing Key 169
Finalizing Key 167
Finalizing Key 166
Finalizing Key 164
Finalizing Key 163
Finalizing Key 161
Finalizing Key 160
Finalizing Key 158
Finalizing Key 157
Finalizing Key 155
Finalizing Key 154
Finalizing Key 152
Finalizing Key 151
Finalizing Key 149
Finalizing Key 148
Finalizing Key 146
Finalizing Key 145
Finalizing Key 143
Finalizing Key 142
Finalizing Key 140
Finalizing Key 139
Finalizing Key 137
Finalizing Key 136
Finalizing Key 134
Finalizing Key 133
Finalizing Key 131
Finalizing Key 130
Finalizing Key 128
Finalizing Key 127
Finalizing Key 125
Finalizing Key 124
Finalizing Key 122
Finalizing Key 121
Finalizing Key 119
Finalizing Key 118
Finalizing Key 116
Finalizing Key 115
Finalizing Key 113
Finalizing Key 112
Finalizing Key 110
Finalizing Key 109
Finalizing Key 107
Finalizing Key 106
Finalizing Key 104
Finalizing Key 103
Finalizing Key 101
Finalizing Key 100
Finalizing Key 98
Finalizing Key 97
Finalizing Key 95
Finalizing Key 94
Finalizing Key 92
Finalizing Key 91
Finalizing Key 89
Finalizing Key 88
Finalizing Key 86
Finalizing Key 85
Finalizing Key 83
Finalizing Key 82
Finalizing Key 80
Finalizing Key 79
Finalizing Key 77
Finalizing Key 76
Finalizing Key 74
Finalizing Key 73
Finalizing Key 71
Finalizing Key 70
Finalizing Key 68
Finalizing Key 67
Finalizing Key 65
Finalizing Key 64
Finalizing Key 62
Finalizing Key 61
Finalizing Key 59
Finalizing Key 58
Finalizing Key 56
Finalizing Key 55
Finalizing Key 53
Finalizing Key 52
Finalizing Key 50
Finalizing Key 49
Finalizing Key 47
Finalizing Key 46
Finalizing Key 44
Finalizing Key 43
Finalizing Key 41
Finalizing Key 40
Finalizing Key 38
Finalizing Key 37
Finalizing Key 35
Finalizing Key 34
Finalizing Key 32
Finalizing Key 31
Finalizing Key 29
Finalizing Key 28
Finalizing Key 26
Finalizing Key 25
Finalizing Key 23
Finalizing Key 22
Finalizing Key 20
Finalizing Key 19
Finalizing Key 17
Finalizing Key 16
Finalizing Key 14
Finalizing Key 13
Finalizing Key 11
Finalizing Key 10
Finalizing Key 8
Finalizing Key 7
Finalizing Key 5
Finalizing Key 4
Finalizing Key 2
Finalizing Key 1








分享到:
评论
1 楼 assertivef 2014-03-12  
简单可测试的代码,言简意赅。

相关推荐

    弱类型语言允许将一块内存看做多种类型

    ### Java 中 WeakHashMap 的使用及特性 #### 五、WeakHashMap 概念 WeakHashMap 是 Java 集合框架中的一个接口实现,它提供了一种基于弱引用的哈希映射。这意味着它所持有的键是弱引用的,如果这些键在其他地方没有...

    java避免内存泄露

    - 示例:使用`WeakHashMap`来存储对象引用,以减少内存占用。 ```java Map, Value&gt; map = new WeakHashMap(); ``` 3. **合理使用静态变量**:尽量减少静态变量的使用,特别是不要让静态变量引用可变对象。 4. *...

    Java 基础核心总结 +经典算法大全.rar

    获取类中的变量(Field) 获取类中的方法(Method) 获取类的构造器(Constructor) 获取注解 通过反射调用方法反射的应用场景 Spring 的 IOC 容器反射 + 抽象工厂模式 JDBC 加载数据库驱动类反射的优势及缺陷 增加程序的...

    java 引用相关文档

    在这里,我们将详细讲解Java中的四种引用类型:强引用、软引用、弱引用和虚引用,以及它们在内存管理和垃圾收集中的作用。 1. **强引用(Strong Reference)** - 强引用是Java中最常见的引用类型,它是默认的引用...

    java 集合部分笔记

    在Java中,集合可以保存对象的引用,而数组则可以保存基本类型和对象。 - **集合和数组的区别**:集合只能保存对象,而数组可以保存基本类型和对象。集合提供了更灵活的数据管理方式,例如动态扩容和多样化数据结构...

    java 面试题 及解答 软件工程 全

    - `char` 类型可以存储一个中文汉字,因为 Java 使用 Unicode 编码,每个 `char` 占 16 位,足以表示一个中文字符。 5. **多线程实现与同步方式** - 多线程实现:可以通过继承 `Thread` 类或实现 `Runnable` 接口...

    Java面试题集锦

    例如,在提供的代码示例中,创建`ChildClass`实例之前会先调用`FatherClass`的构造器。因此,输出结果中首先看到的是`FatherClassCreate`,然后是`ChildClassCreate`。 ### 6. 内部类的实例化方式 内部类可以在...

    10分钟带你理解Java中的弱引用

    Java中的弱引用是一种特殊的引用类型,它在内存管理中扮演着重要的角色,特别是在避免内存泄漏和优化资源使用方面。本文将从三个方面详细解释弱引用:What(定义)、Why(使用原因)和How(使用方法)。 一、What...

    Java高级软件工程师考试范围

    在给出的示例中,正确答案是选项C和E。选项C定义了一个私有的非静态内部类,并且该类可以访问外部类的成员变量`d1`。选项E定义了一个抽象的非静态内部类,这是一个合法的定义。选项A、B和D都是错误的,因为它们试图...

    疯狂java讲义代码.rar

    虽然无法直接查看压缩包中的具体代码示例,但根据《疯狂Java讲义》的描述,我们可以预期这些主题都会有所涵盖。这本书通过实例讲解,帮助读者理解和掌握Java编程的核心概念和技术。通过深入学习这些知识点,你将具备...

    java常见面试题合集

    问题:Java中`char`类型占用多少个字节?为什么? 解答:在Java中,`char`类型占用2个字节。这是因为Java采用的是Unicode编码标准,每个`char`字符占用16位,即2个字节的空间。这样设计是为了支持全球范围内的各种...

    java 面试题集锦

    Java使用Unicode编码,一个`char`类型占用16位(2字节),足以容纳包括中文在内的大部分Unicode字符。 #### 4. 多线程的实现方法及其同步机制 - **多线程实现方法**: - 继承`Thread`类。 - 实现`Runnable`接口。...

    Java常见面试题集-

    这意味着在多线程环境中使用 `Vector` 更加安全,但性能相对较低。 - `Vector` 的某些方法(如 `add`, `set`)默认情况下会同步,而 `ArrayList` 的这些操作不进行同步。 - `Vector` 在需要扩展时,默认将容量增加...

    Java(J2SE、J2EE)学习小结

    Java是世界上最流行的...`wait()`使线程等待,释放锁,需在同步块中使用。 这只是Java J2SE和J2EE学习中的一小部分,要成为一名出色的Java开发者,还需要不断深入学习和实践,掌握更多的高级特性、框架和最佳实践。

    JAVA-提高反射效率

    - **缓存反射对象**:使用WeakHashMap或ConcurrentHashMap存储已获取的Class、Method、Constructor对象,避免重复查找。 - **使用invokeStatic()**:如果可以,优先考虑使用Method的invokeStatic(),以减少对象...

    java对象复制

    因此,函数内部对对象的任何修改会影响到原始对象,就像上面代码示例中`myMethod()`函数改变`Point`对象的`x`属性一样。 为了实现期望的不修改原始对象的效果,我们可以使用对象的克隆(Clone)机制。Java中的克隆...

    Java通过What、Why、How了解弱引用

    在Java编程语言中,弱引用是一种特殊的引用类型,它允许开发者在特定场景下更精细地控制对象的生命周期。本文将从What、Why、How三个方面详细解释弱引用的概念、使用原因及其实现方式。 What——什么是弱引用? 弱...

    Java中几个Reference常见的作用详解

    在Java编程语言中,`Reference`类是一组特殊类型的引用,它们允许程序员以更精细的方式控制对象的垃圾收集过程。这些引用类型主要包括`StrongReference`、`WeakReference`、`SoftReference`和`PhantomReference`,每...

Global site tag (gtag.js) - Google Analytics