如何从ArrayList中通过数据ID删除数据 [英] How to remove data from Arraylist by data id

查看:251
本文介绍了如何从ArrayList中通过数据ID删除数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在通过其ID从我的ArrayList中删除数据有问题。
我有我的一个ArrayList的书,我想删除基于其ID特定的书籍。
我想下面的code,但我的数据不会被删除。

请检查下面我code和提出解决方案吧。

更新
当我把这种方法,那么编译器读取所有低于code,但它不会从我的数组列表中删除我的数据。我没有得到任何错误。

  -------------------
  -------------------
   公共字符串removebookfrmSession()
    {
        清单< Bookdetails>书=新的ArrayList< Bookdetails>();
        串BOOKID =的request.getParameter(BKID);
        Bookdetails书= dao.listBookDetailsById(的Integer.parseInt(BOOKID));
        图书=(ArrayList的)session.get(BillTransactionBooksConstants.BOK);
        如果(书籍== NULL)的书=新的ArrayList< Bookdetails>();
        布尔ALREADY_EXISTS = FALSE;
        对于(Bookdetails乙:书)
        {
            如果(Integer.toString(b.getId())。等于(BOOKID))
            {
                ALREADY_EXISTS = TRUE;
                打破;
            }
        }
        如果(书= NULL&放大器;!&安培; ALREADY_EXISTS)
        {
            books.remove(书);
            的System.out.println(书大小+ books.size());
            session.put(BillTransactionBooksConstants.BOK,书籍);
        }
        返回成功;
    }

