疯狂JAVA讲义---第七章(上):集合set
2008-12-29 13:39
435 查看
集合是j2se中非常重要的一部分,和我前面花了一个星期讲的面向对象是java基础的两座大山。首先我先给个这部分的结构图
我先列些基础操作:foreach遍历集合
public class TestForeach
{
public static void main(String[] args)
{
//创建一个集合
Collection books = new HashSet();
books.add(new String("轻量级J2EE企业应用实战"));
books.add(new String("Struts2权威指南"));
books.add(new String("基于J2EE的Ajax宝典"));
for (Object obj : books)
{
String book = (String)obj;
System.out.println(book);
if (book.equals("Struts2权威指南"))
{
//下面代码会引发ConcurrentModificationException异常
books.remove(book);
}
}
System.out.println(books);
}
}
现在开始正式讲集合,先讲set中最重要的HashSet
集合HashSet的重点是要学会重写其equals和hashcode的方法
注意:应该尽量保证两个对象通过equals比较返回true,他们的hashcode返回也相等。
大家可以通过下面的代码来体会其中的奥秘,eg
class A
{
public boolean equals(Object obj)
{
return true;
}
}
//类B的hashCode()方法总是返回1,但没有重写其equals()方法
class B
{
public int hashCode()
{
return 1;
}
}
//类C的hashCode()方法总是返回2,但没有重写其equals()方法
class C
{
public int hashCode()
{
return 2;
}
public boolean equals(Object obj)
{
return true;
}
}
public class TestHashSet
{
public static void main(String[] args)
{
HashSet books = new HashSet();
//分别向books集合中添加2个A对象,2个B对象,2个C对象
books.add(new A());
books.add(new A());
books.add(new B());
books.add(new B());
books.add(new C());
books.add(new C());
System.out.println(books);
}
}
HashSet最主要的优点是快。
然后讲下LinkedHashSet,他的优点是按照插入顺序排列,速度略慢,eg
public class TestLinkedHashSet
{
public static void main(String[] args)
{
LinkedHashSet books = new LinkedHashSet();
books.add("Struts2权威指南");
books.add("轻量级J2EE企业应用实战");
//删除 Struts2权威指南
books.remove("Struts2权威指南");
//重新添加 Struts2权威指南
books.add("Struts2权威指南");
System.out.println(books);
}
}
TreeSet具体原理用到了红黑树http://baike.baidu.com/view/133754.htm比较复杂
主要TreeSet会调用对象的compareTo进行元素比较,
自然排序(根据集合元素的大小,将他们以升序排列)eg
class Z implements Comparable
{
int age;
public Z(int age)
{
this.age = age;
}
public boolean equals(Object obj)
{
return false;
}
public int compareTo(Object obj)
{
return 1;
}
}
public class TestTreeSet
{
public static void main(String[] args)
{
TreeSet set = new TreeSet();
Z z1 = new Z(6);
set.add(z1);
System.out.println(set.add(z1));
//下面输出set集合,将看到有2个元素
System.out.println(set);
//修改set集合的第一个元素的age属性
((Z)(set.first())).age = 9;
//输出set集合的最后一个元素的age属性,将看到也变成了9
System.out.println(((Z)(set.last())).age);
}
}
定制排序:egclass M
{
int age;
public M(int age)
{
this.age = age;
}
}
class N
{
int age;
public N(int age)
{
this.age = age;
}
}
public class TestTreeSet3
{
public static void main(String[] args)
{
TreeSet ts = new TreeSet(new Comparator()
{
public int compare(Object o1, Object o2)
{
int age1 = o1 instanceof M ? ((M)o1).age :((N)o1).age;
int age2 = o1 instanceof M ? ((M)o2).age :((N)o2).age;
return age1 - age2;
/*
M m1 = (M)o1;
M m2 = (M)o2;
if (m1.age > m2.age)
{
return -1;
}
else if (m1.age == m2.age)
{
return 0;
}
else
{
return 1;
}
*/
}
});
ts.add(new M(5));
ts.add(new M(-3));
ts.add(new N(9));
System.out.println(ts);
}
}
最后讲下EnumSet,这个是专为枚举类准备(具体知识请看我前几天的博客)这里我就举了例子,egenum Season
{
SPRING,SUMMER,FALL,WINTER
}
public class EnumSetDemo
{
public static void main(String[] args)
{
//创建一个EnumSet集合,集合元素就是Season枚举类的全部枚举值
EnumSet es1 = EnumSet.allOf(Season.class);
//输出[SPRING,SUMMER,FALL,WINTER]
System.out.println(es1);
//创建一个EnumSet空集合,指定其集合元素是Season类的枚举值。
EnumSet es2 = EnumSet.noneOf(Season.class);
//输出[]
System.out.println(es2);
//手动添加两个元素
es2.add(Season.WINTER);
es2.add(Season.SPRING);
//输出[SPRING,WINTER]
System.out.println(es2);
//以指定枚举值创建EnumSet集合
EnumSet es3 = EnumSet.of(Season.SUMMER , Season.WINTER);
//输出[SUMMER,WINTER]
System.out.println(es3);
EnumSet es4 = EnumSet.range(Season.SUMMER , Season.WINTER);
//输出[SUMMER,FALL,WINTER]
System.out.println(es4);
//新创建的EnumSet集合的元素和es4集合的元素有相同类型,
//es5的集合元素 + es4集合元素 = Season枚举类的全部枚举值
EnumSet es5 = EnumSet.complementOf(es4);
//输出[SPRING]
System.out.println(es5);
}
}
注意以上的集合都不线程安全,解决线程问题,我将过两天总结。
我先列些基础操作:foreach遍历集合
public class TestForeach
{
public static void main(String[] args)
{
//创建一个集合
Collection books = new HashSet();
books.add(new String("轻量级J2EE企业应用实战"));
books.add(new String("Struts2权威指南"));
books.add(new String("基于J2EE的Ajax宝典"));
for (Object obj : books)
{
String book = (String)obj;
System.out.println(book);
if (book.equals("Struts2权威指南"))
{
//下面代码会引发ConcurrentModificationException异常
books.remove(book);
}
}
System.out.println(books);
}
}
现在开始正式讲集合,先讲set中最重要的HashSet
集合HashSet的重点是要学会重写其equals和hashcode的方法
注意:应该尽量保证两个对象通过equals比较返回true,他们的hashcode返回也相等。
大家可以通过下面的代码来体会其中的奥秘,eg
class A
{
public boolean equals(Object obj)
{
return true;
}
}
//类B的hashCode()方法总是返回1,但没有重写其equals()方法
class B
{
public int hashCode()
{
return 1;
}
}
//类C的hashCode()方法总是返回2,但没有重写其equals()方法
class C
{
public int hashCode()
{
return 2;
}
public boolean equals(Object obj)
{
return true;
}
}
public class TestHashSet
{
public static void main(String[] args)
{
HashSet books = new HashSet();
//分别向books集合中添加2个A对象,2个B对象,2个C对象
books.add(new A());
books.add(new A());
books.add(new B());
books.add(new B());
books.add(new C());
books.add(new C());
System.out.println(books);
}
}
HashSet最主要的优点是快。
然后讲下LinkedHashSet,他的优点是按照插入顺序排列,速度略慢,eg
public class TestLinkedHashSet
{
public static void main(String[] args)
{
LinkedHashSet books = new LinkedHashSet();
books.add("Struts2权威指南");
books.add("轻量级J2EE企业应用实战");
//删除 Struts2权威指南
books.remove("Struts2权威指南");
//重新添加 Struts2权威指南
books.add("Struts2权威指南");
System.out.println(books);
}
}
TreeSet具体原理用到了红黑树http://baike.baidu.com/view/133754.htm比较复杂
主要TreeSet会调用对象的compareTo进行元素比较,
自然排序(根据集合元素的大小,将他们以升序排列)eg
class Z implements Comparable
{
int age;
public Z(int age)
{
this.age = age;
}
public boolean equals(Object obj)
{
return false;
}
public int compareTo(Object obj)
{
return 1;
}
}
public class TestTreeSet
{
public static void main(String[] args)
{
TreeSet set = new TreeSet();
Z z1 = new Z(6);
set.add(z1);
System.out.println(set.add(z1));
//下面输出set集合,将看到有2个元素
System.out.println(set);
//修改set集合的第一个元素的age属性
((Z)(set.first())).age = 9;
//输出set集合的最后一个元素的age属性,将看到也变成了9
System.out.println(((Z)(set.last())).age);
}
}
定制排序:egclass M
{
int age;
public M(int age)
{
this.age = age;
}
}
class N
{
int age;
public N(int age)
{
this.age = age;
}
}
public class TestTreeSet3
{
public static void main(String[] args)
{
TreeSet ts = new TreeSet(new Comparator()
{
public int compare(Object o1, Object o2)
{
int age1 = o1 instanceof M ? ((M)o1).age :((N)o1).age;
int age2 = o1 instanceof M ? ((M)o2).age :((N)o2).age;
return age1 - age2;
/*
M m1 = (M)o1;
M m2 = (M)o2;
if (m1.age > m2.age)
{
return -1;
}
else if (m1.age == m2.age)
{
return 0;
}
else
{
return 1;
}
*/
}
});
ts.add(new M(5));
ts.add(new M(-3));
ts.add(new N(9));
System.out.println(ts);
}
}
最后讲下EnumSet,这个是专为枚举类准备(具体知识请看我前几天的博客)这里我就举了例子,egenum Season
{
SPRING,SUMMER,FALL,WINTER
}
public class EnumSetDemo
{
public static void main(String[] args)
{
//创建一个EnumSet集合,集合元素就是Season枚举类的全部枚举值
EnumSet es1 = EnumSet.allOf(Season.class);
//输出[SPRING,SUMMER,FALL,WINTER]
System.out.println(es1);
//创建一个EnumSet空集合,指定其集合元素是Season类的枚举值。
EnumSet es2 = EnumSet.noneOf(Season.class);
//输出[]
System.out.println(es2);
//手动添加两个元素
es2.add(Season.WINTER);
es2.add(Season.SPRING);
//输出[SPRING,WINTER]
System.out.println(es2);
//以指定枚举值创建EnumSet集合
EnumSet es3 = EnumSet.of(Season.SUMMER , Season.WINTER);
//输出[SUMMER,WINTER]
System.out.println(es3);
EnumSet es4 = EnumSet.range(Season.SUMMER , Season.WINTER);
//输出[SUMMER,FALL,WINTER]
System.out.println(es4);
//新创建的EnumSet集合的元素和es4集合的元素有相同类型,
//es5的集合元素 + es4集合元素 = Season枚举类的全部枚举值
EnumSet es5 = EnumSet.complementOf(es4);
//输出[SPRING]
System.out.println(es5);
}
}
注意以上的集合都不线程安全,解决线程问题,我将过两天总结。
相关文章推荐
- 疯狂JAVA讲义---第七章(中):集合List,Queue
- 疯狂JAVA讲义---第七章(中):集合List,Queue(转)
- 疯狂JAVA讲义---第七章(下):集合Map(转)
- 疯狂JAVA讲义---第七章(下):集合Map
- [疯狂java讲义精粹] 第七章|泛型
- [疯狂Java]集合:SortedSet、TreeSet
- [疯狂Java]集合:EnumSet、各Set性能分析(选择)
- [疯狂Java讲义精粹] 第六章|Java集合
- [疯狂Java]集合:Set、HashSet、LinkedHashSet
- java集合之set集合的实现类HashSet源码分析
- Java常见集合框架(二):Iterable、Collection(List、Set、Queue)、AbstractCollection
- Java基础:循环迭代Array/List/Set/Map集合 增强for循环
- [疯狂Java]集合:专门用于聚集操作的一次性集合——Stream(流)
- Java 集合转换(数组、List、Set、Map相互转换)
- Java中set集合
- java--利用集合List和Set实现简易版学生选课系统
- java集合中:set与list相互转换
- Java集合面试题(02) Java中List和Set之间区别
- [转]java中的Set集合
- JAVA数据集合:Set与Get效率对比