Java 进阶知识(六)

2 阅读6分钟
package collection;

import java.util.ArrayList;
import java.util.Collection;

/**
 * java.util.Collection
 * 集合
 * 集合与数组相似,可以保存一组元素,并且提供
 * 了操作集合元素的相关方法,使用便捷。
 * Collection接口下面有两个常见的子接口:
 * java.util.List:可重复集合,并且有序,可以
 * 通过下标操作元素。
 * 
 * java.util.Set:不可重复集合。
 * 元素是否重复是依靠元素自身equals比较进行
 * 判定的.
 * @author ta
 *
 */
public class CollectionDemo1 {
	public static void main(String[] args) {
		Collection c = new ArrayList();
		/*
		 * boolean add(E e)
		 * 向当前集合中添加给定元素,当该元素
		 * 成功添加则返回true。
		 */
		c.add("one");
		c.add("two");
		c.add("three");
		c.add("four");
		c.add("five");
		
		System.out.println(c);
		/*
		 * int size()
		 * 返回当前集合的元素个数
		 */
		int size = c.size();
		System.out.println("size:"+size);
		/*
		 * boolean isEmpty()
		 * 判断当前集合是否为空集(不含有任何
		 * 元素)
		 */
		boolean isEmpty = c.isEmpty();
		System.out.println("isEmpty:"+isEmpty);
		/*
		 * void clear()
		 * 清空当前集合
		 */
		c.clear();
		System.out.println("集合已清空");
		
		System.out.println(c);
		System.out.println("size:"+c.size());
		System.out.println("isEmpty:"+c.isEmpty());
	}
}
package collection;
/**
 * 使用当前类实例作为集合元素,测试集合相关
 * 操作。
 * @author ta
 *
 */
public class Point {
	private int x;
	private int y;
	public Point(int x, int y) {
		super();
		this.x = x;
		this.y = y;
	}
	public int getX() {
		return x;
	}
	public void setX(int x) {
		this.x = x;
	}
	public int getY() {
		return y;
	}
	public void setY(int y) {
		this.y = y;
	}
	public String toString() {
		return "("+x+","+y+")";
	}
	public boolean equals(Object obj) {
		if(obj == null) {
			return false;
		}
		if(obj == this) {
			return true;
		}
		if(obj instanceof Point) {
			Point p = (Point)obj;
			return this.x==p.x&&this.y==p.y;
		}
		return false;
	}
}
package collection;

import java.util.ArrayList;
import java.util.Collection;

/**
 * 集合提供了判断是否包含给定元素的方法:
 * boolean contains(E e)
 * 
 * @author ta
 *
 */
public class ContainsDemo {
	public static void main(String[] args) {
		Collection c = new ArrayList();
		c.add(new Point(1,2));
		c.add(new Point(3,4));
		c.add(new Point(5,6));
		c.add(new Point(7,8));
		c.add(new Point(9,0));
		/*
		 * 集合的toString方法会将每个元素
		 * 的toString体现出来。
		 */
		System.out.println(c);
		
		Point p = new Point(1,2);
//		c.add(p);
		/*
		 * contains方法是依靠元素自身equals比较
		 * 的结果判别集合是否包含该元素
		 */
		boolean contains = c.contains(p);
		System.out.println("包含:"+contains);
	}
}
package collection;

import java.util.ArrayList;
import java.util.Collection;

/**
 * 集合只能存放引用类型元素,并且存放的也是元
 * 素的引用(地址)
 * @author ta
 *
 */
public class CollectionDemo2 {
	public static void main(String[] args) {
		Collection c = new ArrayList();
		Point p = new Point(1,2);
		
		c.add(p);
		System.out.println("c:"+c);
		System.out.println("p:"+p);
		
		p.setX(2);
		System.out.println("c:"+c);//[(2,2)]
		System.out.println("p:"+p);//(2,2)
	}
}
package collection;

import java.util.ArrayList;
import java.util.Collection;

public class Test {
	public static void main(String[] args) {
		int a = 1;
		String b = "hello";
		Point p = new Point(1,2);
		Collection c = new ArrayList();
		c.add(p);
		test(a, b, p, c);
		System.out.println("a:"+a);//?
		System.out.println("b:"+b);//?
		System.out.println("p:"+p);//?
		System.out.println("c:"+c);//?
	}
	
