import java.util.*;
//
public class TestColl {
public static void main(String[] args) {
Collection coll = new ArrayList();
coll.add("abcd");
//插入字符串对象
coll.add(123);
//插入Int对象
coll.add(123);
coll.add(new Student("Gaoxiaof",23));
//插入Student对象
coll.add(new Student("Gaoxiaof",23));
//插入另一个Student对象
System.out.println(coll);
//直接输出集合中的元素,得到结果[abcd,123,123,Gaoxiaof 23,Gaoxiaof 23]
}
}
//
class Student {
private String name;
private int age;
Student(String name,int n) {
this.name = name;
this.age = n;
}
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
public String toString() {
return this.name + " " + this.age;
}
}
Collection coll = new ArrayList();
coll.add("abcd");
coll.add(new Integer(128));
coll.add(new Student("Gaoxiaofang",23));
System.out.println(coll.remove(new Integer(128))); //true
coll.remove(new Student("Gaoxiaofang",23)); //false,因为没有重写equals()
System.out.println(coll); //return: [abcd,Gaoxiaofang 23]
Collection coll = new ArrayList(); coll.add(new Integer(128)); System.out.println(coll.contains(new Integer(128))); //true
import java.util.*;
public class TestColl {
public static void main(String[] args) {
Collection coll = new ArrayList();
coll.add("abcd");
coll.add(new Integer(129));
coll.add(new Student("Gaoxiaofang",23));
Iterator it = coll.iterator();
while (it.hasNext()) { //Iterator遍历的方法
System.out.println(it.next()); //return:abcd,129,Gaoxiaofang 23
}
}
}
class Student {
private String name;
private int age;
Student(String name,int n) {
this.name = name;
this.age = n;
}
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
public String toString() {
return this.name + " " + this.age;
}
}
Collection coll = new ArrayList();
coll.add("abcd");
coll.add(new Integer(129));
coll.add(new Student("Gaoxiaofang",23));
for (Iterator it = coll.iterator();it.hasNext();) {
Object obj = it.next();
if (obj instanceof Student) {
Student s = (Student)obj;
System.out.println(s.getName()); //return: Gaoxiaofang
} else {
it.remove();
}
}
System.out.println(coll); //return: [Gaoxiaofang 23]
for (Iterator it = coll.iterator();it.hasNext();) {
if (it.next() instanceof Student) {
Student s = (Student)it.next();
System.out.println(s.getName());
}
}
List l = new ArrayList();
for (int i=0;i<l.size();i++) {
System.out.println(l.get(i));
}
import java.util.*;
public class TestList {
public static void main(String[] args) {
List ls = new ArrayList();
ls.add(new Student("Malong1",21));
ls.add(new Student("Malong2",22));
ls.add(1,new Student("Malong3",23));
//[Malong1 21,Malong3 23,Malong2 22]
System.out.println(ls.indexOf(new Student("Malong3",23)));
// return:1
ls.set(2,new Student("Gaoxiao1",22));
//[Malong1 21,Malong3 23,Gaoxiao1 22]
for (Iterator it = l.iterator();it.hasNext();) {
//第一种迭代
Student stu = (Student)it.next();
if (stu.getAge() == 22) {
it.remove();
// the safe way to operate element
//ls.add(new Student("Malong4",24)); //throw ConcurrentModificationException
}
}
//[Malong1 21,Malong3 23]
System.out.println(l+"\n---------------");
for (int i=0;i<ls.size();i++) {
//第二种迭代
System.out.println(ls.get(i));
}
}
}
class Student {
private String name;
private int age;
Student(String name,int n) {
this.name = name;
this.age = n;
}
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
//override toString()
public String toString() {
return this.name + " " + this.age;
}
//override equals()
public Boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Student)) {
throw new ClassCastException("Class error");
}
Student stu = (Student)obj;
return this.name.equals(stu.name) && this.age == stu.age;
}
}
List l = new ArrayList();
l.add(new Student("Malong1",21));
l.add(new Student("Malong2",22));
l.add(1,new Student("Malong3",23)); //[Malong1 21,Malong3 23,Malong2 22]
l.set(2,new Student("Gaoxiao1",22));//[Malong1 21,Malong3 23,Gaoxiao1 22]
for (ListIterator li = l.listIterator();li.hasNext();) {
Student stu = (Student)li.next();
if (stu.getAge() == 22) {
//l.add(new Student("Malong4",24)); //throw ConcurrentModificationException
li.add(new Student("Malong4",24));
}
}
import java.util.*;
class MyQueue {
private LinkedList mylist;
MyQueue() {
mylist = new LinkedList();
}
// add element to queue
public void add(Object obj) {
mylist.addFirst(obj);
//Fisrt In
}
//get element from queue
public Object get() {
return mylist.removeLast();
//First Out
}
//queue is null?
public Boolean isNull() {
return mylist.isEmpty();
}
//the size of queue
public int size() {
return mylist.size();
}
//remove element in queue by index
public Boolean remove(int index) {
if(this.size()-1 < index) {
throw new IndexOutOfBoundsException("index too large!");
}
mylist.remove(index);
return true;
}
//remove the first appearance element in queue by Object
public Boolean remove(Object obj) {
return mylist.remove(obj);
}
public String toString() {
return mylist.toString();
}
}
import java.util.*;
public class FIFO {
public static void main(String[] args) {
MyQueue mq = new MyQueue();
mq.add("Malong1");
mq.add("Malong2");
mq.add("Malong3");
mq.add("Malong4"); //[Malong4,Malong3,Malong2,Malong1]
System.out.println(mq.size()); //return:4
mq.remove(2); //[Malong4,Malong3,Malong1]
mq.remove("Malong1"); //[Malong4,Malong3]
System.out.println(mq);
while (!mq.isNull()) {
System.out.println(mq.get());
}
}
}
import java.util.*;
public class TestHashSet {
public static void main(String[] args) {
Set s = new HashSet();
s.add("abcd4");
s.add("abcd1");
s.add("abcd2");
s.add("abcd3");
s.add("abcd1"); //重复
for (Iterator it = s.iterator();it.hasNext();) {
Object obj = it.next();
System.out.println(obj);
}
}
}
abcd2 abcd3 abcd4 abcd1
class Student {
String name;
int age;
Student(String name,int n) {
this.name = name;
this.age = n;
}
//override toString()
public String toString() {
return this.name + " " + this.age;
}
//override equals()
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Student)) {
return false;
}
Student stu = (Student)obj;
return this.name.equals(stu.name) && this.age == age;
}
}
import java.util.*;
public class TestHashSet {
public static void main(String[] args) {
Set s = new HashSet();
s.add(new Student("Malong1",21));
s.add(new Student("Malong1",21));
s.add(new Student("Malong1",21));
for (Iterator it = s.iterator();it.hasNext();) {
Object obj = it.next();
System.out.println(obj);
}
}
}
Malong1 21 Malong1 21 Malong1 21
public int hashCode() {
return this.name.hashCode() + age*31; //31可以是任意数,但不能是1或0。
}
s.add(new Student("lisi1",21));
s.add(new Student("lisi1",21)); //此处将调用equals(),且最终判断为重复对象
s.add(new Student("lisi2",24));
s.add(new Student("lisi3",23)); //此处将调用equals()
s.add(new Student("Gaoxiao1",23));
s.add(new Student("Gaoxiao2",21));
s.add(new Student("Gaoxiao3",22));
lisi1 21 Gaoxiao1 23 Gaoxiao3 22 lisi2 24 lisi3 23 Gaoxiao2 21
import java.util.*;
public class TestHashSet {
public static void main(String[] args) {
Set s = new LinkedHashSet();
s.add(new Student("lisi1",21));
s.add(new Student("lisi1",21));
s.add(new Student("lisi2",24));
s.add(new Student("lisi3",23));
s.add(new Student("Gaoxiao1",23));
s.add(new Student("Gaoxiao3",21));
s.add(new Student("Gaoxiao2",22));
for (Iterator it = s.iterator();it.hasNext();) {
Object obj = it.next();
System.out.println(obj);
}
}
}
lisi1 21 lisi2 24 lisi3 23 Gaoxiao1 23 Gaoxiao3 21 Gaoxiao2 22
import java.util.*;
//
public class TestTreeSet {
public static void main(String[] args) {
Set t = new TreeSet();
t.add("abcd2");
t.add("abcd11");
t.add("abcd3");
t.add("abcd1");
//t.add(23);
//t.add(21);
//t.add(21);
for (Iterator it = t.iterator();it.hasNext();) {
Object obj = it.next();
System.out.println(obj);
}
}
}
Exception in thread "main" java.lang.ClassCastException: Student cannot be cast to java.lang.Comparable at java.util.TreeMap.compare(Unknown Source) at java.util.TreeMap.put(Unknown Source) at java.util.TreeSet.add(Unknown Source) at TestTreeSet.main(TestTreeSet.java:8)
class Student implements Comparable {
String name;
int age;
Student(String name,int n) {
this.name = name;
this.age = n;
}
public String toString() {
return this.name + " " + this.age;
}
public int compareTo(Object obj) {
if (!(obj instanceof Student)) {
throw new ClassCastException("Class cast wrong!");
}
Student stu = (Student)obj;
// compare name first, then age
int temp = this.name.compareTo(stu.name);
return temp == 0 ? this.age - stu.age :temp;
}
}
t.add(new Student("Malongshuai1",23));
t.add(new Student("Malongshuai3",21));
t.add(new Student("Malongshuai2",23));
t.add(new Student("Malongshuai1",23)); //重复
t.add(new Student("Malongshuai1",22));
Malongshuai1 22 Malongshuai1 23 Malongshuai2 23 Malongshuai3 21
import java.util.*;
//
public class SortByAge implements Comparator {
public int compare(Object o1,Object o2) {
//Cast to Student first
if (!(o1 instanceof Student) || !(o2 instanceof Student)) {
throw new ClassCastException("Wrong");
}
Student s1 = (Student)o1;
Student s2 = (Student)o2;
//compare age first, then name
int temp = s1.age - s2.age;
return temp == 0 ? s1.name.compareTo(s2.name) : temp;
}
}
public class TestTreeSet {
public static void main(String[] args) {
Set t = new TreeSet(new SortByAge());
t.add(new Student("Malongshuai1",23));
t.add(new Student("Malongshuai3",21));
t.add(new Student("Malongshuai2",23));
t.add(new Student("Malongshuai1",23)); //重复
t.add(new Student("Malongshuai1",22));
for (Iterator it = t.iterator();it.hasNext();) {
Object obj = it.next();
System.out.println(obj);
}
}
}
Malongshuai3 21 Malongshuai1 22 Malongshuai1 23 Malongshuai2 23
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有