Java中WeakHashMap类型使用示例_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Java中WeakHashMap类型使用示例

Java中WeakHashMap类型使用示例

 2011/9/14 8:38:34  YuHuang.Neil  http://yuhuang-neil.iteye.com  我要评论(0)
  • 摘要:在Java集合中有一种特殊的Map类型——WeakHashMap,在这种Map中存放了键对象的弱引用,当一个键对象被垃圾回收器回收时,那么相应的值对象的引用会从Map中删除。WeakHashMap能够节省存储空间,可用来缓存那些非必须存在的数据。下面程序段测试了垃圾回收器对WeakHashMap中值对象的回收过程,其中能够被3整除的键不会被垃圾回收器回收是由于其引用是强引用。importjava.util.*;importjava.lang.ref.*;classKey{Stringid
  • 标签:has 使用 Map Hash Java
在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








发表评论
用户名: 匿名