为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

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

2 回答

?
莫回无

TA贡献1865条经验 获得超7个赞

此代码说明了您想要的一切。注意 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


查看完整回答
反对 回复 2021-11-03
?
侃侃无极

TA贡献2051条经验 获得超10个赞

这是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) 或更好)) .


查看完整回答
反对 回复 2021-11-03
  • 2 回答
  • 0 关注
  • 171 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信