您的位置:首页 > 编程语言 > Java开发

Java的基础知识4——容器

2016-07-26 22:03 615 查看
1、Collection接口定义了存取一组对象的方法,其子接口Set和List分别定义了存储方式。

Set中的数据对象没有顺序且不可以重复。

List中的数据对象有顺序且可以重复。

Map接口定义了存储键值对的方法,键值对通过键来标识,所以键值不能重复。

2、容器类对象在调用remove、contains等方法时需要比较对象是否相等,这会涉及到对象类型的equals方法和hashCode方法。对于自定义的类型,需要重写equals和hashCode方法以实现自定义的对象相等规则。

3、所有实现了Collection接口的容器类都有一个Iterator方法,用来返回一个实现了Iterator接口的对象。

Iterator对象称作迭代器,用以方便的实现对容器内元素的遍历操作。

4、java.util.Collections提供了一些静态方法实现基于List容器的一些常用算法。

5、所有可以“排序”的类都实现了java.lang.Comparable接口,Comparable接口中只有一个方法,public int compareTo(Object obj);该方法返回0表示this == obj;返回正数表示this > obj;返回负数表示this < obj。实现Comparable接口的类通过实现compareTo方法从而确定该类对象的排序方式。

import java.util.*;

public class Test{

public static void main(String args[]){
Collection c = new ArrayList();
c.add("Hello World!");
c.add(new Integer(100));
c.add(new Double(3.33));
c.add(new Name("三","张"));
c.add(new Name("四","李"));
c.add(new Name("四四","李"));
c.add(new Name("五","王"));
c.add(new Name("六","赵"));
System.out.println(c.size());
System.out.println(c);
c.remove("Hello World!");
c.remove(new Integer(100));
c.remove(new Double(3.33));
c.remove(new Name("四","李"));
System.out.println(c.size());
System.out.println(c);

//Iterator遍历  start
/*Iterator i = c.iterator();
while(i.hasNext()){
Name name = (Name)i.next();
if(name.getFirstName().length() >= 2){
i.remove();
}
}
*/
for(Iterator j=c.iterator();j.hasNext();){
Name name = (Name)j.next();
if(name.getFirstName().length() >= 2){
j.remove();
}
}
System.out.println(c);
//Iterator遍历  end

//Set接口  start(HashSet)
Set s1 = new HashSet();
s1.add("a");
s1.add("b");
s1.add("c");
Set s2 = new HashSet();
s2.add("d");
s2.add("c");
s2.add("a");
Set sn = new HashSet(s1);
sn.retainAll(s2);
Set su = new HashSet(s2);
su.addAll(s1);
System.out.println(sn);
System.out.println(su);
//Set接口  end

//List接口 start(LinkedList)
List l = new LinkedList();
for(int i=0;i<5;i++){
l.add("list add "+i);
}
System.out.println(l);
l.add(1,"<hello world>");
System.out.println(l);
l.set(3, "0123456789");
System.out.println(l);
l.remove(5);
System.out.println(l);
System.out.println((String)l.get(0));
System.out.println(l.indexOf("0123456789"));
//List接口 end

//Collections类  start
List ll = new ArrayList();
for(int j=0;j<5;j++){
ll.add("a"+j);
}
System.out.println(ll);
Collections.shuffle(ll);
System.out.println(ll);
Collections.reverse(ll);
System.out.println(ll);
Collections.sort(ll);
System.out.println(ll);
System.out.println(Collections.binarySearch(ll, "a3"));
//Collections类  end

//Comparable接口  start
List lc = new ArrayList();
lc.add(new Name("-yahong","cao"));
lc.add(new Name("-hai","xie"));
lc.add(new Name("-zhen","qin"));
lc.add(new Name("-qiang","a"));
lc.add(new Name("-shan","cao"));
System.out.println(lc);
Collections.sort(lc);
System.out.println(lc);
//Comparable接口  end

//Map接口  start(HashMap、TreeMap)
Map m1 = new HashMap();
Map m2 = new TreeMap();
Map m3 = null;
m1.put("one",new Integer(1));
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());
System.out.println(m1);
System.out.println(m2);
m1.remove("three");
System.out.println(m1);
System.out.println(m1.containsKey("one"));
System.out.println(m2.containsValue(new Integer(3)));
if(m1.containsKey("two")){
int i = ((Integer)m1.get("two")).intValue();
System.out.println(i);
}
m3 = new TreeMap(m1);
m3.putAll(m2);
System.out.println(m3);
//Map接口  end
}
}

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;
}

public String toString(){
return lastName+firstName;
}

public boolean equals(Object obj){
if(obj instanceof Name){
Name name = (Name)obj;
return (this.firstName.equals(name.firstName) && this.lastName.equals(name.lastName));
}
return super.equals(obj);
}

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

public int compareTo(Object obj){
Name name = (Name)obj;
int temp = this.lastName.compareTo(name.lastName);
return  temp != 0 ? temp : this.firstName.compareTo(name.firstName);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: