Is there any way to figure out how many of something an ArrayList contains? Let's say:
ArrayList list = new ArrayList(); list.add("Hi"); list.add("Hi"); list.add("Hi");
Now I want to be able to figure out how many "Hi"'s are in there.. any easy method or small codeblock to?
Collections.frequency . So in your example, Collections.frequency(list, "Hi");
标准JDK已经提供了完全做到这一点的Collections.frequency(Collection<?> c, Object o)
。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.