Java容器

一、List接口

ArrayList,对象加入之后大都是为了取出,而不会长做删除或插入的动作,则使用ArrayList效率会更加好,但是经常在容器里面做删除添加动作,则使用LinkList会更加好(该类是利用链表实现的),故增加了象addFirst()、addLast()、getFirst()、getLast()、removeFirst()、removeFast()等,这样适合实现堆栈和队列。

二、Set接口

List容器中允许重复的对象,但是Set容器中的对象都是唯一的,介入Set 容器必须重新定义equals(),hasCode()方法,Set容器有自己一套排序规则,其规则是根据哈希法,所以加入HashSet容器的对象还必须重新定义hasCode()方法.

Set<String> set=new HashSet<String>();

Iterator iterator=set.iterator();

while(iterator.hasNext()){    iterator.next()   };

也可以利用加强型进行访问元素:for(String name : set){  System.out.println(name);  }

如果想要以加入的顺序显示,则可以用LinkedHashSet,此类是HashSet的子类,它在内部类实现时使用哈希玛进行排序但是在迭代的行为上像LinkList那样.

TreeSet实现了Set接口和SortSet接口,SortSet提供的相关方法让您有序的去处对应位置的对象,像first()、last()等方法,TreeSet是J2SE中唯一实现了SortSet接口的类,它使用了红黑树结构来加入对象将进行排序。如果对对象有自己的一套排序,要定义一个实现java.util,Comparator接口的对象,要实现compare()方法.

三、Map接口

Map接口的对象会将键(Key)映射至值(Value),值指的是要存入Map容器的对象,其对象不能有重复的键,Map拥有

自己的排序机制。

HashMap实现Map接口,在内部实现使用了哈希法,如果在迭代的时候想要以添加的顺序

来排序,则可以用HashMap子类LinkedHashMap。

其访问对象的代码:

Map<String,String> map=new HashMap<String,String>();

for(String value :map.values){

System.out.println(value);

}

TreeMap实现Map接口和SortMap接口,SortMap提供相关的方法让你有序地取出对应位置的对象,像firstKey(),lastKey()等方法,TreeMap是唯一实现SortedMap接口的类,它使用了红黑树结构来对加入的对象进行排序。

四、hashCode的作用

非常重要





 

   小心内存泄露问题

用容器的时候为什么要覆盖equals()和hasCode()方法?上面是解释的原因

什么时候需要覆盖hashCode方法?

下面用例子进行证明:

Reflectpoint.java代码   收藏代码
  1. package cn.itcast.day1;  
  2.   
  3. import java.util.Date;  
  4.   
  5. public class ReflectPoint {  
  6.     private Date birthday = new Date();  
  7.       
  8.     private int x;  
  9.     public int y;  
  10.     public String str1 = "ball";  
  11.     public String str2 = "basketball";  
  12.     public String str3 = "itcast";  
  13.       
  14.     public ReflectPoint(int x, int y) {  
  15.         super();  
  16.         this.x = x;  
  17.         this.y = y;  
  18.     }  
  19.       
  20.       
  21.     @Override  
  22.     public int hashCode() {  
  23.         final int prime = 31;  
  24.         int result = 1;  
  25.         result = prime * result + x;  
  26.         result = prime * result + y;  
  27.         return result;  
  28.     }  
  29.   
  30.   
  31.     @Override  
  32.     public boolean equals(Object obj) {  
  33.         if (this == obj)  
  34.             return true;  
  35.         if (obj == null)  
  36.             return false;  
  37.         if (getClass() != obj.getClass())  
  38.             return false;  
  39.         final ReflectPoint other = (ReflectPoint) obj;  
  40.         if (x != other.x)  
  41.             return false;  
  42.         if (y != other.y)  
  43.             return false;  
  44.         return true;  
  45.     }  
  46.   
  47.   
  48.     @Override  
  49.     public String toString(){  
  50.         return str1 + ":" + str2 + ":" + str3;  
  51.     }  
  52.   
  53.   
  54.     public int getX() {  
  55.         return x;  
  56.     }  
  57.   
  58.   
  59.     public void setX(int x) {  
  60.         this.x = x;  
  61.     }  
  62.   
  63.   
  64.     public int getY() {  
  65.         return y;  
  66.     }  
  67.   
  68.   
  69.     public void setY(int y) {  
  70.         this.y = y;  
  71.     }  
  72.   
  73.   
  74.     public Date getBirthday() {  
  75.         return birthday;  
  76.     }  
  77.   
  78.   
  79.     public void setBirthday(Date birthday) {  
  80.         this.birthday = birthday;  
  81.     }  
  82.       
  83. }  
 
