Java基础十六
Posted xuweiweiwoaini
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java基础十六相关的知识,希望对你有一定的参考价值。
1 Set接口概述
- 一个不包含重复元素的Collection。
2 HashSet类
2.1 HashSet类的概述
- 它不保证Set的迭代顺序,特别是它不保证该顺序恒久不变。
- 底层数据结构是哈希表。
- 添加功能依赖于两个方法:
public int hashCode()
public boolean equals(Object obj)
2.2 HashSet的应用
- 应用:
package com.xuweiwei; import java.util.HashSet; import java.util.Iterator; import java.util.Set; /** * @author 许威威 * @version 1.0 */ public class SetDemo { public static void main(String[] args) { Set<String> set = new HashSet<>(); set.add("hello"); set.add("world"); for(Iterator<String> iterator = set.iterator();iterator.hasNext();){ String str = iterator.next(); System.out.println("str = " + str); } System.out.println("------------------------"); for (String s : set) { System.out.println("s = " + s); } } }
- 示例:
package com.xuweiwei; import java.util.Objects; /** * @author 许威威 * @version 1.0 */ public class Student { private String name; private Integer age; public Student() { } public Student(String name, Integer age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Student student = (Student) o; return Objects.equals(name, student.name) && Objects.equals(age, student.age); } @Override public int hashCode() { return Objects.hash(name, age); } @Override public String toString() { return "Student{" + "name=‘" + name + ‘‘‘ + ", age=" + age + ‘}‘; } }
package com.xuweiwei; import java.util.HashSet; import java.util.Set; /** * @author 许威威 * @version 1.0 */ public class SetDemo2 { public static void main(String[] args) { Set<Student> set = new HashSet<>(); set.add(new Student("张三",20)); set.add(new Student("李四",20)); set.add(new Student("张三",20)); set.add(new Student("李四",30)); set.add(new Student("王五",20)); set.add(new Student("王五",35)); set.add(new Student("王五",25)); for (Student student : set) { System.out.println("student = " + student); } } }
3 LinkedHashSet类
3.1 LinkedHashSet类概述
- 元素有序且唯一。
- 由链表保证元素有序。
- 由哈希表保证元素唯一。
3.2 LinkedHashSet的应用
- 示例:
package com.xuweiwei; import java.util.LinkedHashSet; import java.util.Set; /** * @author 许威威 * @version 1.0 */ public class LinkedHashSetDemo { public static void main(String[] args) { Set<String> set = new LinkedHashSet<>(); set.add("hello"); set.add("world"); set.add("java"); set.add("hello"); for (String s : set) { System.out.println("s = " + s); } } }
4 TreeSet类
4.1 TreeSet类概述
- 使用元素的自然顺序对元素进行排序(自然排序)。
- 可以根据创建set时候提供的Comparator进行排序(比较器排序)。
- 具体取决于使用的构造方法。
4.2 TreeSet是如何保证元素的排序和唯一性的
- 底层数据结构是红黑树。
4.3 TreeSet的应用
- 示例:自然排序
package com.xuweiwei; import java.util.Set; import java.util.TreeSet; /** * @author 许威威 * @version 1.0 */ public class TreeSetDemo { public static void main(String[] args) { Set<Integer> set = new TreeSet<>(); set.add(20); set.add(18); set.add(23); set.add(22); set.add(17); set.add(24); set.add(19); set.add(18); set.add(24); for (Integer i : set) { System.out.println("i = " + i); } } }
- 示例:TreeSet存储自定义对象并保证排序和唯一(自然排序)
package com.xuweiwei; /** * @author 许威威 * @version 1.0 */ public class Student implements Comparable<Student> { private String name; private Integer age; public Student() { } public Student(String name, Integer age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public int compareTo(Student o) { int num = this.getAge() - o.getAge(); return num == 0 ? this.getName().compareTo(o.getName()) : num; } @Override public String toString() { return "Student{" + "name=‘" + name + ‘‘‘ + ", age=" + age + ‘}‘; } }
package com.xuweiwei; import java.util.Set; import java.util.TreeSet; /** * @author 许威威 * @version 1.0 */ public class TreeSetDemo { public static void main(String[] args) { Set<Student> set = new TreeSet<>(); set.add(new Student("张三",20)); set.add(new Student("张三",30)); set.add(new Student("李四",20)); set.add(new Student("王五",90)); set.add(new Student("赵六",7)); set.add(new Student("田七",10)); set.add(new Student("王八",35)); for (Student student : set) { System.out.println("student = " + student); } } }
- 示例:比较器排序
package com.xuweiwei; /** * @author 许威威 * @version 1.0 */ public class Student { private String name; private Integer age; public Student() { } public Student(String name, Integer age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Student{" + "name=‘" + name + ‘‘‘ + ", age=" + age + ‘}‘; } }
package com.xuweiwei; import java.util.Comparator; import java.util.Set; import java.util.TreeSet; /** * @author 许威威 * @version 1.0 */ public class TreeSetDemo { public static void main(String[] args) { Set<Student> set = new TreeSet<>(new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { int num = o1.getAge() - o2.getAge(); return num == 0 ? o1.getName().compareTo(o2.getName()) : num; } }); set.add(new Student("张三", 20)); set.add(new Student("张三", 30)); set.add(new Student("李四", 20)); set.add(new Student("王五", 90)); set.add(new Student("赵六", 7)); set.add(new Student("田七", 10)); set.add(new Student("王八", 35)); for (Student student : set) { System.out.println("student = " + student); } } }
以上是关于Java基础十六的主要内容,如果未能解决你的问题,请参考以下文章
大数据必学Java基础(一百一十六):Application域监听器