我有一个ArrayList
有一些记录,一列包含气体名作为CO2 CH4 SO2 etc.Now我想不只是从repetation检索不同的气体名称(唯一) ArrayList
。 怎么做到呢?
Answer 1:
您应该使用一个设置 。
的集合为集合不包含重复。
如果你有一个列表 ,其中包含重复的,你可以得到这样独特的条目:
List<String> gasList = // create list with duplicates...
Set<String> uniqueGas = new HashSet<String>(gasList);
System.out.println("Unique gas count: " + uniqueGas.size());
注 :本HashSet的构造通过调用的元素的equals()方法识别重复。
Answer 2:
您可以使用Java的8个流API 。
方法不同的是,过滤器流中,仅允许DISTICT值(使用对象默认:: equals方法)传递到下一个操作的中间操作 。
我写了下面的例子为你的情况,
// Create the list with duplicates.
List<String> listAll = Arrays.asList("CO2", "CH4", "SO2", "CO2", "CH4", "SO2", "CO2", "CH4", "SO2");
// Create a list with the distinct elements using stream.
List<String> listDistinct = listAll.stream().distinct().collect(Collectors.toList());
// Display them to terminal using stream::collect with a build in Collector.
String collectAll = listAll.stream().collect(Collectors.joining(", "));
System.out.println(collectAll); //=> CO2, CH4, SO2, CO2, CH4 etc..
String collectDistinct = listDistinct.stream().collect(Collectors.joining(", "));
System.out.println(collectDistinct); //=> CO2, CH4, SO2
Answer 3:
我希望我正确地理解你的问题:假设值的类型的String
,最有效的方式可能是转换为HashSet
,并遍历它:
ArrayList<String> values = ... //Your values
HashSet<String> uniqueValues = new HashSet<>(values);
for (String value : uniqueValues) {
... //Do something
}
Answer 4:
ArrayList values = ... // your values
Set uniqueValues = new HashSet(values); //now unique
Answer 5:
这里是不诉诸自定义比较器或类似的东西简单的方法:
Set<String> gasNames = new HashSet<String>();
List<YourRecord> records = ...;
for(YourRecord record : records) {
gasNames.add(record.getGasName());
}
// now gasNames is a set of unique gas names, which you could operate on:
List<String> sortedGasses = new ArrayList<String>(gasNames);
Collections.sort(sortedGasses);
注意:使用TreeSet
,而不是HashSet
将给予直接排序ArrayList和上述Collections.sort
就可以跳过去,但TreeSet
是efficent反之则少,所以它往往更好,而且很少更糟的是,使用HashSet
甚至需要排序时。
Answer 6:
如果你有某种对象(豆)的数组,你可以这样做:
List<aBean> gasList = createDuplicateGasBeans();
Set<aBean> uniqueGas = new HashSet<aBean>(gasList);
像说马蒂亚斯施瓦茨上面,但你必须提供与方法的aBean hashCode()
和equals(Object obj)
,可以轻松地在Eclipse中通过专用菜单来完成“ Generate hashCode() and equals()
”(同时在bean类)。 设置将评估覆盖的方法来区分等于对象。
Answer 7:
@SuppressWarnings({ "unchecked", "rawtypes" })
public static List getUniqueValues(List input) {
return new ArrayList<>(new HashSet<>(input));
}
不要忘记你实现equals方法
Answer 8:
当我在做同样的查询,我已经很难适应的解决方案,以我的情况下,虽然以前所有的答案,有良好的洞察力。
这是当一个人有获得唯一对象,而不是字符串列表的解决方案。 比方说,一个有记录对象的列表。 Record
类只有类型的属性String
,类型的无属性int
。 这里实施hashCode()
变得困难,因为hashCode()
必须返回一个int
。
以下是一个示例Record
类。
public class Record{
String employeeName;
String employeeGroup;
Record(String name, String group){
employeeName= name;
employeeGroup = group;
}
public String getEmployeeName(){
return employeeName;
}
public String getEmployeeGroup(){
return employeeGroup;
}
@Override
public boolean equals(Object o){
if(o instanceof Record){
if (((Record) o).employeeGroup.equals(employeeGroup) &&
((Record) o).employeeName.equals(employeeName)){
return true;
}
}
return false;
}
@Override
public int hashCode() { //this should return a unique code
int hash = 3; //this could be anything, but I would chose a prime(e.g. 5, 7, 11 )
//again, the multiplier could be anything like 59,79,89, any prime
hash = 89 * hash + Objects.hashCode(this.employeeGroup);
return hash;
}
正如别人更早建议的,类需要同时重写equals()
和hashCode()
的方法能够使用HashSet
。
现在,让我们说,记录清单是allRecord
( List<Record> allRecord
)。
Set<Record> distinctRecords = new HashSet<>();
for(Record rc: allRecord){
distinctRecords.add(rc);
}
这只会在不同的记录添加到HashSet中,distinctRecords。
希望这可以帮助。
Answer 9:
你可以用这个做一个独特的名单
ArrayList<String> listWithDuplicateValues = new ArrayList<>();
list.add("first");
list.add("first");
list.add("second");
ArrayList uniqueList = (ArrayList) listWithDuplicateValues.stream().distinct().collect(Collectors.toList());