简体   繁体   English

Android:比较数组列表中的字符串并将其组合

[英]Android: Compare strings in array list and combine them

I'm wanting to compare the strings in an array list taken from my database and join them together.. Here is the code which collects data from my database.. 我想比较从我的数据库中获取的数组列表中的字符串,并将它们连接在一起。这是从我的数据库中收集数据的代码。

public List<String> getData2List() {
    String[] columns = new String[]{ KEY_ROWID, KEY_DATE, KEY_NAME, KEY_PRICE};
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, "1", null, null, null, null);

    List<String> results = new ArrayList<String>();
    int iCM = c.getColumnIndex(KEY_DATE);

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
        results.add(c.getString(iCM));
    }
    return results;
}

and here is the code to place them in the list.. 这是将它们放置在列表中的代码。

Database info = new Database(this);
    info.open();        

    List<String> dates = info.getData2List();
    lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, dates));

    info.close();

This all works fine but if there are more than one entry which is the same I end up with a list of the same thing (if this makes sense!?). 这一切都很好,但是如果有多个相同的条目,我最终会列出相同的东西(如果这有意义!?)。

Example: if the list came out like {"01/01/13", "02/01/13", "01/01/13", "03/02/13", "01/01/13"} I'm trying to make the out come like {"01/01/13", "02/01/13", "03/02/13"} so that all entry of the same value have been compiled into one. 示例:如果列表像{"01/01/13", "02/01/13", "01/01/13", "03/02/13", "01/01/13"}试图使输出像{"01/01/13", "02/01/13", "03/02/13"}以便将所有相同值的条目编译成一个。

Any help or ideas is much appreciated. 任何帮助或想法,我们将不胜感激。

Just use an Set instead of a List. 只需使用Set而不是List。

An HashSet will provide you unique strings, a List instead can contain same occurrence. HashSet将为您提供唯一的字符串,而List可以包含相同的字符串。

Why don't you just use HashSet<String> ? 为什么不只使用HashSet<String>

Example: 例:

List<String> dates = info.getData2List();
Set<String> uniqueDates = new HashSet<String>(dates);

You can use HashSet instead of ArrayList. 您可以使用HashSet代替ArrayList。

because in your case you will have following advantages 因为在您的情况下,您将具有以下优势

  1. joining two set will be more easier useing addAll method 使用addAll方法将两个集合连接起来会更容易
  2. There is no chance to have duplicate values 没有机会重复值

You need a Set collection class, basically what "Set" does is, storing only unique values, and if you try to set a new value that already exist in the collection it will just ignore, make sure you are overriding equals, and hashCode method as well in case you are trying to store your own object, look at the documentation for more info about how it works.. 您需要一个Set集合类,基本上“ Set”的作用是,仅存储唯一值,如果尝试设置集合中已存在的新值,它将被忽略,请确保您覆盖了equals和hashCode方法如果您要存储自己的对象,请查看文档以获取有关其工作方式的更多信息。

http://docs.oracle.com/javase/6/docs/api/java/util/Set.html http://docs.oracle.com/javase/6/docs/api/java/util/Set.html

Regards! 问候!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM