【问题标题】:detect dulpicate elements in multi column arraylist in android/java?检测 android/java 中多列数组列表中的重复元素?
【发布时间】:2014-08-23 11:49:15
【问题描述】:

我有一个下面提到的数组列表结构。

ArrayList<Arraylisttableitem> tablestatuslist = new ArrayList<Arraylisttableitem>();

下面提到了这个数组列表的模型类。

public class Arraylisttableitem {

    String total, noofcustomer, firstname, tab_name, balance, time;

    public Arraylisttableitem(String total, String noofcustomer,
            String firstname, String tab_name, String balance, String time) {
        // TODO Auto-generated constructor stub
        this.total = total;
        this.noofcustomer = noofcustomer;
        this.firstname = firstname;
        this.tab_name = tab_name;
        this.balance = balance;
        this.time = time;
    }

    public String getTotal() {
        return total;
    }

    public void setTotal(String total) {
        this.total = total;
    }

    public String getNoofcustomer() {
        return noofcustomer;
    }

    public void setNoofcustomer(String noofcustomer) {
        this.noofcustomer = noofcustomer;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getTab_name() {
        return tab_name;
    }

    public void setTab_name(String tab_name) {
        this.tab_name = tab_name;
    }

    public String getBalance() {
        return balance;
    }

    public void setBalance(String balance) {
        this.balance = balance;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }
}

在名为 tab_name 的元素中,我可能有重复的元素。我想识别重复的元素并添加所有重复的值。示例 tab_num 10 的余额为 5,而 tab_num 10 的余额为 10。我想要的是 tab_nam 10 应该出现一次,并且余额应该是 15,并且列表中没有任何重复值。

请解决我的问题...

我不知道在里面写什么..

for(Arraylisttableitem itemlist : tablestatuslist)
                    {

                    }

我的 JSON 有重复值。所以,我的数组列表支持重复值。

【问题讨论】:

  • 当您将对象添加到数组列表时,为什么不处理这种情况,而不是稍后删除重复项?
  • @ShivamVerma 这会在每次添加数据时减慢整个操作。

标签: java android collections arraylist core


【解决方案1】:

如果您想添加值,那么最好将字段类型保留为 int 而不是 string。但是,在您的情况下,您可以这样做

Map<String, Arraylisttableitem> map = new HashMap<String, Arraylisttableitem>();
for(Arraylisttableitem item : tableStatusList){
    Arraylisttableitem prevItem = map.get(item.getTab_name());
    if(prevItem != null){
        prevItem.setTotal(String.valueOf(Integer.parseInt(item.getTotal()) + Integer.parseInt(prevItem.getTotal())));
        prevItem.setBalance(String.valueOf(Integer.parseInt(item.getBalance()) + Integer.parseInt(prevItem.getBalance())));
        prevItem.setNoofcustomer(String.valueOf(Integer.parseInt(item.getNoofcustomer()) + Integer.parseInt(prevItem.getNoofcustomer())));
        prevItem.setTime(String.valueOf(Integer.parseInt(item.getTime()) + Integer.parseInt(prevItem.getTime())));
        map.put(item.getTab_name(), prevItem);
    } else {
        map.put(item.getTab_name(), item);
    }
}       
tableStatusList = new ArrayList<Arraylisttableitem>(map.values());

【讨论】:

    【解决方案2】:

    为每个项目使用HashMap。当您put 一个项目到地图时,它会返回您之前存储在那里的旧项目。所以这是你可以将两者结合起来的地方。

    【讨论】:

      【解决方案3】:
      • 从列表更改为集合,因为集合不允许重复值。
      • 在 Arraylisttableitem 中重写方法 hashCodeequals*

      【讨论】:

      • 我必须支持重复值。
      • 这不加值。
      猜你喜欢
      • 1970-01-01
      • 2012-10-05
      • 2018-11-04
      • 2012-03-28
      • 2014-08-25
      • 1970-01-01
      • 1970-01-01
      • 2020-07-06
      • 1970-01-01
      相关资源
      最近更新 更多