Demonstrates the use of final collections : 集合数据结构 « 集合数据结构 « Java

En
Java
1. 图形用户界面
2. 三维图形动画
3. 高级图形
4. 蚂蚁编译
5. Apache类库
6. 统计图
7. 
8. 集合数据结构
9. 数据类型
10. 数据库JDBC
11. 设计模式
12. 开发相关类
13. EJB3
14. 电子邮件
15. 事件
16. 文件输入输出
17. 游戏
18. 泛型
19. GWT
20. Hibernate
21. 本地化
22. J2EE平台
23. 基于J2ME
24. JDK-6
25. JNDI的LDAP
26. JPA
27. JSP技术
28. JSTL
29. 语言基础知识
30. 网络协议
31. PDF格式RTF格式
32. 映射
33. 常规表达式
34. 脚本
35. 安全
36. Servlets
37. Spring
38. Swing组件
39. 图形用户界面
40. SWT-JFace-Eclipse
41. 线程
42. 应用程序
43. Velocity
44. Web服务SOA
45. 可扩展标记语言
Java 教程
Java » 集合数据结构 » 集合数据结构屏幕截图 
Demonstrates the use of final collections
  
/*
 *     file: FinalCollections.java
 *  package: oreilly.hcj.finalstory
 *
 * This software is granted under the terms of the Common Public License,
 * CPL, which may be found at the following URL:
 * http://www-124.ibm.com/developerworks/oss/CPLv1.0.htm
 *
 * Copyright(c) 2003-2005 by the authors indicated in the @author tags.
 * All Rights are Reserved by the various authors.
 *
 ########## DO NOT EDIT ABOVE THIS LINE ########## */

import java.awt.Color;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

/**
 * Demonstrates the use of final collections.
 
 @author <a href=mailto:kraythe@arcor.de>Robert Simmons jr. (kraythe)</a>
 @version $Revision: 1.3 $
 */
public class FinalCollections {
  /**
   * Main method for demonstration.
   
   @param args
   *          Command line arguments.
   */
  public static final void main(final String[] args) {
    Rainbow.someMethod();
    System.out.println("------------------");
    try {
      RainbowBetter.someMethod();
    catch (final UnsupportedOperationException ex) {
      ex.printStackTrace(System.out);
      System.out.println("Told you it would exception =)");
    }
  }

  /**
   * A class that demonstrates the rainbow.
   
   @author <a href=mailto:kraythe@arcor.de>Robert Simmons jr. (kraythe)</a>
   @version $Revision: 1.3 $
   */
  public static class Rainbow {
    /** The set of valid colors of the rainbow. */
    public static final Set VALID_COLORS;

    static {
      VALID_COLORS = new HashSet();
      VALID_COLORS.add(Color.red);
      VALID_COLORS.add(Color.orange);
      VALID_COLORS.add(Color.yellow);
      VALID_COLORS.add(Color.green);
      VALID_COLORS.add(Color.blue);
      VALID_COLORS.add(Color.decode("#4B0082"))// indigo
      VALID_COLORS.add(Color.decode("#8A2BE2"))// violet
    }

    /**
     * A demo method.
     */
    public static final void someMethod() {
      Set colors = Rainbow.VALID_COLORS;
      colors.add(Color.black)// <= logic error but allowed by compiler
      System.out.println(colors);
    }
  }

  /**
   * A better version of the rainbow class using an unmodifiable set.
   
   @author <a href=mailto:kraythe@arcor.de>Robert Simmons jr. (kraythe)</a>
   @version $Revision: 1.3 $
   */
  public static class RainbowBetter {
    /** The valid colors of the rainbow. */
    public static final Set VALID_COLORS;

    static {
      Set temp = new HashSet();
      temp.add(Color.red);
      temp.add(Color.orange);
      temp.add(Color.yellow);
      temp.add(Color.green);
      temp.add(Color.blue);
      temp.add(Color.decode("#4B0082"))// indigo
      temp.add(Color.decode("#8A2BE2"))// violet
      VALID_COLORS = Collections.unmodifiableSet(temp);
    }

    /**
     * Some demo method.
     */
    public static final void someMethod() {
      Set colors = RainbowBetter.VALID_COLORS;
      colors.add(Color.black)// <= exception here
      System.out.println(colors);
    }
  }
}

/* ########## End of File ########## */

   
    
  
Related examples in the same category
1. Collections.min与比较
2. 最低和最高数字数组
3. Shuffling the Elements of a List or Array: use Collections.shuffle() to randomly reorder the elements in a list
4. Shuffle数组
5. 创建一个空集合对象
6. Collections.reverse
7. Use Collections.sort to sort custom class and user defined Comparator
8. Collections.shuffle名单
9. Collections.fill
10. Use Collections.shuffle to shuffle listUse Collections.shuffle to shuffle list
11. Making a Collection Read-Only
12. Shuffle的通用列表
13. Create List containing n Copies of Specified Object Example
14. 创建并展示一个不可改变的集合。
15. 排序和搜索LinkedList 。
16. Finding an Element in a Sorted List
17. 使用反向,旋转,和Shuffle。
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.