版权声明:本文为博主原创文章,未经博主允许不得转载。
List特点:元素有放入顺序,元素可重复
Map特点:元素按键值对存储,无放入顺序
Set特点:元素无放入顺序,元素不可重复(注意:元素虽然无放入顺序,但是元素在set中的位置是有该元素的HashCode决定的,其位置其实是固定的)
新建一个实体类
- public class User {
- private String id;
- private String name;
- private String age;
- private String address;
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getAge() {
- return age;
- }
- public void setAge(String age) {
- this.age = age;
- }
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) {
- this.address = address;
- }
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + ((id == null) ? 0 : id.hashCode());
- result = prime * result + ((name == null) ? 0 : name.hashCode());
- result = prime * result + ((age == null) ? 0 : age.hashCode());
- result = prime * result + ((address == null) ? 0 : address.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 == null) {
- if (other.id != null) {
- return false;
- }
- } else if (!id.equals(other.id)) {
- return false;
- }
- if (name == null) {
- if (other.name != null) {
- return false;
- }
- } else if (!name.equals(other.name)) {
- return false;
- }
- if (age == null) {
- if (other.age != null) {
- return false;
- }
- } else if (!age.equals(other.age)) {
- return false;
- }
- if (address == null) {
- if (other.address != null) {
- return false;
- }
- } else if (!address.equals(other.address)) {
- return false;
- }
- return true;
- }
- }
调用即可
- private static List<User> removeListDuplicateObject(List<User> list) {
- System.out.println(Arrays.toString(list.toArray()));
- Set<User> set = new HashSet<User>();
- set.addAll(list);
- System.out.println(Arrays.toString(set.toArray()));
- List<User> listnewList = new ArrayList<User>(set);
- return listnewList;
- }
附上一个按某个字段排序功能
- @Override
- public int compareTo(DBBakPolicyDetail arg0) {
- if(this.getDbName() == null && arg0.getDbName() == null){
- return 0;
- }else if(this.getDbName() == null && arg0.getDbName() != null){
- return 1;
- }else if(this.getDbName() != null && arg0.getDbName() == null){
- return -1;
- }else {
- return this.getDbName().compareTo(arg0.getDbName());
- }
- }
- <span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.200000762939453px;">List特点:元素有放入顺序,元素可重复 </span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.200000762939453px;" /><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.200000762939453px;">Map特点:元素按键值对存储,无放入顺序 </span><br style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.200000762939453px;" /><p><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.200000762939453px;">Set特点:元素无放入顺序,元素不可重复(注意:元素虽然无放入顺序,但是元素在set中的位置是有该元素的HashCode决定的,其位置其实是固定的) </span></p><p><span style="font-family: Helvetica, Tahoma, Arial, sans-serif;"><span style="font-size: 14px; line-height: 25.200000762939453px;">新建一个实体类</span></span></p>
- public class User {
- private String id;
- private String name;
- private String age;
- private String address;
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getAge() {
- return age;
- }
- public void setAge(String age) {
- this.age = age;
- }
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) {
- this.address = address;
- }
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- result = prime * result + ((id == null) ? 0 : id.hashCode());
- result = prime * result + ((name == null) ? 0 : name.hashCode());
- result = prime * result + ((age == null) ? 0 : age.hashCode());
- result = prime * result + ((address == null) ? 0 : address.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 == null) {
- if (other.id != null) {
- return false;
- }
- } else if (!id.equals(other.id)) {
- return false;
- }
- if (name == null) {
- if (other.name != null) {
- return false;
- }
- } else if (!name.equals(other.name)) {
- return false;
- }
- if (age == null) {
- if (other.age != null) {
- return false;
- }
- } else if (!age.equals(other.age)) {
- return false;
- }
- if (address == null) {
- if (other.address != null) {
- return false;
- }
- } else if (!address.equals(other.address)) {
- return false;
- }
- return true;
- }
- }
之后调用即可
- private static List<User> removeListDuplicateObject(List<User> list) {
- System.out.println(Arrays.toString(list.toArray()));
- Set<User> set = new HashSet<User>();
- set.addAll(list);
- System.out.println(Arrays.toString(set.toArray()));
- List<User> listnewList = new ArrayList<User>(set);
- return listnewList;
- }
附上一个按某个字段排序的功能:
- @Override
- public int compareTo(DBBakPolicyDetail arg0) {
- if(this.getDbName() == null && arg0.getDbName() == null){
- return 0;
- }else if(this.getDbName() == null && arg0.getDbName() != null){
- return 1;
- }else if(this.getDbName() != null && arg0.getDbName() == null){
- return -1;
- }else {
- return this.getDbName().compareTo(arg0.getDbName());
- }
- }
List特点:元素有放入顺序,元素可重复
Map特点:元素按键值对存储,无放入顺序
Set特点:元素无放入顺序,元素不可重复(注意:元素虽然无放入顺序,但是元素在set中的位置是有该元素的HashCode决定的,其位置其实是固定的)
新建一个实体类