	public static void test(int a,String b,Point p,Collection c){
		a = 2;
		b = b+"world";
		p.setX(3);
		p = new Point(5,6);
		c.clear();
		c.add(p);
		p.setY(7);
		c = new ArrayList();
		c.add(p);
	}
}
package collection;

import java.util.ArrayList;
import java.util.Collection;

/**
 * 删除集合元素
 * @author ta
 */
public class RemoveDemo {
	public static void main(String[] args) {
		Collection c = new ArrayList();
		c.add(new Point(1,2));
		c.add(new Point(3,4));
		c.add(new Point(5,6));
		c.add(new Point(7,8));
		c.add(new Point(9,0));
		c.add(new Point(1,2));
		System.out.println(c);
		
		Point p = new Point(1,2);
		/*
		 * 删除元素也是依靠元素equals比较判定
		 */
		c.remove(p);
		System.out.println(c);
	}
}
package collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;

/**
 * 集合操作
 * @author ta
 *
 */
public class CollectionDemo3 {
	public static void main(String[] args) {
		Collection c1 = new ArrayList();
		c1.add("java");
		c1.add("c");
		c1.add("c++");
		System.out.println("c1:"+c1);
		
		Collection c2 = new HashSet();
		c2.add("php");
		c2.add(".net");
		c2.add("java");
		System.out.println("c2:"+c2);
		/*
		 * boolean addAll(Collection c)
		 * 将给定集合中的所有元素添加到
		 * 当前集合中。
		 */
		c2.addAll(c1);
		System.out.println("c2:"+c2);
		
		Collection c3 = new ArrayList();
		c3.add("c");
		c3.add(".net");
		c3.add("android");
		System.out.println("c3:"+c3);
		/*
		 * boolean containsAll(Collection c)
		 * 判断当前集合是否包含给定集合中的
		 * 所有元素
		 */
		boolean ca = c2.containsAll(c3);
		System.out.println("全包含:"+ca);
		/*
		 * 删除当前集合与给定集合的共有元素
		 */
		c2.removeAll(c3);
		System.out.println(c2);
	}
}
package collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * 集合提供了同一的遍历元素方式:迭代器模式
 * 
 * 对应方法:
 * Iterator iterator()
 * 该方法可以获取一个用来遍历当前集合的迭代器
 * 实现类,通过它遍历元素
 * 
 * java.util.Iterator接口
 * 迭代器接口,规定了迭代器遍历集合的相关操作
 * 不同的集合都实现了一个用于遍历自身元素的
 * 迭代器实现类。
 * 
 * 迭代器遍历集合元素遵循的过程:问,取,删
 * 其中删除元素不是必要操作
 * 
 * @author ta
 *
 */
public class IteratorDemo {
	public static void main(String[] args) {
		Collection c = new ArrayList();
		c.add("one");
		c.add("#");
		c.add("two");
		c.add("#");
		c.add("three");
		c.add("#");
		c.add("four");
		c.add("#");
		c.add("five");
		System.out.println(c);	
		//获取迭代器
		Iterator it = c.iterator();
		/*
		 * boolean hasNext()
		 * 判断集合是否还有元素可以迭代
		 */
		while(it.hasNext()) {
			/*
			 * E next()
			 * 迭代集合下一个元素
			 */
			String o = (String)it.next();
			if("#".equals(o)) {
//				c.remove(o);
				/*
				 * 删除的是通过next方法取出
				 * 的元素
				 */
				it.remove();
			}
			System.out.println(o);
		}
	
		System.out.println(c);
	}
}
package collection;
/**
 * JDK5推出时,推出了一个新的特性:
 * 增强型for循环,也称为新循环,for each
 * 
 * 新循环不取代传统for循环的工作,它专门设计
 * 是用来遍历集合或数组的。
 * 
 * @author ta
 *
 */
public class NewForDemo1 {
	public static void main(String[] args) {
		String[] array = {"one","two","three","four"};
		
		for(int i=0;i<array.length;i++) {
			System.out.println(array[i]);
		}
		/*
		 * 新循环的语法也是编译器认可,而非虚拟机
		 * 认可。编译器会在编译源代码时将新循环
		 * 遍历数组改为传统for循环遍历的方式
		 */
		for(String str : array) {
			System.out.println(str);
		}
	}
}
package collection;

import java.util.ArrayList;
import java.util.Collection;

/**
 * 使用新循环遍历集合
 * @author ta
 *
 */
