- set集合特点
- set集合的遍历方式
- set集合的去重原理
- set集合排序
set集合特点
1.无序,没有修改方法;
2.不可重复
set集合的遍历方式
1.foreach
package collaction; import java.util.HashSet; import java.util.Iterator; public class sDemo1 { public static void main(String[] args) { HashSet hs=new HashSet<>(); hs.add("a"); hs.add("b"); hs.add("c"); //foreach for (Object obj : hs) { System.out.println(obj); } //iterator Iterator it=hs.iterator(); while(it.hasNext()) { System.out.println(it.next()); } } }
set集合的去重原理
1.根据set集合里存放的元素对象的hashCode方法和equals决定的
package com.zhulinjun; import java.util.HashSet; import java.util.Iterator; public class sDemo2 { public static void main(String[] args) { HashSet hs=new HashSet<>(); hs.add(new User(1, "zs")); hs.add(new User(2, "ls")); hs.add(new User(3, "ww")); System.out.println(hs.contains(new User(2,"ls")));//返回true } } class User{ private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "User [id=" + id + ", name=" + name + "]"; } public User(int id, String name) { super(); this.id = id; this.name = name; } public User() { super(); // TODO Auto-generated constructor stub } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + id; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; User other = (User) obj; if (id != other.id) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } }
set集合排序----应用场景
1.java.lang.comprable:自然排序接口
2.java.util.comprator:比较器排序接口
package com.zhulinjun; import java.util.Comparator; import java.util.HashSet; import java.util.Iterator; import java.util.TreeSet; public class sDemo3 { public static void main(String[] args) { HashSet hs=new HashSet<>(); hs.add(new Person(1,22,"zs",10000)); hs.add(new Person(2,32,"ls",18000)); hs.add(new Person(3,25,"ww",20000)); hs.add(new Person(4,23,"LL",100000)); hs.add(new Person(5,18,"wm",20000)); //默认排序 相当于调用别人的接口传递回来的数据 for (Object object : hs) { System.out.println(object); } TreeSet ts=new TreeSet<>(); //对数据加工 for (Object object : hs) { ts.add(object); } System.out.println("============默认看到的排序数据================"); for (Object object : ts) { System.out.println(object); } System.out.println("===============充值最多的排序数据=========================="); TreeSet tsPlus=new TreeSet<>(new Comparator<Person>() { @Override public int compare(Person o1, Person o2) { // TODO Auto-generated method stub return o2.getMoney()-o1.getMoney(); } }); for (Object object : hs) { tsPlus.add(object); } for (Object object : tsPlus) { System.out.println(object); } System.out.println("============充值最多并且最年轻的排序顺序============="); TreeSet tsPlusMax=new TreeSet<>(new Comparator<Person>() { @Override public int compare(Person o1, Person o2) { // TODO Auto-generated method stub int num=o2.getMoney()-o1.getMoney(); if(num==0) { return o1.getAge()-o2.getAge(); } return o2.getMoney()-o1.getMoney(); } }); for (Object object : hs) { tsPlusMax.add(object); } for (Object object : tsPlusMax) { System.out.println(object); } } } class Person implements Comparable<Person>{ private int id; private int age; private String name; private int money; public int getId() { return id; } public void setId(int id) { this.id = id; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getMoney() { return money; } public void setMoney(int money) { this.money = money; } @Override public String toString() { return "Person [id=" + id + ", age=" + age + ", name=" + name + ", money=" + money + "]"; } public Person(int id, int age, String name, int money) { super(); this.id = id; this.age = age; this.name = name; this.money = money; } public Person() { super(); // TODO Auto-generated constructor stub } @Override public int compareTo(Person o) { // TODO Auto-generated method stub return this.id-o.id; // return this.money-o.money; } }