马士兵J2SE-第七章-容器

 
class Name {
	private String firstName,lastName;
	public Name(String firstName,String lastName) {
		this.firstName=firstName;
		this.lastName=lastName;
	}
	
	public String getFirstName() {
		return firstName;
	}
	
	public String getLastName() {
		return lastName;
	}
	
	public String toString() {
		return firstName+" "+lastName;
	}
}



public class test {
	public static void main(String[] args) {
		Name name1=new Name("f1","l1");
		Name name2=new Name("f2","l2");
		Name name3=new Name("f3","l3");
		System.out.println(name1);
		System.out.println(name2);
		System.out.println(name3);
	}
}


输出:

 

f1 l1
f2 l2
f3 l3

 

 

import java.util.*;

public class test {
	public static void main(String[] args) {
		Collection c=new ArrayList();
		
		c.add("hello");
		c.add(new Name("f1","l1"));
		c.add(new Integer(100));
		
		System.out.println(c.size());
		System.out.println(c);
	}
}

class Name {
	private String firstName,lastName;
	public Name(String firstName,String lastName) {
		this.firstName=firstName;
		this.lastName=lastName;
	}
	
	public String getFirstName() {
		return firstName;
	}
	
	public String getLastName() {
		return lastName;
	}
	
	public String toString() {
		return firstName+" "+lastName;
	}
}
输出:

3
[hello, f1 l1, 100]


import java.util.*;

public class test {
	public static void main(String[] args) {
		Collection c=new HashSet();
		
		c.add("hello");
		c.add(new Name("f1","l1"));
		c.add(new Integer(100));
		
		c.remove("hello");
		c.remove(new Integer(100));
		
		System.out.println(c.remove(new Name("f1","l1")));
		System.out.println(c.size());
		System.out.println(c);
	}
}

class Name {
	private String firstName,lastName;
	public Name(String firstName,String lastName) {
		this.firstName=firstName;
		this.lastName=lastName;
	}
	
	public String getFirstName() {
		return firstName;
	}
	
	public String getLastName() {
		return lastName;
	}
	
	public String toString() {
		return firstName+" "+lastName;
	}
}

输出:

false
1
[f1 l1]


import java.util.*;

public class test {
	public static void main(String[] args) {
		Collection c=new LinkedList();
		
		
		c.add(new Name("f1","l1"));
		c.add(new Name("f2","l2"));
		
		System.out.println(c.contains(new Name("f2","l2")));
		c.remove(new Name("f2","l2"));
		System.out.println(c);
		
	}
}

class Name {
	private String firstName,lastName;
	public Name(String firstName,String lastName) {
		this.firstName=firstName;
		this.lastName=lastName;
	}
	
	public String getFirstName() {
		return firstName;
	}
	
	public String getLastName() {
		return lastName;
	}
	
	public String toString() {
		return firstName+" "+lastName;
	}
	
	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);
	}

	public int hashCode() {
		return firstName.hashCode();
	}
	
}


输出:

true
[f1 l1]


 

Iterator方法之remove

package com.zzk.test;

import java.util.*;

public class test {
	public static void main(String[] ars) {
		Collection c=new HashSet();
		c.add(new Name("ff1f","l1l1"));
		c.add(new Name("f2","l2"));
		c.add(new Name("fff3","lll3"));
		for(Iterator i=c.iterator();i.hasNext();) {
			Name name=(Name)i.next();
			if(name.getFirstName().length()<3) {
				i.remove();
			}
		}
		System.out.println(c);
	}
}


class Name {
	private String firstName,lastName;
	public Name(String firstName,String lastName) {
		this.firstName=firstName;
		this.lastName=lastName;
	}
	
	public String getFirstName() {
		return firstName;
	}
	
	public String getLastName() {
		return lastName;
	}
	
	public String toString() {
		return firstName+" "+lastName;
	}
}

输出:

[fff3 lll3, ff1f l1l1]



增强的for循环:

package com.zzk.test;

import java.util.*;


public class test {
	public static void main(String[] args) {
		int[] arr={1,2,3,4,5};
		for(int i:arr) {
			System.out.println(i);
		}
		
		Collection c=new ArrayList();
		c.add(new String("aaa"));
		c.add(new String("bbb"));
		c.add(new String("ccc"));
		for(Object o:c) {
			System.out.println(o);
		}
	}
}

输出:

1
2
3
4
5
aaa
bbb
ccc



Set方法举例:

package com.zzk.test;

import java.util.*;
 
public class test {
	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 sn=new HashSet(s1);
		sn.retainAll(s2);
		System.out.println(s2);
		
		Set su=new HashSet(s1);
		su.addAll(s2);
		System.out.println(su);
	}
}


输出:

[d, b, a]
[d, b, c, a]

 

 

 LIST方法

package com.zzk.test;

import java.util.*;
 
public class test {
	public static void main(String[] args) {
		List l1=new LinkedList();
		
		for(int i=0;i<=5;i++) {
			l1.add("a"+i);
		}
		
		System.out.println(l1);
		l1.add(3,"a100");
		System.out.println(l1);
		l1.set(6, "a200");
		
		System.out.println(l1);
		System.out.print((String)l1.get(2)+" ");
		System.out.println(l1.indexOf("a3"));
		
		l1.remove(1);
		System.out.println(l1);
		
	}
}


