JDK5特性简介

本文介绍了JDK5.0中的多项新特性,包括从XML中装载属性、并发集合的改进、增强的for循环、枚举类型、泛型的使用以及自动装箱拆箱等功能,并提供了具体的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

新项目要用JDK5.0,所以简单的在网上查了查,具体开发时,一点点研究
我会把心得狂帖
JDK5.0新特性简介
一 从xml中装载属性

 

格式:
<properties>
<comment>Hi</comment>
<entry key="id">1</entry>
<entry key="name">rocie</entry>
<entry key="email">rocie@sina.com</entry>
</properties>

 

 

通过以下方法加载:
Properties prop = new Properties();
FileInputStream fis =  new FileInputStream("d:/jdk5/properties/test.xml");
prop.loadFromXML(fis);
prop.list(System.out);
System.out.println("Name: " + prop.getProperty("name"));

 

 

将xml文件放在WEB-INF/classes/目录下,可以防止对路径的硬编码
InputStream is = PropertiesTest.class.getClassLoader().getResourceAsStream("rocie.xml");
Properties ps = new Properties();
ps.loadFromXML(is);
System.out.println("name:"+ps.getProperty("name"));        

 

 

通过以下方法保存:
Properties prop = new Properties();
prop.setProperty("id", "2");
prop.setProperty("name", "彭大双");
prop.setProperty("email", "pds@dotraining.com");
FileOutputStream fos = new FileOutputStream("d:/jdk5/properties/pds.xml");
prop.storeToXML(fos, "彭大双","gbk");
fos.close();

 

 

xml文件细节:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Hi</comment>
<entry key="id">1</entry>
<entry key="name">rocie</entry>
<entry key="email">rocie@sina.com</entry>
</properties>

 

 

DTD文件:
<?xml version="1.0" encoding="UTF-8"?>
<!-- DTD for properties -->
<!ELEMENT properties ( comment?, entry* ) >
<!ATTLIST properties version CDATA #FIXED "1.0">
<!ELEMENT comment (#PCDATA) >
<!ELEMENT entry (#PCDATA) >
<!ATTLIST entry key CDATA #REQUIRED>
注:?表示0或1;*表示0至无限;+表示至少一个

 

 

二 并发集合

问题:ConcurrentModificationExceptionfail-fast

 

当遍历一个Iterator时,如果该Iterator对应的List里的元素发生改变,则抛出这个异常。

解决方法:java.util.concurrent

 

用CopyOnWriteArrayList代替原来的ArrayList
copy-on-write:这意味着如果有大量的读(即 get() ) 和迭代,不必同步操作以照顾偶尔的写(即 add() )调用。对于新的 CopyOnWriteArrayList 和 CopyOnWriteArraySet 类,所有可变的(mutable)操作都首先取得后台数组的副本,对副本进行更改,然后替换副本。这种做法保证了在遍历自身更改的集合时,永远不会抛出 ConcurrentModificationException 。遍历集合会用原来的集合完成,而在以后的操作中使用更新后的集合。

 

 

用 for/in 在 Java 5.0 中增强循环
public static List<Integer> list = new ArrayList<Integer>();
      
       public static int[] is = new int[]{1,2,3,4,65,456};
      
       static
       {           
              for(int i=0;i<100;i++)
                     list.add(i);             
       }
      
       public static void main(String args[])
       {
              for(Integer i:list)
                     System.out.println(i);    
              System.out.println("-------other for each--------");
              for(int i:is)
                     System.out.println(i);
       }
枚举类型 enum
public class EnumTest {
       private Color color;
      
       public static enum Color {red,yellow,blue,green};    
      
       public Color getColor() {
              return color;
       }

 

 

       public void setColor(Color color) {
              this.color = color;
       }

 

 

       public static void main(String args[])
       {
              for(Color c:Color.values())
              {
                     System.out.println("name:"+c.name()+"ordinal:"+c.ordinal());
              }    
              EnumTest et = new EnumTest();
              et.setColor(Color.red);
              Color c = et.getColor();
              switch(c)
              {
              case red: System.out.println("red:"+c.name()+c.ordinal());
                                   break;
              case yellow: System.out.println("yellow"+c.name()+c.ordinal());
                                   break;
              default : System.out.println("over");
              }
             
       }

}

 

 

使用泛型 Generics types

定义简单的泛型

 

public static void main(String args[])
{
List<String> list = new ArrayList<String>();
             
       for(int i=0;i<100;i++)
              list.add(String.valueOf(i));
             
       String five = list.get(4);               

       }

使用通配符

 

public abstract class Shape {

 

public abstract void draw(Canvas c);

 

}

 

public class Circle extends Shape { private int x, y, radius;

 

public void draw(Canvas c) { ... }

 

}

 

public class Rectangle extends Shape { private int x, y, width, height;

 

public void draw(Canvas c) { ... }

 

}

 

 

 

public class Canvas {

 

    public void draw2All(List<? extends Shape> shapes)

 

    {

 

           for (Shape s: shapes)

 

                  s.draw(this);

 

    }

 

 

 

    public static void main(String args[])

 

    {

 

           List<Shape> list = new ArrayList<Shape>();

 

           list.add(new Circle());

 

           list.add(new Rectangle());

 

           new Canvas().draw2All(list);

 

    }

 

}

 

 

泛形方法

 

       public static <T> void fromArrayToCollection(T[] a, Collection<T> c)

 

       {

 

              for (T o : a)

 

                     c.add(o);

 

             

 

              for(T o:c)

 

                     System.out.println(o);

 

       }     

 

      

 

       public static void main(String args[])

 

       {

 

              String[] as = new String[]{"1s","2s","3s","4s"};

 

              List<String> list = new ArrayList<String>();

 

              fromArrayToCollection(as,list);            

 

             

 

              Integer[] is = new Integer[]{new Integer(1),new Integer(2),new Integer(3)};

 

              List<Integer> li = new ArrayList<Integer>();

 

              fromArrayToCollection(is,li);        

 

       }

 

 

自动拆/装箱 Autoboxing/Unboxing
public static void main(String args[])
{
              List<Integer> list = new ArrayList<Integer>();
              for(int i=0;i<100;i++)
                     list.add(i);             

       }

 

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值