[英]How to search for similarities in an ArrayList
I have an arrayList of String which I would like to display in a Spinner or drop down menu, but mostly what happens is I have Strings repeating, what I want to do is to search through the arrayList for similarities, if its found a string for example "Hello World" occurs 7 times in the arrayList, remove the the other 6 and assign 7 to it to show that it occurred 7 times, so my new String will be "Hello world (7)", could anyone help me on how I can implement this in my code below: 我有一个要在Spinner或下拉菜单中显示的String的arrayList,但是大多数情况是我重复了Strings,我想做的就是在arrayList中搜索相似之处,如果它找到了一个字符串例如,“ Hello World”在arrayList中出现了7次,请删除其他6项并为其分配7,以表明它发生了7次,所以我的新String将是“ Hello world(7)”,有人可以帮助我我可以在下面的代码中实现这一点:
for(int i = 0; i < timesCalleddb.getAllTimesCalled(missedCall.getNumber()).size(); i++)
{
if(timesCalleddb.getAllTimesCalled(missedCall.getNumber()).get(i) ==
timesCalleddb.getAllTimesCalled(missedCall.getNumber()).get(i+1))
{
//where am guessing the implementation my problem should be
}
}
You should consider using map data structure, since you have to store the counter, otherwise, hash set would be perfect: 您应该考虑使用地图数据结构,因为您必须存储计数器,否则,哈希集将是完美的:
ArrayList<String> strs = ...;
HashMap<String, Integer> counter = new HashMap<String, Integer>();
for(String s : strs) {
counter.put(s, counter.get(s) == null ? 1 : counter.get(s) + 1);
}
for(String s : counter.keySet()) {
System.out.println(s + " (" + counter.get(s) + ")");
}
You could do the following: 您可以执行以下操作:
List
and make a HashMap<String, Integer>
indicating how many times each String
appears. List
并创建一个HashMap<String, Integer>
指示每个String
出现多少次。 List
using list = new ArrayList<String>(new LinkedHashSet<String>(list));
list = new ArrayList<String>(new LinkedHashSet<String>(list));
从List
删除重复项list = new ArrayList<String>(new LinkedHashSet<String>(list));
. LinkedHashSet
means that the order is kept. LinkedHashSet
表示保留订单。 List
by iterating over the List
and adding either string
or string + " (" + map.get(string) + ")"
depending on whether map.get(string)
is 1
or more than 1
. List
并添加string
或string + " (" + map.get(string) + ")"
构建新List
,具体取决于map.get(string)
是1
还是大于1
。 To remove the repeated String
from a List
, use the following: 要从
List
删除重复的String
,请使用以下命令:
List<String> arrayList = new ArrayList<>();
// Your elements must be added to the arrayList, before progressing to the next step.
Set<String> set = new HashSet<>();
set.addAll(arrayList );
// Code for getting count of each String
int count = 0;
List<Integer> arrayListCount = new ArrayList<>();
for (Iterator<String> it = set.iterator(); it.hasNext(); ) {
String str = it.next();
arrayListCount.add(count , 0);
for(int i = 0; i < arrayList.size(); i++){
String s = arrayList.get(i);
if (str.equals(s)) {
arrayListCount.set(count , arrayListCount.get(count) + 1);
}
}
count++;
}
// Code for getting count ends here
arrayList.clear();
arrayList.addAll(set);
Note: The sequence of the List
won't be retained. 注意:
List
的顺序将不会保留。
Hope that Helps!!! 希望对您有帮助!!!
You can use this code for filtering you CustomList based on a particular String of the list. 您可以使用此代码基于列表的特定String来过滤CustomList。 If you want to count the number of occurence, you can add some counter in the loop.
如果要计算发生次数,可以在循环中添加一些计数器。
List<MyCustomObject> arrayList = new ArrayList<>();
List<MyCustomObject> result = new ArrayList<>();
Set<String> set = new HashSet<>();
for(MyCustomObject item : arrayList) {
if(set.add(item.getSomeString()) {
resultArray.add(item);
}
}
arrayList.clear();
arrayList.addAll(result);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.