为什么出现集合类?
面向对象语言对事物的体现都是以对象的形式,所以为了方便对多个对象的操作,就对对象进行存储,集合就是存储对象最常用的一种方式。所以被迭代器取代了。
枚举郁郁而终了
Linklist的特有用法:
getFirst();
getLast();//获取元素,但不删除元素
addFirst();//添加元素
addLast();
removeLast();//删除元素
removeFirst();
JDK 1.6:
pollFirst()
获取并移除此列表的第一个元素;如果此列表为空,则返回 null。
pollLast()
获取并移除此列表的最后一个元素;如果此列表为空,则返回 null。
peekFirst()
获取但不移除此列表的第一个元素;如果此列表为空,则返回 null。
peekLast()
获取但不移除此列表的最后一个元素;如果此列表为空,则返回 null。
class LinkedListDemo{ public static void main(){ Linked link=new LinkedList(); link.addList("java01"); link.addlist("java02"); while(!link.isEmpty()){ sop(link.removeLast()); } } public static void sop(Object object){ System.out.println("obj"); } }
class Queue{ private LinkedList link; Queue{ link=new LinkedList(); } public void myadd(Object obj){ link.addFirst(obj); } public Object myGet(){ return link.removeLast(); } public boolean isNull(){ return link.isEmpty(); } }
classArraylistTest{ public static void main(){ ArrayList a1=new ArrayList(); a1.add("java01"); a1.add("java01"); a1.add("java01"); a1.add("java01"); a1.add("java01"); sop(a1); a1=singleElement(a1); sop(a1); } 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; } public static void sop(Object object){ System.out.println("obj"); } }
class Person{ private String name; private int age; Person(String name,int age) { this.name=name; this.age=age; } public boolean equals(Object object){ if(!obj instanceof Person) return false; Person p=(Person)Obj; return this.name.equals(p.name)&&this.age=p.age; } public String getName(){ return name; } public int getAge(){ return age; } }
Class ArrayListTest2{ public static void main(String[] args){ ArrayList a1=new ArrayList(); a1.add(new Person("lisi01,30")); Iterator it=al.iterator(); while(it.hasNext()){ //想用特有方法时,就要转型,因为迭代器不知道你是什么类型,只返回obj Object obj=it.next(); Person p=(Person) obj; sop(it.next().getName()+"::"+it.next().getAge()); } } 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; } }
class HashSetDemo { public static void sop(Object obj) { System.out.println(obj); } public static void main(String[] args) { HashSet hs = new HashSet(); sop(hs.add("java01")); sop(hs.add("java01")); hs.add("java02"); hs.add("java03"); hs.add("java03"); hs.add("java04"); Iterator it = hs.iterator(); while(it.hasNext()) { sop(it.next()); } } }
import java.util.*; /* 往hashSet集合中存入自定对象 姓名和年龄相同为同一个人,重复元素。 */ class HashSetTest { public static void sop(Object obj) { System.out.println(obj); } public static void main(String[] args) { HashSet hs = new HashSet(); hs.add(new Person("a1",11)); hs.add(new Person("a2",12)); hs.add(new Person("a3",13)); // hs.add(new Person("a2",12)); // hs.add(new Person("a4",14)); //sop("a1:"+hs.contains(new Person("a2",12))); // hs.remove(new Person("a4",13)); Iterator it = hs.iterator(); while(it.hasNext()) { Person p = (Person)it.next(); sop(p.getName()+"::"+p.getAge()); } } }
class Person { private String name; private int age; Person(String name,int age) { this.name = name; this.age = age; } public int hashCode() { System.out.println(this.name+"....hashCode"); return name.hashCode()+age*37;//这个地方37,只是在一个范围里面。与具体的值无关 } public boolean equals(Object obj)//这个地方记复写, { if(!(obj instanceof Person)) return false; Person p = (Person)obj; System.out.println(this.name+"...equals.."+p.name); return this.name.equals(p.name) && this.age == p.age; } public String getName() { return name; } public int getAge() { return age; } }
Class TreeSetDemo(){ public static void main(){ TreeSet ts=new TreeSet(); ts.add(new Student("liuhan",22)); ts.add(new Student("diudan",24));//这个学生对象不具有比较性//找一个接口去实现它,实现了就具有比较性 Iterator it=ts.Iterator(); while(it.hasNext()){ System.out.println(it.next()); } } }
Class Student implements Comparable{//该接口强制学生具有比较性 private String name; private int age; Student (String name,int age){ this.name=name; this.age=age; } public int CompareTo(){ if(!(obj instanceof Student)) throw new RuntimeException("不是学生对象"); Student s=(Student)Obj; System.out.println(this.name+"...Compare to"+s.name); if(this.age>s.age)//此对象与参数对象进行比较 return 1; if(this.age==s.age) { this.name.compareTo(s.name);//如果年龄相同,就按照字符串排序 } //当主要条件相同时,就按次要条件排序。 return 0; return -1; } public String getName(){ return name; } public String getAge(){ return age; } }
class MyCompare implements Comparator{ public int compare(Object o1,Object o2){ Student s1=(Student)o1; Student s2=(Student)o2; int num=s1.getName().compareTo(s2.getName()); if(num==0){ return s1.getAge()-s2.getAge(); //对象封装,调用CompareTo return new Integar(s1.getAge()).compareTo(new Integer(s2.getAge())); } return num; } }
import java.util.*; class TreeSetTest { public static void main(String[] args) { TreeSet ts = new TreeSet(new StrLenComparator()); ts.add("abcd"); ts.add("cc"); ts.add("cba"); ts.add("aaa"); ts.add("z"); ts.add("hahaha"); Iterator it = ts.iterator(); while(it.hasNext()) { System.out.println(it.next()); } } } class StrLenComparator implements Comparator { public int compare(Object o1,Object o2) { String s1 = (String)o1; String s2 = (String)o2; /* if(s1.length()>s2.length()) return 1; if(s1.length()==s2.length()) return 0; */ int num = new Integer(s1.length()).compareTo(new Integer(s2.length())); if(num==0) return s1.compareTo(s2); return num; } }