j2ee_02_集合框架_02之Set集合

1. Set
1.1 特点:无序数据存入顺序与输出顺序不一致
对象不能重复 唯一:数据被过滤掉
1.2 遍历

		Set<Student> stus = new HashSet<Student>();
		stus.add(new Student(2, "hh", "boy"));
		stus.add(new Student(1, "dd", "boy"));
		stus.add(new Student(3, "ww", "boy"));
		stus.add(new Student(3, "ww", "girl"));
1.2.1 foreach
	for (Student student : stus) {
				System.out.println(student);
			}
1.2.2 迭代器 
		System.out.println("--------iterator----------");
		Iterator<Student> it = stus.iterator();
		while(it.hasNext()) {
			System.out.println(it.next());
		}

1.3 常用实现类
HashSet
TreeSet:根据某种(规则)对里面的元素进行排序

		System.out.println("------------TressSet---------------");
		Set<String> s = new TreeSet<String>();
		s.add("w");
		s.add("e");
		s.add("q");
		s.add("b");
		for (String str : s) {
			System.out.println(str);
		}
    自然比较接口: java.lang.Comparable 
	System.out.println("-----------java.lang.Comparable 中的排序----------------");
		Set<Student> tstu = new TreeSet<Student>();
		tstu.add(new Student(2, "hh", "boy"));
		tstu.add(new Student(1, "dd", "boy"));
		tstu.add(new Student(3, "ww", "boy"));
		for (Student student : tstu) {
			System.out.println(student);
		}
    比较器: java.util.Comparator
    字符串  String是以AscII码进行比较,返回差值

LinkedHashSet的特点
1)元素是有顺序的
2)元素是不重复的
3)底层数据结构是按照链表的结构存储的 Linked

集合框架的思维导图
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_43582260/article/details/91125835
今日推荐