Reflecttest2.java代码   收藏代码
  1. package cn.itcast.day1;  
  2.   
  3. import java.io.FileInputStream;  
  4. import java.io.InputStream;  
  5. import java.util.ArrayList;  
  6. import java.util.Collection;  
  7. import java.util.HashMap;  
  8. import java.util.HashSet;  
  9. import java.util.Map;  
  10. import java.util.Properties;  
  11.   
  12. public class ReflectTest2 {  
  13.   
  14.     /**  
  15.      * @param args  
  16.      */  
  17.     public static void main(String[] args) throws Exception{  
  18.         // TODO Auto-generated method stub  
  19.         /*getRealPath();//金山词霸/内部  
  20.         一定要记住用完整的路径,但完整的路径不是硬编码,而是运算出来的。*/  
  21.         //InputStream ips = new FileInputStream("config.properties");  
  22.           
  23.         //InputStream ips = ReflectTest2.class.getClassLoader().getResourceAsStream("cn/itcast/day1/config.properties");  
  24.         //InputStream ips = ReflectTest2.class.getResourceAsStream("resources/config.properties");  
  25.     /*  InputStream ips = ReflectTest2.class.getResourceAsStream("/cn/itcast/day1/resources/config.properties");  
  26.   
  27.         Properties props = new Properties();  
  28.         props.load(ips);  
  29.         ips.close();  
  30.         String className = props.getProperty("className");  
  31.         Collection collections = (Collection)Class.forName(className).newInstance();  
  32.         */  
  33.         Collection collections = new HashSet();  
  34.         ReflectPoint pt1 = new ReflectPoint(3,3);  
  35.         ReflectPoint pt2 = new ReflectPoint(5,5);  
  36.         ReflectPoint pt3 = new ReflectPoint(3,3);     
  37.   
  38.         collections.add(pt1);  
  39.         collections.add(pt2);  
  40.         collections.add(pt3);  
  41.         collections.add(pt1);  
  42.       
  43.           
  44.         //pt1.y = 7;          
  45.         //collections.remove(pt1);  
  46.           
  47.         System.out.println(collections.size());  
  48.     }  
  49.   
  50. }  

 也就是说如果突然修改对象hashCode的值pt1.y=7,此容器想要移出这个对象,这个是办不到的,因此导致了内存的泄露

六、如何利用反射读取配置文件?用类加载器的方式管理资源和配置文件

InputStream ips = ReflectTest2.class.getClassLoader().getResourceAsStream("cn/itcast/day1/config.properties");

InputStream ips = ReflectTest2.class.getResourceAsStream("resources/config.properties")这个是第二种方式寻找配置文件.此文件假设存放在cn/itcast/day1/resources/config.properties

cn/itcast/day1/config.properties这个是所在的包中的文件路径,ReflectTest2类是在本类中找到装载器并加载所在的文件,这里注意类加载器再在class文件是从自己建的项目的路径开始加载,所以在寻找配置文件的时候一定要注意要加上所放配置文件的相对路径。所以在找文件的时候是在calsspath路径下找此文件。

Properties props = new Properties();
        props.load(ips);
        ips.close();
        String className = props.getProperty("className");
        Collection collections = (Collection)Class.forName(className).newInstance();

这样就能够根据className字符串动态的构建对象.

读取的文件格式为:className=java.util.ArrayList

具体源码请您参见张孝祥的源代码高级编程

还有疑问就是配置文件放在哪个路径下?

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值