输出:

[a0, a1, a2, a3, a4, a5]
[a0, a1, a2, a100, a3, a4, a5]
[a0, a1, a2, a100, a3, a4, a200]
a2 4
[a0, a2, a100, a3, a4, a200]

 

 LIST常用算法:

package com.zzk.test;

import java.util.*;
 
public class test {
	public static void main(String[] args) {
		List l1=new LinkedList();
		List l2=new LinkedList();
		for(int i=0;i<=9;i++) {
			l1.add("a"+i);
		}
		
		System.out.println(l1);
		Collections.shuffle(l1);//随机排序
		System.out.println(l1);
		
		Collections.reverse(l1);//逆序排序
		System.out.println(l1);
		
		Collections.sort(l1);
		System.out.println(l1);
		
		Collections.binarySearch(l1, "a5");
		System.out.println(l1);
		
	}
}

输出:

[a0, a1, a2, a3, a4, a5, a6, a7, a8, a9]
[a9, a7, a6, a4, a0, a1, a3, a8, a5, a2]
[a2, a5, a8, a3, a1, a0, a4, a6, a7, a9]
[a0, a1, a2, a3, a4, a5, a6, a7, a8, a9]
[a0, a1, a2, a3, a4, a5, a6, a7, a8, a9]

 

 


Comparable接口:

package com.zzk.test;
import java.util.*;

public class test {
    public static void main(String[] args) {
       List l1=new LinkedList();
       l1.add(new Name("Karl","M"));
       l1.add(new Name("Strven","Lee"));
       l1.add(new Name("John","O"));
       l1.add(new Name("Tom","M"));
       System.out.println(l1);
       Collections.sort(l1);
       System.out.println(l1);
    }


}

class Name implements Comparable {
    private String firstName,lastName;
    public Name(String firstName, String lastName) {
        this.firstName = firstName; this.lastName = lastName;
    }
    public String getFirstName() {  return firstName;   }
    public String getLastName() {   return lastName;   }
    public String toString() {  return firstName + " " + lastName;  }
    
    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);
		}
		public int hashCode() {
		    return firstName.hashCode();
		}
		
		
		
		public int compareTo(Object o) {//重写,要去copy!
        Name n = (Name)o;
        int lastCmp = 
            lastName.compareTo(n.lastName);
        return 
             (lastCmp!=0 ? lastCmp :
              firstName.compareTo(n.firstName));
    }
		
}


输出:

[Karl M, Strven Lee, John O, Tom M]
[Strven Lee, Karl M, Tom M, John O]

 

 

 Map接口

package com.zzk.test;
import java.util.*;

public class test {
	public static void main(String[] args) {
		Map m1=new HashMap();
		Map m2=new TreeMap();
		
		m1.put("one", new Integer(1));//key是String类型,value是Integer类型
		m1.put("two", new Integer(2));
		m1.put("three", new Integer(3));
		
		m2.put("A", new Integer(1));
		m2.put("B", new Integer(2));
		
		System.out.println(m1.size());//m1的size,3
		System.out.println(m1.containsKey("one"));//contains时需要equals,需要比较hashcode
		System.out.println(m2.containsValue(new Integer(1)));
		
		if(m1.containsKey("two")) {
			int i=((Integer)m1.get("two")).intValue();
			System.out.println(i);
		}
		
		Map m3=new HashMap();
		m3.putAll(m2);
		System.out.println(m3);
	}
}


输出:

3
true
true
2
{A=1, B=2}

 

 泛型

package com.zzk.test;
import java.util.*;

public class test {
	public static void main(String[] args) {
		List<String> c=new ArrayList<String>();
		c.add("aaa");
		c.add("bbb");
		c.add("ccc");
		c.add("aaa");
		
		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");
		c2.add("aaa");
		
		for(Iterator<String> it=c2.iterator();it.hasNext();) {
			String s=it.next();
			System.out.println(s);
		}
		
	}
}


class MyName implements Comparable<MyName> {
	int age;
	
	@Override
	public int compareTo(MyName mn) {
		// TODO Auto-generated method stub
		if(this.age>mn.age) return 1;
		else if(this.age<mn.age) return -1;
		else return 0;
	}
}


输出:

aaa
bbb
ccc
aaa
aaa
ccc
bbb

 

 

 泛型与打包

package com.zzk.test;
import java.util.*;

public class test {
	public static void main(String[] args) {
		Map<String,Integer> m1=new HashMap<String,Integer>();
		m1.put("one", 1);
		m1.put("two", 2);
		m1.put("three", 3);
		
		System.out.println(m1.size());
		System.out.println(m1.containsKey("one"));
		
		if(m1.containsKey("two")) {
			int i=m1.get("two");
			System.out.println(i);
		}
	}
}


输出:

3
true
2

 

 

 

 

 

 

 

 


 



 

你可能感兴趣的:(马士兵J2SE-第七章-容器)