Bookdetails.java(POJO)

 包v.esoft.pojos;    //下午九时37分14秒由Hibernate工具生成3.4.0.CR1 2012年11月5日    进口java.util.Date;
    进口javax.persistence.Column;
    进口javax.persistence.Entity;
    进口javax.persistence.GeneratedValue;
    引入静态javax.persistence.GenerationType.IDENTITY;
    进口javax.persistence.Id;
    进口javax.persistence.Table;
    进口javax.persistence.Temporal;
    进口javax.persistence.TemporalType;    / **
     *通过调用:hbm2java生成Bookdetails
     * /
    @实体
    @Table(NAME =bookdetails,目录=vbsoftware)
    公共类Bookdetails实现java.io.Serializable {        私人整数ID;
        私人字符串ISBN;
        私人字符串bookTitile;
        私人字符串authFirstname;
        私人字符串authLastname;
        私人字符串editionYear;
        私人整数subjectId;
        私人整数coverId;
        私人整数languageId;
        私人字符串PUBLISHERNAME;
        私人整数editionId;
        私人浮动价格;
        私人字符串数量;
        私人字符串描述;
        私人整数locationId;
        私人字符串的言论;
        私人字符串IMG1;
        私人字符串IMG2;
        私人字符串videoUrl;
        私人整数createrId;
        私人日期createdDate;
        私人整数updateId;
        私人日期updatedDate;        公共Bookdetails(){
        }        公共Bookdetails(ISBN字符串,字符串bookTitile,字符串authFirstname,
                字符串authLastname,字符串editionYear,整数subjectId,
                整数coverId,整数languageId,字符串PUBLISHERNAME,
                整editionId,浮动价格,数量的字符串,
                字符串描述,整数locationId,字符串的话,
                字符串IMG1,IMG2字符串,字符串videoUrl,整数createrId,
                日期createdDate,整数updateId,日期updatedDate){
            this.isbn = ISBN;
            this.bookTitile = bookTitile;
            this.authFirstname = authFirstname;
            this.authLastname = authLastname;
            this.editionYear = editionYear;
            this.subjectId = subjectId;
            this.coverId = coverId;
            this.languageId = languageId;
            this.publisherName = PUBLISHERNAME;
            this.editionId = editionId;
            this.price =价格;
            this.quantity =数量;
            this.description =描述;
            this.locationId = locationId;
            this.remarks =言论;
            this.img1 = IMG1;
            this.img2 = IMG2;
            this.videoUrl = videoUrl;
            this.createrId = createrId;
            this.createdDate = createdDate;
            this.updateId = updateId;
            this.updatedDate = updatedDate;
        }        @ID
        @GeneratedValue(策略= IDENTITY)
        @Column(NAME =ID,独特= TRUE,可为空= FALSE)
        公共整数的getId(){
            返回this.id;
        }        公共无效SETID(整数ID){
            this.id = ID;
        }        @Column(NAME =书号,长度= 90)
        公共字符串getIsbn(){
            返回this.isbn;
        }        公共无效setIsbn(字符串ISBN){
            this.isbn = ISBN;
        }        @Column(NAME =book_titile)
        公共字符串getBookTitile(){
            返回this.bookTitile;
        }        公共无效setBookTitile(字符串bookTitile){
            this.bookTitile = bookTitile;
        }        @Column(NAME =auth_firstname,长度= 120)
        公共字符串getAuthFirstname(){
            返回this.authFirstname;
        }        公共无效setAuthFirstname(字符串authFirstname){
            this.authFirstname = authFirstname;
        }        @Column(NAME =auth_lastname,长度= 120)
        公共字符串getAuthLastname(){
            返回this.authLastname;
        }        公共无效setAuthLastname(字符串authLastname){
            this.authLastname = authLastname;
        }        @Column(NAME =edition_year,长度= 20)
        公共字符串getEditionYear(){
            返回this.editionYear;
        }        公共无效setEditionYear(字符串editionYear){
            this.editionYear = editionYear;
        }        @Column(NAME =subject_id)
        公共整数getSubjectId(){
            返回this.subjectId;
        }        公共无效setSubjectId(整数subjectId){
            this.subjectId = subjectId;
        }        @Column(NAME =cover_id)
        公共整数getCoverId(){
            返回this.coverId;
        }        公共无效setCoverId(整数coverId){
            this.coverId = coverId;
        }        @Column(NAME =LANGUAGE_ID)
        公共整数getLanguageId(){
            返回this.languageId;
        }        公共无效setLanguageId(整数languageId){
            this.languageId = languageId;
        }        @Column(NAME =PUBLISHER_NAME,长度= 70)
        公共字符串getPublisherName(){
            返回this.publisherName;
        }        公共无效setPublisherName(字符串PUBLISHERNAME){
            this.publisherName = PUBLISHERNAME;
        }        @Column(NAME =edition_id)
        公共整数getEditionId(){
            返回this.editionId;
        }        公共无效setEditionId(整数editionId){
            this.editionId = editionId;
        }        @Column(NAME =价,precision = 12,比例= 0)
        公众持股量用getPrice(){
            返回this.price;
        }        公共无效setPrice(浮动价){
            this.price =价格;
        }        @Column(NAME =量,长度= 40)
        公共字符串getQuantity(){
            返回this.quantity;
        }        公共无效setQuantity(字符串数量){
            this.quantity =数量;
        }        @Column(名称=说明,长度= 65535)
        公共字符串getDescription(){
            返回this.description;
        }        公共无效setDescription(字符串描述){
            this.description =描述;
        }        @Column(NAME =LOCATION_ID)
        公共整数getLocationId(){
            返回this.locationId;
        }        公共无效setLocationId(整数locationId){
            this.locationId = locationId;
        }        @Column(NAME =言论,长度= 65535)
        公共字符串getRemarks(){
            返回this.remarks;
        }        公共无效setRemarks(字符串的话){
            this.remarks =言论;
        }        @Column(NAME =IMG1)
        公共字符串getImg1(){
            返回this.img1;
        }        公共无效setImg1(字符串IMG1){
            this.img1 = IMG1;
        }        @Column(NAME =IMG2)
        公共字符串getImg2(){
            返回this.img2;
        }        公共无效setImg2(字符串IMG2){
            this.img2 = IMG2;
        }        @Column(NAME =VIDEO_URL,长度= 65535)
        公共字符串getVideoUrl(){
            返回this.videoUrl;
        }        公共无效setVideoUrl(字符串videoUrl){
            this.videoUrl = videoUrl;
        }        @Column(NAME =creater_id)
        公共整数getCreaterId(){
            返回this.createrId;
        }        公共无效setCreaterId(整数createrId){
            this.createrId = createrId;
        }        @Temporal(TemporalType.TIMESTAMP)
        @Column(NAME =CREATED_DATE,长度= 19)
        公开日期getCreatedDate(){
            返回this.createdDate;
        }        公共无效setCreatedDate(日期createdDate){
            this.createdDate = createdDate;
        }        @Column(NAME =UPDATE_ID)
        公共整数getUpdateId(){
            返回this.updateId;
        }        公共无效setUpdateId(整数updateId){
            this.updateId = updateId;
        }        @Temporal(TemporalType.TIMESTAMP)
        @Column(NAME =updated_date,长度= 19)
        公开日期getUpdatedDate(){
            返回this.updatedDate;
        }        公共无效setUpdatedDate(日期updatedDate){
            this.updatedDate = updatedDate;
        }    }


