在 Collections.unmodifiableList 实现中,我看到它总是用 UnmodifiableList 包装给定列表,即使给定列表已经是 UnmodifiableList ......所以如果我一遍又一遍地调用这个方法 - 我会得到一个巨大的堆栈跟踪,如下所示:
at java.util.Collections$UnmodifiableCollection.size(Collections.java:998)
at java.util.Collections$UnmodifiableCollection.size(Collections.java:998)
at java.util.Collections$UnmodifiableCollection.size(Collections.java:998)
at java.util.Collections$UnmodifiableCollection.size(Collections.java:998)
at java.util.Collections$UnmodifiableCollection.size(Collections.java:998)
at java.util.Collections$UnmodifiableCollection.size(Collections.java:998)
at java.util.Collections$UnmodifiableCollection.size(Collections.java:998)
at java.util.Collections$UnmodifiableCollection.size(Collections.java:998)
at java.util.Collections$UnmodifiableCollection.size(Collections.java:998)
at java.util.Collections$UnmodifiableCollection.size(Collections.java:998)
at java.util.Collections$UnmodifiableCollection.size(Collections.java:998)
at java.util.Collections$UnmodifiableCollection.size(Collections.java:998)
at java.util.Collections$UnmodifiableCollection.size(Collections.java:998)
at java.util.Collections$UnmodifiableCollection.size(Collections.java:998)
at java.util.Collections$UnmodifiableCollection.size(Collections.java:998)
at java.util.Collections$UnmodifiableCollection.size(Collections.java:998)
at java.util.Collections$UnmodifiableCollection.size(Collections.java:998)
at java.util.Collections$UnmodifiableCollection.size(Collections.java:998)
at java.util.Collections$UnmodifiableCollection.size(Collections.java:998)
at java.util.Collections$UnmodifiableCollection.size(Collections.java:998)
at java.util.Collections$UnmodifiableCollection.size(Collections.java:998)
at java.util.Collections$UnmodifiableCollection.size(Collections.java:998)
at java.util.Collections$UnmodifiableCollection.size(Collections.java:998)
我想检查列表是否已经是 UnmodifiabelList 并返回相同的列表,就像谷歌的收藏一样,但这不是公共课程。
任何建议如何优雅地解决这个问题?
谢谢