public class NewForDemo2 {
	public static void main(String[] args) {
		Collection c = new ArrayList();
		c.add("one");
		c.add("two");
		c.add("three");
		c.add("four");
		c.add("five");
		System.out.println(c);
		/*
		 * 新循环遍历集合会被编译器改为使用
		 * 迭代器遍历。所以在遍历的过程中是
		 * 不能通过集合的方法增删元素的。
		 */
		for(Object o : c) {
			String str = (String)o;
			System.out.println(str);
		}
	}
}
package collection;
/**
 * 泛型
 * 泛型是JDK5推出的特性, 也称为参数化类型
 * 它允许将一个类中属性的类型,方法参数的类型
 * 以及方法返回值类型等的定义权移交给使用者。
 * 这使得实际应用中使用这个类更加灵活便捷。
 * @author ta
 *
 */
public class Location<E> {
	private E x;
	private E y;
	public Location(E x, E y) {
		super();
		this.x = x;
		this.y = y;
	}
	public E getX() {
		return x;
	}
	public void setX(E x) {
		this.x = x;
	}
	public E getY() {
		return y;
	}
	public void setY(E y) {
		this.y = y;
	}
	
	@Override
	public String toString() {
		return "("+x+","+y+")";
	}
}
package collection;
/**
 * 测试泛型
 * @author ta
 */
public class TypeDemo {
	public static void main(String[] args) {
		Location<Integer> loc1 = new Location<Integer>(1,2);
		loc1.setX(2);
		int x1 = loc1.getX();
		System.out.println("loc1:"+loc1);
		System.out.println("x1:"+x1);
		Location<Double> loc2 = new Location<Double>(1.1,2.2);
		loc2.setX(2.2);
		double x2 = loc2.getX();
		System.out.println("loc2:"+loc2);
		System.out.println("x2:"+x2);
		Location<String> loc3 = new Location<String>("一","二 ");
		loc3.setX("三");
		String x3 = loc3.getX();
	}
}
package collection;
/**
 * 泛型是编译器认可,而非虚拟机。
 * 编译器会将泛型改为Object。所以泛型的实际
 * 类型就是Object
 * 在使用泛型时,编译器会辅助做两个操作
 * 1:对泛型设置值时,编译器会检查该值的类型
 *   是否与泛型一致,不一致则编译不通过。
 * 2:在获取泛型值时,编译器会添加向下造型的
 *   代码。  
 * @author ta
 *
 */
public class TypeDemo2 {
	public static void main(String[] args) {
		Location<Integer> loc1 = new Location<Integer>(1,2);
		/*
		 * 编译器会检查实际赋值是否符合泛型类型
		 * 要求,不符合编译不通过。
		 */
		loc1.setX(1);
//		loc1.setX("1");//编译不通过
		/*
		 * 编译器会在编译时补全向下造型的代码为:
		 * int x1 = (Integer)loc1.getX();
		 * 然后还会触发自动拆箱,改为
		 * int x1 = ((Integer)loc1.getX()).intValue();
		 */
		int x1 = loc1.getX();
		System.out.println("loc1:"+loc1);
		System.out.println("x1:"+x1);
		/*
		 * 泛型可以不指定,不指定则按照默认的
		 * Object看待
		 */
		Location loc2 = loc1;
		System.out.println("loc2:"+loc2);
		loc2.setX("三");
		System.out.println("loc2:"+loc2);
		
		//再次以loc1角度获取x
		//x1 = (Integer)loc1.getX();
		x1 = loc1.getX();//类造型异常
		System.out.println("x1:"+x1);
	}
}
package collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * 泛型在集合当中的应用---约束集合中的元素类型
 * @author ta
 *
 */
public class CollectionDemo4 {
	public static void main(String[] args) {
		Collection<String> c = new ArrayList<String>();
		/*
		 * 指定后add方法只能传入泛型要求的
		 * 元素
		 */
		c.add("one");
		c.add("two");
		c.add("three");
		c.add("four");
		System.out.println(c);
		/*
		 * 新循环可以直接用实际类型接收元素
		 */
		for(String str: c) {
			System.out.println(str);
		}
		/*
		 * 迭代器也支持泛型,指定的类型与
		 * 集合的泛型一致即可
		 */
		Iterator<String> it = c.iterator();
		while(it.hasNext()) {
			String str = it.next();
			System.out.println(str);
		}
	}
}