解决方案

Bookdetails 类必须实现公共布尔等于(对象o),以使用列表#删除(对象o)方法的方法。否则,你应该处理自己的对象去除由@juergend说。

实施等于的方法是这样的:

 公共类Bookdetails {
    私人诠释身份证;
    //其他属性和方法...    @覆盖
    公共布尔等于(对象o){
        如果(邻的instanceof Bookdetails){
            Bookdetails oBookdetails =(Bookdetails)O;
            回报(this.id == oBookdetails.id);
        }
        返回false;
    }
}

现在的 books.remove(书); 无需会工作为(Bookdetails乙:书)循环。

I am having problem in removing data from my arraylist by its id. I have a books in my arraylist and i want to remove a particular books based on its id. I tried the below code but my data is not removing.

Please check my code below and suggest a solution for it.

Update When i calls this method then compiler reads all the below code but it does not removes my data from my arraylist. I am not getting any error.

  -------------------
  -------------------
   public String removebookfrmSession()
    {  
        List<Bookdetails> books = new ArrayList<Bookdetails>();
        String bookid = request.getParameter("bkid");    
        Bookdetails book = dao.listBookDetailsById(Integer.parseInt(bookid));
        books = (ArrayList) session.get(BillTransactionBooksConstants.BOK);
        if ( books == null ) books = new ArrayList<Bookdetails>();
        boolean already_exists = false;
        for ( Bookdetails b : books ) 
        {
            if ( Integer.toString(b.getId()).equals(bookid))
            {
                already_exists = true; 
                break;
            }
        }
        if (book != null && already_exists  ) 
        { 
            books.remove(book);
            System.out.println("books size"+books.size()); 
            session.put(BillTransactionBooksConstants.BOK,books);
        }
        return SUCCESS;
    } 

