如何仅使用其值之一从parcelable数组列表中擦除元素?

我正在使用我拥有的自定义对象的数组列表。让我们将其视为一个用户数组列表,其中每个用户都有例如用户 ID、用户名...等。现在我只需要根据其 id 删除一个用户,因为当我想删除该用户时,其中的某些内容之前已更改,因此我无法使用 user 对象擦除它。我必须这样做的唯一想法是遍历列表中的所有用户,但这个想法对我来说并不好。所以我的问题是如何从使用用户 ID 的用户数组列表中删除用户对象,而无需遍历列表中的所有用户?


PIPIONE
浏览 172回答 2
2回答

莫回无

此代码说明了您想要的一切。注意 hashCode、equals 和 compareTo 方法是如何被覆盖的。package com.pkr.test;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Collections;import java.util.Date;import java.util.HashSet;import java.util.List;import java.util.Set;public class User implements Comparable<User> {&nbsp; &nbsp; public static SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");&nbsp; &nbsp; private long userId;&nbsp; &nbsp; private String userName;&nbsp; &nbsp; private Date createdDate;&nbsp; &nbsp; public User(long userId, String userName, Date createdDate) {&nbsp; &nbsp; &nbsp; &nbsp; this.userId = userId;&nbsp; &nbsp; &nbsp; &nbsp; this.userName = userName;&nbsp; &nbsp; &nbsp; &nbsp; this.createdDate = createdDate;&nbsp; &nbsp; }&nbsp; &nbsp; public long getUserId() {&nbsp; &nbsp; &nbsp; &nbsp; return userId;&nbsp; &nbsp; }&nbsp; &nbsp; public void setUserId(long userId) {&nbsp; &nbsp; &nbsp; &nbsp; this.userId = userId;&nbsp; &nbsp; }&nbsp; &nbsp; public String getUserName() {&nbsp; &nbsp; &nbsp; &nbsp; return userName;&nbsp; &nbsp; }&nbsp; &nbsp; public void setUserName(String userName) {&nbsp; &nbsp; &nbsp; &nbsp; this.userName = userName;&nbsp; &nbsp; }&nbsp; &nbsp; public Date getCreatedDate() {&nbsp; &nbsp; &nbsp; &nbsp; return createdDate;&nbsp; &nbsp; }&nbsp; &nbsp; public void setCreatedDate(Date createdDate) {&nbsp; &nbsp; &nbsp; &nbsp; this.createdDate = createdDate;&nbsp; &nbsp; }&nbsp; &nbsp; public String toString() {&nbsp; &nbsp; &nbsp; &nbsp; return String.format("userId: %s, userName: %s, createdDate: %s",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Object[] { userId, userName, sdf.format(createdDate) });&nbsp; &nbsp; }&nbsp; &nbsp; public boolean equals(Object o) {&nbsp; &nbsp; &nbsp; &nbsp; if (o instanceof User) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return ((User) o).getUserId() == this.getUserId();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }&nbsp; &nbsp; public int hashCode() {&nbsp; &nbsp; &nbsp; &nbsp; return new Long(userId).hashCode();&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public int compareTo(User o) {&nbsp; &nbsp; &nbsp; &nbsp; return this.getCreatedDate().compareTo(o.getCreatedDate());&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String args[]) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; Set<User> userSet = new HashSet<User>();&nbsp; &nbsp; &nbsp; &nbsp; userSet.add(new User(1, "Pushpesh", sdf.parse("16-02-2018")));&nbsp; &nbsp; &nbsp; &nbsp; userSet.add(new User(2, "Vikrant", sdf.parse("12-02-2018")));&nbsp; &nbsp; &nbsp; &nbsp; userSet.add(new User(3, "Abhay", sdf.parse("11-02-2018")));&nbsp; &nbsp; &nbsp; &nbsp; userSet.add(new User(4, "Komal", sdf.parse("18-02-2018")));&nbsp; &nbsp; &nbsp; &nbsp; userSet.stream().forEach(System.out::println);&nbsp; &nbsp; &nbsp; &nbsp; // this will remove user with id 2 no matter what his name and&nbsp; &nbsp; &nbsp; &nbsp; // createdDate are&nbsp; &nbsp; &nbsp; &nbsp; userSet.remove(new User(2, "Vikrant", sdf.parse("12-02-2018")));&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("After removing userId 2");&nbsp; &nbsp; &nbsp; &nbsp; userSet.stream().forEach(System.out::println);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; List<User> userList = new ArrayList<User>(userSet);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Before sorting");&nbsp; &nbsp; &nbsp; &nbsp; userList.stream().forEach(System.out::println);&nbsp; &nbsp; &nbsp; &nbsp; Collections.sort(userList); // This will sort based on date&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("After sorting");&nbsp; &nbsp; &nbsp; &nbsp; userList.stream().forEach(System.out::println);&nbsp; &nbsp; }}希望这能澄清你想要的一切。如果您有任何疑问,请告诉我。执行后,此代码产生以下输出,userId: 1, userName: Pushpesh, createdDate: 16-02-2018userId: 2, userName: Vikrant, createdDate: 12-02-2018userId: 3, userName: Abhay, createdDate: 11-02-2018userId: 4, userName: Komal, createdDate: 18-02-2018After removing userId 2userId: 1, userName: Pushpesh, createdDate: 16-02-2018userId: 3, userName: Abhay, createdDate: 11-02-2018userId: 4, userName: Komal, createdDate: 18-02-2018Before sortinguserId: 1, userName: Pushpesh, createdDate: 16-02-2018userId: 3, userName: Abhay, createdDate: 11-02-2018userId: 4, userName: Komal, createdDate: 18-02-2018After sortinguserId: 3, userName: Abhay, createdDate: 11-02-2018userId: 1, userName: Pushpesh, createdDate: 16-02-2018userId: 4, userName: Komal, createdDate: 18-02-2018

侃侃无极

这是javascript中的示例,但可以使用任何类型的字典/地图轻松将其重写为java。people = new Map()user = {id:"das", name: "Darek"}people.set(user.id, user)user = {id:"aaa", name: "Czarrek"}people.set(user.id, user)user = {id:"bbb", name: "Marek"}people.set(user.id, user)console.log(people.get("das"), people.get("aaa"), people.get("bbb"), people.size);people.delete('aaa');console.log(people.get("das"), people.get("aaa"), people.get("bbb"), people.size);映射添加在 O(1) 中工作(对于哈希映射),删除也在 O(1) 中。如果您选择在您的语言中使用的数据结构是使用某种树之王实现的,那么这两个操作都在 O(log(n)) 中运行(可能)。如果只有随机排序的列表并且没有额外的数据,那么唯一的选择就是查看所有列表的项目。它不能比 O(n) 快如果您希望能够快速选择/删除项目,您应该从一开始就使用 Map/Hashmap/Dictionary(在您的语言中如何调用它们无关紧要)(即 O(log n) 或更好)) .
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java