java中set集合的掌握

Set集合和List集合的区别?
  Set集合:不允许元素重复,唯一的(元素可以为null) ,不能保证迭代的顺序恒久不变(底层哈希表和hascode)
  无序(存储和取出不一致)
  List集合:允许元素重复,并且存储特点:有序性(存储和取出一致)

通过Set集合存储字符串并遍历

发现Set集合存储元素的时候,可以保证元素的唯一性,原因什么?
  看源码:
  HashSet集合的add方法底层依赖于双列集合HashMap,它依赖于两个方法,HashCode()方法和equals()方法
   先比较字符串的HashCode()码值一样,再比较equals()方法
  如果hasCode码值一样,还要比较内容是否相同,由于存储String,重写了equals()方法
  String本身重写了equals方法,所以不需要再重写了!
  让你用Set集合存储自定义对象遍历

public class SetDemo {
public static void main(String[] args) {
	Set<String> s=new HashSet<String>();
	s.add("hello");
	s.add("world");
	s.add("hello");
	s.add("java");
for(String s1:s) {
	System.out.println(s1);
}
}
}

注释:由此可知set集合不重复元素。

public class SetDemo2 {
public static void main(String[] args) {
	Set<Student> s=new HashSet<Student>();
	Student s1=new Student("薛之谦",30);
	Student s2=new Student("王富贵",29);
	Student s3=new Student("薛宝钗",20);
	Student s4=new Student("薛之谦",30);
	s.add(s1);
	s.add(s2);
	s.add(s3);
	s.add(s4);
	for(Student ss:s) {
		System.out.println(ss);
	}
}
}
如果在开发中,元素唯一性,并且还要保证元素有序(存储和取出一致),使用LinkedHashSet集合
  如果开发中要使用集合排序的问题,使用TreeSet集合(红黑树结构),下午分解...
   自然排序
   选择器排序
 LinkedHashSet集合:
  底层是一种链接列表和哈希表组成
  可以保证元素的唯一性,是由哈希表决定的(hashCode()和equals())

  可以保证元素的迭代顺序一致(有序),存储和取出一致,是由链表决定

举例:

public class LinkedHashSetDemo {
    public static void main(String[] args) { 
	LinkedHashSet<String> s=new LinkedHashSet<String>();
	s.add("world") ;
	s.add("hello") ;
	s.add("java") ;
	s.add("world") ;
	s.add("world") ;
	s.add("world") ;
	s.add("java") ;
	for(String ss:s) {
		System.out.println(ss);
	}
    } 
}
TreeSet集合模拟情况下是通过自然顺序对集合中的元素排序
  TreeSet:
   可以保证元素唯一并且元素排序(Integer类型的元素自然升序)
  自然排序
   比较器排序

  给TreeSet集合存储以下元素:20,23,22,18,17,19,24..

举例:

* @author 田伟
 *
 */
public class TreeSetDemo1 {
    public static void main(String[] args) {
	TreeSet<Integer> t=new TreeSet<Integer>();
    t.add(20);
    t.add(23);
    t.add(22);
    t.add(18);
    t.add(17);
    t.add(19);
    t.add(24);
  for(Integer i:t) {
	  System.out.println(i);
  }
}
}
TreeSet集合存储自定义对象(Student)

   按照姓名的长度从小到大进行比较 :主要条件

TreeSet集合的构造方式不同,使用的排序也不同
  自然排序:自定义的类实现Compareable接口,然后创建TreeSet对象,通过无参构造形式创建对象
  比较器排序 :public TreeSet(Comparator<E> comparator)
 
  两种方式:
  1)自定义一个类,该类实现Comparator接口,重写Comparator接口中的compare()方法
  2)直接使用接口匿名内部类的方式实现
 TreeSet集合保证元素唯一,是看返回值是否为0 
  保证元素进行排序,两种排序方式
 
 使用比较器排序的方式去,遍历Student对象,并且按照姓名长度进行比较

举例:

public class TreeSetDemo2 {
public static void main(String[] args) {
	TreeSet<Student> t=new TreeSet<Student>();
	Student s1 = new Student("gaoyuanyuan", 27) ;
	Student s2 = new Student("zhangguorong",29) ;
	Student s3 = new Student("wuqilong", 40) ;
	Student s4 = new Student("liushishi", 28) ; 
	Student s5 = new Student("fengqingy", 29) ;
	Student s6 = new Student("gaoyuanyuan", 22) ;
	Student s7 = new Student("gaoyuanyuan", 27) ;
	t.add(s1) ;
	t.add(s2) ;
	t.add(s3) ;
	t.add(s4) ;
	t.add(s5) ;
	t.add(s6) ;
	t.add(s7) ;
	for(Student s:t) {
		System.out.println(s);
	}
}
}

注释:学生类必须继承实现Comparable接口。


猜你喜欢

转载自blog.csdn.net/wt5264/article/details/80273132