collection
----- LIst
---ArrayList
---LinkedList
---Vector
------Set
----HashSet
----TreeSet
ArrayList 的基本用法
import java.util.*;
class ArrayListDemo
{
public static void main(String[] args)
{
ArrayList al = new ArrayList();
al.add("haha");
al.add("hello list");
/*
Iterator it = al.iterator();
while(it.hasNext()) {
System.out.println(it.next());
}*/
//for 循环节省内存;
for(Iterator it = al.iterator();it.hasNext();){
System.out.println(it.next());
}
//ListIterator 接口对象比 iterator 对象具有更多的方法;但是只有list 对象有;
ListIterator all = al.listIterator();
while(all.hasNext())
{
Object obj = all.next();
if(obj.equals("haha")){
all.set("fasfdsaf");
}
}
print(al);
}
public static void print(Object obj)
{
System.out.println(obj);
}
}
LinkedList 的基本用法
//添加一个person 对象中 并且 除去重复的
import java.util.*;
class Person
{
private int age;
private String name;
Person(int age, String name)
{
this.age = age;
this.name = name;
}
public int sayAge()
{
return age;
}
public String sayName()
{
return name;
}
public boolean equals(Object obj)
{
if(!(obj instanceof Person)){
return false;
}
Person p = (Person) obj;
return p.name == name && p.age == age;
}
}
class linkListDemo
{
public static void main(String[] args)
{
ArrayList al = new ArrayList();
al.add(new Person(12,"tang"));
al.add(new Person(12,"tang"));
al.add(new Person(13,"wang"));
al = singleElement(al);
Iterator it = al.iterator();
while(it.hasNext()){
Person p = (Person) it.next();
sop(p.sayName()+"----"+p.sayAge());
}
}
public static void sop(Object obj)
{
System.out.println(obj);
}
public static ArrayList singleElement(ArrayList al)
{
ArrayList newal = new ArrayList();
Iterator it = al.iterator();
while(it.hasNext())
{
Object obj = it.next();
if (!newal.contains(obj)) {
newal.add(obj);
}
}
return newal;
}
}
vector的基本用法
import java.util.*;
class VectorDemo
{
public static void main(String[] args){
Vector vc = new Vector();
vc.add("val1");
vc.add("val2");
//其实和迭代器一样;
Enumeration enums = vc.elements();
while(enums.hasMoreElements()) {
System.out.println(enums.nextElement());
}
}
}
HashSet的基本用法
import java.util.*;
class Person
{
private int age;
private String name;
Person(int age, String name)
{
this.age = age;
this.name = name;
}
public int getAge()
{
return this.age;
}
public String getName()
{
return this.name;
}
public int hashCode()
{
System.out.println(this.name+"----hashCode");
return this.name.hashCode()+this.age*37;
}
public boolean equals(Object obj)
{
if(!(obj instanceof Person)){
return false;
}
System.out.println(this.name+"----equals");
Person p = (Person) obj;
return this.name.equals(p.getName()) && this.age == p.getAge();
}
}
class HashSetTest
{
public static void main(String[] args)
{
HashSet hs = new HashSet();
hs.add(new Person(11,"a1"));
hs.add(new Person(12,"a2"));
hs.add(new Person(13,"a3"));
hs.add(new Person(13,"a3"));
Iterator it = hs.iterator();
while(it.hasNext())
{
Person p = (Person) it.next();
sop(p.getName()+"---"+p.getAge());
}
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
import java.util.*;
import java.lang.*;
class Person implements Comparable
{
public int age;
public String name;
Person(int age, String name)
{
this.age = age;
this.name = name;
}
public int compareTo(Object obj)
{
if(!(obj instanceof Person)){
throw new RuntimeException("非 Person 对象");
}
Person p = (Person) obj;
if(this.age > p.age) {
return 1;
}
if(this.age == p.age){
if(this.name.hashCode() > p.name.hashCode()){
return 1;
}
if(this.name.hashCode() < p.name.hashCode()){
return -1;
}
if(this.name.hashCode() == p.name.hashCode()){
return 0;
}
}
if(this.age < p.age){
return -1;
}
return 0;
}
public int getAge()
{
return this.age;
}
public String getName()
{
return this.name;
}
}
class TreeSetDemo
{
public static void main(String[] args)
{
TreeSet ts = new TreeSet();
ts.add(new Person(13,"a3"));
ts.add(new Person(12,"a2"));
ts.add(new Person(11,"a1"));
ts.add(new Person(11,"a5"));
Iterator it = ts.iterator();
while(it.hasNext())
{
Person p = (Person) it.next();
sop(p.getName()+"---"+p.getAge());
}
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}