Java – how do I get numbers? How many items have different names?
•
Java
I have a list < custom > Where is custom
class Custom {
public int id;
public String name;
}
How do I get numbers? How many items have different names? Is there a simpler way to put all settings and sizes instead?
Solution
Setting Name:
Set<String> set = new HashSet<String>();
for (Custom c : list) {
set.add(c.name);
}
System.out.println(set.size());
If you use a corresponding comparator that compares only names, you can also populate the set with a custom object:
int nDifferentNames = new HashSet<Custom>(list,new Comparator<Custom>() {
public int compare(Custom one,Custom two) {
return one.name.compareTo(two.name);
}
}).size();
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
