HashSet
1.Set集合是Collection的子接口,该集合中元素是唯一的,无序的。
2.HashSet不能保证元素迭代顺序恒久不变,底层是hash表(基于HashMap)。
3.无序性:存储和取出不一致。
4.哈希表能够保证元素的唯一:
依赖于:public int hashCode()
public boolean equals(Object e)
package ;import java.util.HashSet;public class SetTest {public static void main(String[] args) {//Set是一个接口,不能自己创建对象,要通过子实现类实例化HashSet<String> set = new HashSet<String>();set.add("hello");set.add("world");set.add("java");set.add("java");set.add("php");set.add("python");set.add("python");//返回 set 的哈希码值。int code = set.hashCode();System.out.println(code);//遍历,set有自动取重的功能for (String s : set) {System.out.println(s+"---"+s.length());}}}
TreeSet
1.TreeSet集合基于TreeMap的一个红黑树结构(Red-Black-Tree):其实就是一种二叉树
2.根据其构造方法:有自然排序和构造器排序。
例:自然排序
package ;import java.util.TreeSet;//自然排序public class TreeSetTest {public static void main(String[] args) {TreeSet<Integer> it = new TreeSet<Integer>();it.add(12);it.add(34);it.add(3);it.add(56);it.add(2);it.add(17);//遍历,使用增强forfor (Integer inte : it) {System.out.println(inte);}}}
选择器排序:
需求:自定义一个学生类,TreeSet集合存储自定义对象必须按照条件进行排序,并且这里自定类型implements Comparable接口(按照年龄从小到大进行排序)
package ;import java.util.TreeSet;public class TreeSetDemo {public static void main(String[] args) {TreeSet<Student> set = new TreeSet<Student>();set.add(new Student("高圆圆",39));set.add(new Student("赵又廷",45));set.add(new Student("吴倩",30));set.add(new Student("吴倩",32));set.add(new Student("张新城",32));set.add(new Student("高圆圆",40));for (Student stu : set) {System.out.println(stu.getName()+"--"+stu.getAge());}}}package ;public class Student implements Comparable<Student> {private String name;private Integer age;public Student() {super();}public Student(String name, Integer age) {super();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;}@Overridepublic String toString() {return "Student [name=" + name + ", age=" + age + "]";}@Overridepublic boolean equals(Object obj) {if (this == obj)return true;if (obj == null)return false;if (getClass() != obj.getClass())return false;Student other = (Student) obj;if (age == null) {if (other.age != null)return false;} else if (!age.equals(other.age))return false;if (name == null) {if (other.name != null)return false;} else if (!name.equals(other.name))return false;return true;}@Overridepublic int compareTo(Student s) {int num=this.age-s.age;int num1=(num==0)?(pareTo(s.name)):num;return num1;}}
Map集合
1.Map集合:是一个双列集合,能够存储两种引用类型(键和值:是一种键值对存在),理解为"夫妻对"
HashMap<K,V>,TreeMap<K,V>
2.集合的遍历
map集合方式1 的遍历:
V get(Object key):通过键获取值
Set keySet():获取Map集合中所有的键的集合
Map集合方式2遍历
Set<Map.Entry<K,V>> entrySet():获取所有的键值对对象
Map.Entry<K,V>:接口:代表键值对对象
K getKey():获取键
V getValue():获取值
3.HashMap:里面有哈希表结构,能够保证元素的唯一
package ;import java.util.HashMap;import java.util.Map;import java.util.Map.Entry;import java.util.Set;public class MapTest {public static void main(String[] args) {Map<Integer, String> map = new HashMap<Integer, String>();map.put(1, "黄蓉");map.put(2, "郭靖");map.put(3, "吴倩");map.put(4, "张新城");map.put(5, "孙俪");map.put(6, "邓超");//遍历//第一种:增强for//首先获取它的键Set<Integer> set = map.keySet();for (Integer it : set) {//通过键获取值String str = map.get(it);System.out.println(it+":"+str);}System.out.println("----------------------------");//第二种:遍历方式//获取集合中所有键值对对象Set<Entry<Integer, String>> st = map.entrySet();for (Entry<Integer, String> en : st) {//通过键值对获取键Integer ks = en.getKey();//通过键值对获取值String value = en.getValue();System.out.println(ks+":"+value);}}}
TreeMap
需求:使用TreeMap集合进行排序,TreeMap<Student,String> 按照学生的年龄从小到大排序
注意:构造方法
TreeMap():空参构造自然排序---->Comparable
public int compareTo(T t)
构造方法:有参构造TreeMap(Comparator comparator)选择器排序---->Comparator
public int compares(T t1,T t2)
package ;import java.util.Set;import java.util.TreeMap;public class TreeMapTest {public static void main(String[] args) {TreeMap<Student1, String> tm = new TreeMap<Student1, String>();tm.put(new Student1("鹿晗", 28, "男"), "《重返20岁》");tm.put(new Student1("黄圣依", 30, "女"), "《天仙配》");tm.put(new Student1("刘诗诗", 32, "女"), "《步步惊心》");tm.put(new Student1("迪丽热巴", 28, "女"), "《三生三世十里桃花》");tm.put(new Student1("郑爽", 29, "女"), "《微微一笑很倾城》");tm.put(new Student1("Angelababy", 31, "女"), "《微微一笑很倾城》");tm.put(new Student1("王源", 20, "男"), "《十七》");//遍历Set<Student1> ks = tm.keySet();for (Student1 stu : ks) {String str = tm.get(stu);System.out.println(stu.getName()+", "+stu.getAge()+", "+stu.getSex()+""+"="+str);}}}package ;public class Student1 implements Comparable<Student1> {private String name;private Integer age;private String sex;public Student1() {super();}public Student1(String name, Integer age, String sex) {super();this.name = name;this.age = age;this.sex = sex;}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;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}@Overridepublic String toString() {return "Student1 [name=" + name + ", age=" + age + ", sex=" + sex + "]";}@Overridepublic boolean equals(Object obj) {if (this == obj)return true;if (obj == null)return false;if (getClass() != obj.getClass())return false;Student1 other = (Student1) obj;if (age == null) {if (other.age != null)return false;} else if (!age.equals(other.age))return false;if (name == null) {if (other.name != null)return false;} else if (!name.equals(other.name))return false;if (sex == null) {if (other.sex != null)return false;} else if (!sex.equals(other.sex))return false;return true;}@Overridepublic int compareTo(Student1 s) {int num=this.age-s.age;int num1=(num==0)?(pareTo(s.name)):num;return num1;}}
HashMap
需求:HashMap<String,Student>:键是字符串,存储的值是自定义对象
键:学生编号使用String
值:学生对象 (姓名和年龄)
package ;import java.util.HashMap;import java.util.Set;public class HashMapTest {public static void main(String[] args) {HashMap<String, Student> hm = new HashMap<String, Student>();hm.put("001", new Student("赵丽颖",32,"女"));hm.put("002", new Student("万茜",30,"女"));hm.put("003", new Student("刘诗诗",33,"女"));hm.put("004", new Student("邓超",34,"男"));hm.put("005", new Student("邓超",34,"男"));hm.put("006", new Student("张学诚",33,"男"));hm.put("007", new Student("吴倩",32,"女"));hm.put("008", new Student("赵丽颖",32,"女"));//遍历//先得到它的键Set<String> ks = hm.keySet();for (String str : ks) {//根据它的键得到它的值Student st = hm.get(str);System.out.println(str+"="+st.getName()+"--"+st.getAge()+"--"+st.getSex());}}}package ;public class Student {private String name;private Integer age;private String sex;public Student() {super();}public Student(String name, Integer age, String sex) {super();this.name = name;this.age = age;this.sex = sex;}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;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}@Overridepublic int hashCode() {final int prime = 31;int result = 1;result = prime * result + ((age == null) ? 0 : age.hashCode());result = prime * result + ((name == null) ? 0 : name.hashCode());result = prime * result + ((sex == null) ? 0 : sex.hashCode());return result;}@Overridepublic boolean equals(Object obj) {if (this == obj)return true;if (obj == null)return false;if (getClass() != obj.getClass())return false;Student other = (Student) obj;if (age == null) {if (other.age != null)return false;} else if (!age.equals(other.age))return false;if (name == null) {if (other.name != null)return false;} else if (!name.equals(other.name))return false;if (sex == null) {if (other.sex != null)return false;} else if (!sex.equals(other.sex))return false;return true;}@Overridepublic String toString() {return "Student [name=" + name + ", age=" + age + ", sex=" + sex + "]";}}