import java.util.*;
import java.util.stream.*;
public class CollectionTest
{
public static void main(String[] args)
{
//拿ArrayList来练习
Collection c = new ArrayList();
c.add("a");
System.out.println(c);
Collection c2 = new ArrayList();
c2.add("b");
c2.add("c");
c.addAll(c2);
System.out.println(c);
c.clear();
System.out.println(c);
System.out.println(c.contains("a"));
System.out.println(c.containsAll(c2));
System.out.println(c.equals(c2));
System.out.println(c.hashCode());
System.out.println(c2.hashCode());
System.out.println(c.isEmpty());
System.out.println("_____________");
Iterator iterator = c2.iterator();
iterator.forEachRemaining((s)->System.out.println(s));
System.out.println(iterator);
iterator.remove();
iterator = c2.iterator();
while(iterator.hasNext())
{
System.out.println(iterator.next());
}
c.add("a");
c.add("b");
c.add("C");
System.out.println(c);
c.remove("C");
System.out.println(c);
System.out.println(c.removeAll(c2));
System.out.println(c2);
System.out.println(c);
System.out.println(c.size());
c.add("dyy");
c.add("fy");
c.removeIf((s)->{
return ((String)s).length()>2;
});
System.out.println(c);
c.add("b");
System.out.println(c2);
//System.out.println(c.retainAll(c2));
Spliterator spl = c.spliterator();
System.out.println(spl);
System.out.println(spl.characteristics());
System.out.println(spl.estimateSize());
System.out.println(c);
spl.forEachRemaining((s)->System.out.println(s));
//System.out.println(spl.getComparator());//报错了,不知道为什么,报了IllegalStateException
System.out.println(spl.hasCharacteristics(1));
System.out.println(spl.tryAdvance((s) -> s = s+"SS"));
System.out.println(c);
/*应该是private内部类
System.out.println(Spliterator.OfDouble);
System.out.println(Spliterator.OfInt);
System.out.println(Spliterator.OfLong);
System.out.println(Spliterator.OfPrimitive);
*/
System.out.println(Spliterator.CONCURRENT);
System.out.println(Spliterator.DISTINCT);
System.out.println(Spliterator.IMMUTABLE);
System.out.println(Spliterator.NONNULL);
System.out.println(Spliterator.ORDERED);
System.out.println(Spliterator.SIZED);
System.out.println(Spliterator.SORTED);
System.out.println(Spliterator.SUBSIZED);
System.out.println(Arrays.toString(c.toArray()));
String[] a = new String[c.size()];
System.out.println(Arrays.toString(c.toArray(a)));
System.out.println(Arrays.toString(a));
//System.out.println(Arrays.toString(c.toArray((s)->s)));
Stream stream = c.parallelStream();
ArrayList<String> c3 = new ArrayList<>();
c3.add("C3");
System.out.println(Arrays.toString(c3.toArray()));
ArrayList c4 = new ArrayList();
c4.add("C4");
System.out.println(Arrays.toString(c4.toArray()));
}
}
练习Collection接口里的方法
于 2022-02-27 22:09:50 首次发布