認(rèn)識(shí)Collection接口
Collection接口是單值保存的最大父類(lèi)接口湘纵。Collection中的常用方法如下
- public boolean add(E e)//向集合中添加元素
- public boolean addAll()//向集合中追加一個(gè)集合
- public void clear()//清空集合
- public boolean contains(Object o)//判斷是否包含指定的內(nèi)容,需要equals支持
- public boolean isEmpty()//判斷是否為空(不是null)
- public boolean remove(Object o)//刪除對(duì)象,需要equals支持
- public Object[] toArray()//將集合變?yōu)閿?shù)組保存
- public Iterator<E>iterator()//為Iterator實(shí)例化
add()和Iterator方法使用頻率最高
List(80%)接口
List接口可以重復(fù),Set接口不能重復(fù)及舍,List按照放入的順序排列肉瓦,Set無(wú)序
和Collection相比增加的方法
- public E get(int index)//取得索引編號(hào)的內(nèi)容遭京,從0開(kāi)始
- public E set(int index)//修改指定索引編號(hào)的內(nèi)容
- public ListIterator<E> listIterator()//為L(zhǎng)istIterator接口實(shí)例化
List接口的兩個(gè)實(shí)現(xiàn)類(lèi)ArrayList(90%)和 Vector
ArrayList類(lèi)
ArratList:異步,非線(xiàn)程安全 泞莉。 Vector:同步,線(xiàn)程安全
ArrayList的遍歷方法船殉,
因?yàn)長(zhǎng)ist接口擴(kuò)充了一個(gè)get()方法所有可以用第一種方法取得
for(int i=0;i<all.size();i++){ System.out.println(all.get(i)); }
equals()支持的案例鲫趁,為什么需要equals()支持?應(yīng)為對(duì)象的比較不是普通的地址比較利虫,要想比較對(duì)象必須覆寫(xiě)equals()方法和hashCode()方法
代碼:
- 定義一個(gè)Book類(lèi)
class Book{ private String title; private double price; public Book(String title, double price) { super(); this.title = title; this.price = price; } @Override public String toString() { return "Book [title=" + title + ", price=" + price + "]\n"; } }
- 定義一個(gè)測(cè)試類(lèi)
@Test public void test() { List<Book> all=new ArrayList<Book>(); all.add(new Book("Java開(kāi)發(fā)", 79.8)); all.add(new Book("Jsp開(kāi)發(fā)", 69.8)); all.add(new Book("oracle開(kāi)發(fā)", 89.8)); all.remove(new Book("oracle開(kāi)發(fā)", 89.8)); System.out.println(all); }
執(zhí)行后還是三本書(shū)挨厚,因?yàn)槲锤矊?xiě)equals()方法所以當(dāng)所有的屬性值相等時(shí)也無(wú)法判斷兩個(gè)是是否相同.
Set接口
- Set接口只是簡(jiǎn)單繼承了Connection接口堡僻,沒(méi)有g(shù)et()方法,沒(méi)有重復(fù)元素
Set的無(wú)重復(fù)元素性對(duì)于一般的數(shù)字字符串都有效,但如果Set中保存的是一樣的對(duì)象疫剃,必須在類(lèi)中添加hashCode()方法钉疫。 - Set接口的常用子類(lèi)HashSet和TreeSet
HashSet無(wú)序排列;TreeSet內(nèi)容自動(dòng)排序(對(duì)象的比排序要用到比較器)巢价,TreeSet是依靠comparable接口中的compareTo()方法牲阁,在比較方法里面需要將這個(gè)類(lèi)的所有屬性都參與比較,很煩,所以一般不用.而且comparable只能負(fù)責(zé)TreeSet重復(fù)元素的判斷壤躲,不會(huì)作用與HashSet,如果要判斷重復(fù)元素只能依靠object的方法。 - public int hasnCode();//取得哈希嗎
|- 先判斷哈希嗎是否相同碉克,依靠哈希嗎取得對(duì)象的內(nèi)容
public boolean equals(Object obj)
|- 將對(duì)象的屬性一次進(jìn)行比較
案例代碼如下:
@Test public void test2() { Set<Book> all=new HashSet<Book>(); all.add(new Book("Java開(kāi)發(fā)", 79.8)); all.add(new Book("Java開(kāi)發(fā)", 79.8)); all.add(new Book("Jsp開(kāi)發(fā)", 79.8)); all.add(new Book("Android開(kāi)發(fā)", 89.8)); System.out.println(all); }
在Book類(lèi)沒(méi)有HashCode方法時(shí),第一條記錄和第二條記錄都會(huì)被輸出漏麦。
@Override public int hashCode() { final int prime = 31; int result = 1; long temp; temp = Double.doubleToLongBits(price); result = prime * result + (int) (temp ^ (temp >>> 32)); result = prime * result + ((title == null) ? 0 : title.hashCode()); return result; }
當(dāng)添加了hashCode()方法是會(huì)自動(dòng)去除重復(fù)的數(shù)據(jù)
集合輸出
- Iterator(95%)
Iterator是一個(gè)接口,要想取得實(shí)例化對(duì)象只能通過(guò)Collection中的iterator()方法
public Iterator<E> iterator();//獲取Iterator接口的實(shí)例化對(duì)象
public boolen hasNext();
public E next();
代碼:
public void test3() { List<String> all=new ArrayList<String>(); all.add("pxc1"); all.add("pxc2"); all.add("pxc3"); Iterator<String> iter=all.iterator(); while(iter.hasNext()){ String str=iter.next(); System.out.println(str); } }
- ListIterator(0.05%)
Iterator只能由前向后輸出(99%都是從前往后輸出)撕贞,而ListIterator可以雙向輸出,在這個(gè)接口里有兩個(gè)重要的方法,此接口是專(zhuān)門(mén)為L(zhǎng)ist接口提供的輸出方法
public boolean hasPrevioud()
public E previoud();
-如果要實(shí)現(xiàn)由后向前輸出麻掸,必須要先經(jīng)過(guò)由前向后輸出
- Enumeration(4.9%)
要想獲得Enumeration對(duì)象的實(shí)例只能依靠Vectot子類(lèi)
public Enumeration elements();//獲得Enumeration對(duì)象的實(shí)例
public boolean hasMoreElements();
public E nextElement();
4.foreach(0.05%)
方便,但不適合新手用
Map接口
public V put(K key,V value)//向集合中保存數(shù)據(jù)
public V get(Object key)//根據(jù)key找到value
public Set<Map.Entry<K,V>> entrySet()//將Map集合轉(zhuǎn)換為Set集合
public Set<key> keySet()//取出所有的key
Map有兩個(gè)常用的子類(lèi):HashMap脊奋,HashTable
-
HashMap
無(wú)序,如果key一樣新的value會(huì)覆蓋舊的value诚隙。HashMap是異步,非線(xiàn)程安全
get()方法久又,如果不存在則返回null不是””;
-
HashTable
key和value都不能是null巫延,同步線(xiàn)程安全
Map的Iterator輸出
遺憾:Map未提供直接獲得Iterator的方法。
每當(dāng)用戶(hù)使用put()方法向Map集合中添加數(shù)據(jù)時(shí)地消,實(shí)際上所有的數(shù)據(jù)都會(huì)被自動(dòng)的封裝為Map.Entry接口炉峰,這是一個(gè)靜態(tài)接口。其中有兩個(gè)方法getKey()和getValue()
Map中有一個(gè)得到所有Map.Entry對(duì)象的方法public Set<Map.Entry<K,Y>> entrySet()脉执。所以Map集合的輸出步驟如下
|- 利用Map接口的entrySet()方法將Map集合變?yōu)镾et集合
|- 利用Set集合中的iter
iterator()方法將Set集合進(jìn)行Iterator輸出
|- 每一次Iterator循環(huán)取出的都是Map.Entry接口對(duì)象疼阔,利用次對(duì)象進(jìn)行key和value的取出。
`@Test
public void test4(){
Map<String,Integer> map=new HashMap<String, Integer>();
map.put("yi", 1);
map.put("er", 2);
map.put("san", 3);
map.put(null, 0);//key的內(nèi)容為空
Set<Map.Entry<String, Integer>> set=map.entrySet();
Iterator<Map.Entry<String, Integer>> iter=set.iterator();
while(iter.hasNext()){
System.out.println(iter.next().getKey()+":"+iter.next().getValue());
}
}
Properties
-
Properties是HashTable類(lèi)的子類(lèi),主要是屬性的操作婆廊,只能保存String迅细,此類(lèi)的主要操作方法如下
public Object setProperty(String key,String value)
public String getProperty(String key)//取得屬性,key不存在返回null
public String getProperty(String key淘邻,String default)//取得屬性茵典,key不存在返回默認(rèn)值
properties的輸出操作:public void store(OutPutStream out,String comments)//傳入輸出流和注釋信息
properties的讀取操作:public void load(InputStram in); 然后使用getProperty()方法讀取數(shù)據(jù)。對(duì)于屬性文件還可以使用 ResourceBundle
示例代碼如下:
@Test public void test5(){ Properties pro=new Properties(); /*pro.setProperty("pxc","123"); pro.setProperty("pxc2","456"); try { pro.store(new FileOutputStream(new File("g:"+File.separator+"area.properties")), "把數(shù)據(jù)保存到文件中"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ try { pro.load(new FileInputStream(new File("g:"+File.separator+"area.properties"))); System.out.println(pro.getProperty("pxc")); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
集合工具類(lèi)的使用
@Test public void test6(){ List<String> all=new ArrayList<String>(); Collections.addAll(all, "a","b","c");//實(shí)現(xiàn)集合的追加 System.out.println(all); Collections.reverse(all);//實(shí)現(xiàn)集合的反轉(zhuǎn) System.out.println(all); }