java集合笔记

2017-05-06  本文已影响0人  渔樵店

认识Collection接口

Collection接口是单值保存的最大父类接口。Collection中的常用方法如下

  1. public boolean add(E e)//向集合中添加元素
  2. public boolean addAll()//向集合中追加一个集合
  3. public void clear()//清空集合
  4. public boolean contains(Object o)//判断是否包含指定的内容,需要equals支持
  5. public boolean isEmpty()//判断是否为空(不是null)
  6. public boolean remove(Object o)//删除对象,需要equals支持
  7. public Object[] toArray()//将集合变为数组保存
  8. public Iterator<E>iterator()//为Iterator实例化

add()和Iterator方法使用频率最高

List(80%)接口

List接口可以重复,Set接口不能重复,List按照放入的顺序排列,Set无序
和Collection相比增加的方法

List接口的两个实现类ArrayList(90%)和 Vector

ArrayList类

ArratList:异步,非线程安全 。 Vector:同步,线程安全
ArrayList的遍历方法,
因为List接口扩充了一个get()方法所有可以用第一种方法取得
for(int i=0;i<all.size();i++){ System.out.println(all.get(i)); }

equals()支持的案例,为什么需要equals()支持?应为对象的比较不是普通的地址比较,要想比较对象必须覆写equals()方法和hashCode()方法

代码:

Set接口

案例代码如下:
@Test public void test2() { Set<Book> all=new HashSet<Book>(); all.add(new Book("Java开发", 79.8)); all.add(new Book("Java开发", 79.8)); all.add(new Book("Jsp开发", 79.8)); all.add(new Book("Android开发", 89.8)); System.out.println(all); }
在Book类没有HashCode方法时,第一条记录和第二条记录都会被输出。
@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; }
当添加了hashCode()方法是会自动去除重复的数据

集合输出

  1. Iterator(95%)
    Iterator是一个接口,要想取得实例化对象只能通过Collection中的iterator()方法
    public Iterator<E> iterator();//获取Iterator接口的实例化对象
    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); } }
  2. ListIterator(0.05%)
    Iterator只能由前向后输出(99%都是从前往后输出),而ListIterator可以双向输出,在这个接口里有两个重要的方法,此接口是专门为List接口提供的输出方法
    public boolean hasPrevioud()

public E previoud();

-如果要实现由后向前输出,必须要先经过由前向后输出

  1. Enumeration(4.9%)

要想获得Enumeration对象的实例只能依靠Vectot子类

public Enumeration elements();//获得Enumeration对象的实例

public boolean hasMoreElements();

public E nextElement();

4.foreach(0.05%)

方便,但不适合新手用

Map接口

public V put(K key,V value)//向集合中保存数据

public V get(Object key)//根据key找到value

public Set<Map.Entry<K,V>> entrySet()//将Map集合转换为Set集合

public Set<key> keySet()//取出所有的key

Map有两个常用的子类:HashMap,HashTable

无序,如果key一样新的value会覆盖旧的value。HashMap是异步,非线程安全

get()方法,如果不存在则返回null不是””;

key和value都不能是null,同步线程安全

Map的Iterator输出

|- 利用Map接口的entrySet()方法将Map集合变为Set集合

|- 利用Set集合中的iter

iterator()方法将Set集合进行Iterator输出

|- 每一次Iterator循环取出的都是Map.Entry接口对象,利用次对象进行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的内容为空
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

示例代码如下:
@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")), "把数据保存到文件中"); } 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(); } }

集合工具类的使用

@Test public void test6(){ List<String> all=new ArrayList<String>(); Collections.addAll(all, "a","b","c");//实现集合的追加 System.out.println(all); Collections.reverse(all);//实现集合的反转 System.out.println(all); }

上一篇 下一篇

猜你喜欢

热点阅读