如何仅使用其值之一从可拆分数组列表中删除元素?

时间:2018-10-06 19:45:51

标签: java android arraylist parcelable

我使用的是自定义对象的数组列表。让我们考虑一下用户的数组列表,其中每个用户都有例如userid,用户名.... etc。现在我只需要根据其ID删除用户,因为当我想删除用户时,它之前已经有所更改,因此无法使用用户对象擦除它。唯一要做的就是遍历列表中的所有用户,但是这个想法对我来说并不好。因此,我的问题是如何使用用户ID从用户数组列表中删除用户对象,而无需遍历列表中的所有用户?

2 个答案:

答案 0 :(得分:1)

此代码说明了您想要的一切。请注意如何覆盖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> {

    public static SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");

    private long userId;
    private String userName;
    private Date createdDate;

    public User(long userId, String userName, Date createdDate) {
        this.userId = userId;
        this.userName = userName;
        this.createdDate = createdDate;
    }

    public long getUserId() {
        return userId;
    }

    public void setUserId(long userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Date getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }

    public String toString() {
        return String.format("userId: %s, userName: %s, createdDate: %s",
                new Object[] { userId, userName, sdf.format(createdDate) });
    }

    public boolean equals(Object o) {
        if (o instanceof User) {
            return ((User) o).getUserId() == this.getUserId();
        }
        return false;
    }

    public int hashCode() {
        return new Long(userId).hashCode();
    }

    @Override
    public int compareTo(User o) {
        return this.getCreatedDate().compareTo(o.getCreatedDate());
    }

    public static void main(String args[]) throws Exception {
        Set<User> userSet = new HashSet<User>();
        userSet.add(new User(1, "Pushpesh", sdf.parse("16-02-2018")));
        userSet.add(new User(2, "Vikrant", sdf.parse("12-02-2018")));
        userSet.add(new User(3, "Abhay", sdf.parse("11-02-2018")));
        userSet.add(new User(4, "Komal", sdf.parse("18-02-2018")));

        userSet.stream().forEach(System.out::println);

        // this will remove user with id 2 no matter what his name and
        // createdDate are
        userSet.remove(new User(2, "Vikrant", sdf.parse("12-02-2018")));

        System.out.println("After removing userId 2");
        userSet.stream().forEach(System.out::println);

        System.out.println();

        List<User> userList = new ArrayList<User>(userSet);
        System.out.println("Before sorting");
        userList.stream().forEach(System.out::println);
        Collections.sort(userList); // This will sort based on date

        System.out.println("After sorting");
        userList.stream().forEach(System.out::println);

    }

}

希望这可以澄清您想要的所有内容。让我知道您是否还有任何疑问。

执行后,此代码将产生以下输出,

userId: 1, userName: Pushpesh, createdDate: 16-02-2018
userId: 2, userName: Vikrant, createdDate: 12-02-2018
userId: 3, userName: Abhay, createdDate: 11-02-2018
userId: 4, userName: Komal, createdDate: 18-02-2018
After removing userId 2
userId: 1, userName: Pushpesh, createdDate: 16-02-2018
userId: 3, userName: Abhay, createdDate: 11-02-2018
userId: 4, userName: Komal, createdDate: 18-02-2018

Before sorting
userId: 1, userName: Pushpesh, createdDate: 16-02-2018
userId: 3, userName: Abhay, createdDate: 11-02-2018
userId: 4, userName: Komal, createdDate: 18-02-2018
After sorting
userId: 3, userName: Abhay, createdDate: 11-02-2018
userId: 1, userName: Pushpesh, createdDate: 16-02-2018
userId: 4, userName: Komal, createdDate: 18-02-2018

答案 1 :(得分:-1)

这里是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)快完成

如果您希望能够快速拾取/删除项目(即O(log n)或),则应从一开始就使用Map / Hashmap / Dictionary(无论您用哪种语言来调用它们)。更好))。