1.無(wú)序(添加和取出的順序不一致)
2.不允許重復(fù)元素,所以最多包含一個(gè)null
3.JDK API中的Set接口的實(shí)現(xiàn)類(lèi)有很多,主要有TreeSet和HashSet兩個(gè)
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class Demo01 {public static void main(String[] args) {Set set = new HashSet();
set.add("jack");
set.add("lucy");
set.add("john");
set.add("jack");
set.add(null);
System.out.println(set);
//迭代器遍歷
Iterator iterator = set.iterator();
while (iterator.hasNext()) {Object next = iterator.next();
System.out.println(next);
}
//增強(qiáng)for循環(huán)
for (Object o: set) {System.out.println(o);
}
//set接口對(duì)象,不能通過(guò)索引來(lái)獲取,因此不能用普通for循環(huán)來(lái)遍歷
}
}
2. HashSet 集合
2.1 底層實(shí)現(xiàn)import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
public class Demo01 {public static void main(String[] args) {Employee employee1 = new Employee("曹操",42,new Employee.MyDate(112,4,7));
Employee employee2 = new Employee("劉備",33,new Employee.MyDate(121,7,9));
Employee employee3 = new Employee("劉備",33,new Employee.MyDate(121,7,9));
HashSet hashSet = new HashSet();
hashSet.add(employee1);
hashSet.add(employee2);
hashSet.add(employee3);
System.out.println(hashSet);
System.out.println(hashSet);
}
static class Employee{private String name;
private int age;
private MyDate birthday;
public Employee(String name, int age, MyDate birthday) {this.name = name;
this.age = age;
this.birthday = birthday;
}
public String getName() {return name;
}
public void setName(String name) {this.name = name;
}
public int getAge() {return age;
}
public void setAge(int age) {this.age = age;
}
public MyDate getBirthday() {return birthday;
}
public void setBirthday(MyDate birthday) {this.birthday = birthday;
}
@Override
public boolean equals(Object o) {if (this == o) return true;
if (!(o instanceof Employee)) return false;
Employee employee = (Employee) o;
return age == employee.age && Objects.equals(name, employee.name) && Objects.equals(birthday, employee.birthday);
}
@Override
public int hashCode() {return Objects.hash(name, age, birthday);
}
@Override
public String toString() {return "Employee{" +
"name='" + name + '\'' +
", age=" + age +
", birthday=" + birthday +
'}';
}
public static class MyDate {int year;
int month;
int day;
public MyDate(int year, int month, int day) {this.year = year;
this.month = month;
this.day = day;
}
@Override
public boolean equals(Object o) {if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MyDate myDate = (MyDate) o;
return year == myDate.year && month == myDate.month && day == myDate.day;
}
@Override
public int hashCode() {return Objects.hash(year, month, day);
}
@Override
public String toString() {return year +
"年" + month +
"月" + day +
"日";
}
}
}
}
3. LinkedHashSet 集合在LinkHashSet中維護(hù)了一個(gè)hash表和雙向鏈表,不同于hashSet中的鏈表,LinkHashSet中的鏈表是可以跨數(shù)組元素變量空間的雙向鏈表
雙向鏈表是有head和tail的,它的順序是我們添加元素的順序,這樣我們遍歷LinkedHashSet時(shí),就能使遍歷順序和插入順序一致。
import java.util.LinkedHashSet;
import java.util.Objects;
public class Demo01 {public static void main(String[] args) {LinkedHashSet linkedHashSet = new LinkedHashSet();
linkedHashSet.add(new Car("A8",700000));
linkedHashSet.add(new Car("A8",700000));
linkedHashSet.add(new Car("C100",1100000));
linkedHashSet.add(new Car("AE86",100000));
System.out.println(linkedHashSet);
}
}
class Car{private String name;
private int price;
public Car(String name, int price) {this.name = name;
this.price = price;
}
public String getName() {return name;
}
public void setName(String name) {this.name = name;
}
public int getPrice() {return price;
}
public void setPrice(int price) {this.price = price;
}
@Override
public boolean equals(Object o) {if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Car car = (Car) o;
return price == car.price && Objects.equals(name, car.name);
}
@Override
public int hashCode() {return Objects.hash(name, price);
}
@Override
public String toString() {return "Car{" +
"name='" + name + '\'' +
", price=" + price +
'}';
}
}
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