容器API
Collection接口
equals & hashCode
注意:重写equals方法必须重写hashCode方法,如果A.equals(B),那么A和B的hashCode码相等。
//姓名
public class Name /*implements Comparable*/{
private String firstName;
private String lastName;
public Name(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
@Override
public String toString() {
return firstName + " " + lastName;
}
@Override
public boolean equals(Object obj) {
if(obj instanceof Name) {
Name name = (Name) obj;
return (firstName.equals(name.firstName))
&& (lastName.equals(name.lastName));
}
return super.equals(obj);
}
@Override
public int hashCode() {
return firstName.hashCode(); //String类实现了hashCode()方法
}
/*
@Override
public int compareTo(Object o) {
Name name = (Name) o;
int last = lastName.compareTo(name.lastName);
return (last != 0 ? last : firstName.compareTo(name.firstName));
}*/
}
//容器基础
public class BaseContainer {
public static void main(String[] args) {
Collection c = new HashSet();
c.add("hello");
c.add(new Name("Li", "Jun"));
c.add(new Integer(100));
c.remove("hello");
c.remove(new Integer(100));
System.out.println(c.remove(new Name("Li", "Jun")));//false
System.out.println(c); //[Li Jun]
}
}
Iterator接口
//迭代器
public class IteratorTest {
public static void main(String[] args) {
Collection c = new HashSet();
c.add(new Name("Li", "Jun"));
c.add(new Name("Hao", "yun"));
c.add(new Name("Ai", "Ya"));
Iterator i = c.iterator();
while(i.hasNext()) {
//next()的返回值为Object类型,需转换为相应类型
Name name = (Name) i.next();
System.out.print(name.getFirstName() + " "); //Hao Li Ai
}
for(Iterator j = c.iterator(); j.hasNext();) {
Name n = (Name)j.next();
if(n.getFirstName().length() < 3) {
j.remove();
}
}
System.out.println(c); //[Hao yun]
}
}
Set接口
//Set接口
public class SetTest {
public static void main(String[] args) {
Set s = new HashSet();
s.add("hello");
s.add(new Name("Li", "Jun"));
s.add(new Integer(100));
s.add("hello"); //重复元素不会加入Set
s.add(new Name("Li", "Jun"));
System.out.println(s); //[hello, 100, Li Jun]
}
}
//方法测试
public class MethodInSet {
public static void main(String[] args) {
Set s1 = new HashSet();
Set s2 = new HashSet();
s1.add("a"); s1.add("b"); s1.add("c");
s2.add("d"); s2.add("a"); s2.add("b");
//Set和List容器类都具有Constructor(Collection c)
//构造方法用以初始化容器类
Set sn = new HashSet(s1);
sn.retainAll(s2); //求交集
Set su = new HashSet(s1);
su.addAll(s2); //将集合s2中的所有元素加入s1中
System.out.println(sn); //[b, a]
System.out.println(su); //[d, b, c, a]
}
}
List接口
//List接口中的方法
public class MethodInList {
public static void main(String[] args) {
List list = new LinkedList();
for(int i = 0; i <= 4; i++) {
list.add("G" + i);
}
System.out.println(list); //[G0, G1, G2, G3, G4]
list.add(1, "G100");
System.out.println(list); //[G0, G100, G1, G2, G3, G4]
list.set(1, "G20");
System.out.println(list); //[G0, G20, G1, G2, G3, G4]
System.out.print(list.get(1) + " ");
System.out.println(list.indexOf("G1")); //G20 2
list.remove(1);
System.out.println(list); //[G0, G1, G2, G3, G4]
}
}
List常用算法
//List中的常用算法
public class AlgorithmInList {
public static void main(String[] args) {
List list = new LinkedList();
for(int i = 0; i <= 5; i++) {
list.add("a" + i);
}
System.out.println(list); //[a0, a1, a2, a3, a4, a5]
Collections.shuffle(list); //随机排列
System.out.println(list); //[a0, a2, a5, a1, a4, a3]
Collections.reverse(list); //逆序
System.out.println(list); //[a3, a4, a1, a5, a2, a0]
Collections.sort(list); //排序
System.out.println(list); //[a0, a1, a2, a3, a4, a5]
System.out.println(Collections.binarySearch(list, "a3")); //二分查找
}
}
Comparable接口
//Comparable接口
public class ComparableTest {
public static void main(String[] args) {
List list = new LinkedList();
list.add(new Name("Karl", "M"));
list.add(new Name("Yun", "Hao"));
list.add(new Name("John", "O"));
list.add(new Name("Tom", "M"));
System.out.println(list); //[Karl M, Yun Hao, John O, Tom M]
Collections.sort(list); //会调用compareTo方法
System.out.println(list); //[Yun Hao, Karl M, Tom M, John O]
}
}
Map接口
//Map接口中的方法
public class MethodInMap {
public static void main(String[] args) {
Map<String, Integer> m1 = new HashMap<String, Integer>();
Map m2 = new TreeMap();
/*m1.put("one", new Integer(1));
m1.put("two", new Integer(2));
m1.put("three", new Integer(3));*/
m1.put("one", 1);
m1.put("two", 2);
m1.put("three", 3);
/*m2.put("A", new Integer(1));
m2.put("B", new Integer(2));*/
m2.put("A", 1);
m2.put("B", 2);
System.out.println(m1.size()); //3
System.out.println(m1.containsKey("one"));//true
System.out.println(m2.containsValue(1)); //true
if(m1.containsKey("two")) {
//int i = ((Integer) m1.get("two")).intValue();
int i = m1.get("two");
System.out.println(i); //2
}
Map m3 = new HashMap(m1);
m3.putAll(m2);
System.out.println(m3); //{two=2, A=1, B=2, one=1, three=3}
}
}
泛型
//泛型
public class Template {
public static void main(String[] args) {
List<String> c = new ArrayList<String>();
c.add("aaa");
c.add("bbb");
c.add("ccc");
for(int i = 0; i < c.size(); i++) {
String s = c.get(i);
System.out.println(s);
}
Collection<String> c2 = new HashSet<String>();
c2.add("aaa"); c2.add("bbb"); c2.add("ccc");
for(Iterator<String> it = c2.iterator(); it.hasNext();) {
String s = it.next();
System.out.println(s);
}
}
}
//泛型类
class MyName implements Comparable<MyName> {
int age;
public int compareTo(MyName mn) {
if(this.age > mn.age) return 1;
else if(this.age < mn.age) return -1;
else return 0;
}
}