Bookdetails.java (POJO)

        package v.esoft.pojos;

    // Generated Nov 5, 2012 9:37:14 PM by Hibernate Tools 3.4.0.CR1

    import java.util.Date;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import static javax.persistence.GenerationType.IDENTITY;
    import javax.persistence.Id;
    import javax.persistence.Table;
    import javax.persistence.Temporal;
    import javax.persistence.TemporalType;

    /**
     * Bookdetails generated by hbm2java
     */
    @Entity
    @Table(name = "bookdetails", catalog = "vbsoftware")
    public class Bookdetails implements java.io.Serializable {

        private Integer id;
        private String isbn;
        private String bookTitile;
        private String authFirstname;
        private String authLastname;
        private String editionYear;
        private Integer subjectId;
        private Integer coverId;
        private Integer languageId;
        private String publisherName;
        private Integer editionId;
        private Float price;
        private String quantity;
        private String description;
        private Integer locationId;
        private String remarks;
        private String img1;
        private String img2;
        private String videoUrl;
        private Integer createrId;
        private Date createdDate;
        private Integer updateId;
        private Date updatedDate;

        public Bookdetails() {
        }

        public Bookdetails(String isbn, String bookTitile, String authFirstname,
                String authLastname, String editionYear, Integer subjectId,
                Integer coverId, Integer languageId, String publisherName,
                Integer editionId, Float price, String quantity,
                String description, Integer locationId, String remarks,
                String img1, String img2, String videoUrl, Integer createrId,
                Date createdDate, Integer updateId, Date updatedDate) {
            this.isbn = isbn;
            this.bookTitile = bookTitile;
            this.authFirstname = authFirstname;
            this.authLastname = authLastname;
            this.editionYear = editionYear;
            this.subjectId = subjectId;
            this.coverId = coverId;
            this.languageId = languageId;
            this.publisherName = publisherName;
            this.editionId = editionId;
            this.price = price;
            this.quantity = quantity;
            this.description = description;
            this.locationId = locationId;
            this.remarks = remarks;
            this.img1 = img1;
            this.img2 = img2;
            this.videoUrl = videoUrl;
            this.createrId = createrId;
            this.createdDate = createdDate;
            this.updateId = updateId;
            this.updatedDate = updatedDate;
        }

        @Id
        @GeneratedValue(strategy = IDENTITY)
        @Column(name = "id", unique = true, nullable = false)
        public Integer getId() {
            return this.id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        @Column(name = "isbn", length = 90)
        public String getIsbn() {
            return this.isbn;
        }

        public void setIsbn(String isbn) {
            this.isbn = isbn;
        }

        @Column(name = "book_titile")
        public String getBookTitile() {
            return this.bookTitile;
        }

        public void setBookTitile(String bookTitile) {
            this.bookTitile = bookTitile;
        }

        @Column(name = "auth_firstname", length = 120)
        public String getAuthFirstname() {
            return this.authFirstname;
        }

        public void setAuthFirstname(String authFirstname) {
            this.authFirstname = authFirstname;
        }

        @Column(name = "auth_lastname", length = 120)
        public String getAuthLastname() {
            return this.authLastname;
        }

        public void setAuthLastname(String authLastname) {
            this.authLastname = authLastname;
        }

        @Column(name = "edition_year", length = 20)
        public String getEditionYear() {
            return this.editionYear;
        }

        public void setEditionYear(String editionYear) {
            this.editionYear = editionYear;
        }

        @Column(name = "subject_id")
        public Integer getSubjectId() {
            return this.subjectId;
        }

        public void setSubjectId(Integer subjectId) {
            this.subjectId = subjectId;
        }

        @Column(name = "cover_id")
        public Integer getCoverId() {
            return this.coverId;
        }

        public void setCoverId(Integer coverId) {
            this.coverId = coverId;
        }

        @Column(name = "language_id")
        public Integer getLanguageId() {
            return this.languageId;
        }

        public void setLanguageId(Integer languageId) {
            this.languageId = languageId;
        }

        @Column(name = "publisher_name", length = 70)
        public String getPublisherName() {
            return this.publisherName;
        }

        public void setPublisherName(String publisherName) {
            this.publisherName = publisherName;
        }

        @Column(name = "edition_id")
        public Integer getEditionId() {
            return this.editionId;
        }

        public void setEditionId(Integer editionId) {
            this.editionId = editionId;
        }

        @Column(name = "price", precision = 12, scale = 0)
        public Float getPrice() {
            return this.price;
        }

        public void setPrice(Float price) {
            this.price = price;
        }

        @Column(name = "quantity", length = 40)
        public String getQuantity() {
            return this.quantity;
        }

        public void setQuantity(String quantity) {
            this.quantity = quantity;
        }

        @Column(name = "description", length = 65535)
        public String getDescription() {
            return this.description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        @Column(name = "location_id")
        public Integer getLocationId() {
            return this.locationId;
        }

        public void setLocationId(Integer locationId) {
            this.locationId = locationId;
        }

        @Column(name = "remarks", length = 65535)
        public String getRemarks() {
            return this.remarks;
        }

        public void setRemarks(String remarks) {
            this.remarks = remarks;
        }

        @Column(name = "img1")
        public String getImg1() {
            return this.img1;
        }

        public void setImg1(String img1) {
            this.img1 = img1;
        }

        @Column(name = "img2")
        public String getImg2() {
            return this.img2;
        }

        public void setImg2(String img2) {
            this.img2 = img2;
        }

        @Column(name = "video_url", length = 65535)
        public String getVideoUrl() {
            return this.videoUrl;
        }

        public void setVideoUrl(String videoUrl) {
            this.videoUrl = videoUrl;
        }

        @Column(name = "creater_id")
        public Integer getCreaterId() {
            return this.createrId;
        }

        public void setCreaterId(Integer createrId) {
            this.createrId = createrId;
        }

        @Temporal(TemporalType.TIMESTAMP)
        @Column(name = "created_date", length = 19)
        public Date getCreatedDate() {
            return this.createdDate;
        }

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

        @Column(name = "update_id")
        public Integer getUpdateId() {
            return this.updateId;
        }

        public void setUpdateId(Integer updateId) {
            this.updateId = updateId;
        }

        @Temporal(TemporalType.TIMESTAMP)
        @Column(name = "updated_date", length = 19)
        public Date getUpdatedDate() {
            return this.updatedDate;
        }

        public void setUpdatedDate(Date updatedDate) {
            this.updatedDate = updatedDate;
        }

    }

解决方案

Your Bookdetails class must implement the public boolean equals(Object o) method in order to use the List#remove(Object o) method. Otherwise, you should handle the object removal by yourself as stated by @juergend.

Implementing the equals method would be like this:

public class Bookdetails {
    private int id;
    //other attributes and methods...

    @Override
    public boolean equals(Object o) {
        if (o instanceof Bookdetails) {
            Bookdetails oBookdetails = (Bookdetails)o;
            return (this.id == oBookdetails.id);
        }
        return false;
    }
}

Now the books.remove(book); would work without need of the for ( Bookdetails b : books ) loop.

这篇关于如何从ArrayList中通过数据ID删除数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