1、add方法:添加
public class CollectionTest { public static void main(String[] args) { Collection<Object> obj = new ArrayList<>(); obj.add(123); obj.add("haha"); obj.add(new Student("HSpringX",19)); obj.add(new String("rose")); System.out.println(obj);
} } class Student{ String name; int age; public Student(String name, int age) { this.name = name; this.age = age; } }
输出结果:
[123, haha, com.learningTasks.collection.Student@2d98a335, rose]
优化:代码
public class CollectionTest { public static void main(String[] args) { Collection<Object> obj = new ArrayList<>(); obj.add(123); obj.add("haha"); obj.add(new Student("HSpringX",19)); obj.add(new String("rose")); System.out.println(obj); System.out.println(obj.size()); } } class Student{ String name; int age; //重写toString @Override public String toString() { return "name='" + name + '\'' + ", age=" + age ; } public Student(String name, int age) { this.name = name; this.age = age; } }
输出结果:
[123, haha, name='HSpringX', age=19, rose]
4
输出obj默认输出obj.toString
重写了Student类的toString,所以输出的new Student变为这样。集合里面仍然是4个元素。
addAll:添加集合中的所以元素
public class CollectionTest { public static void main(String[] args) { Collection<Object> obj = new ArrayList<>(); obj.add(123); obj.add("haha"); obj.add(new Student("HSpringX",19)); obj.add(new String("rose")); System.out.println(obj); Collection<Object> obj1 = new ArrayList<>(); obj1.add(345); obj1.add("haha"); obj.addAll(obj1); } } 输出: [123, haha, name='HSpringX', age=19, rose] [123, haha, name='HSpringX', age=19, rose, 345, haha]
clear:删除集合中的所有元素
public class CollectionTest { public static void main(String[] args) { Collection<Object> obj1 = new ArrayList<>(); obj1.add(345); obj1.add("haha"); obj1.clear(); System.out.println(obj1.size()); } } 输出: 0
contains方法:包含
public class CollectionTest { public static void main(String[] args) { Collection<Object> obj = new ArrayList<>(); obj.add(123); obj.add("haha"); obj.add(new Student("HSpringX",19)); obj.add(new String("rose")); System.out.println(obj.contains(new Student("HSpringX",19))); //String中的equals方法被重写了,比较的是内容是否相等 System.out.println(obj.contains(new String("rose"))); } } class Student{ String name; int age; public Student(String name, int age) { this.name = name; this.age = age; } }
输出结果: false true
自定义类Student,equals方法为Object中的equals方法,需要重写才能满足比较内容是否相等的需求
public class CollectionTest { public static void main(String[] args) { Collection<Object> obj = new ArrayList<>(); obj.add(123); obj.add("haha"); obj.add(new Student("HSpringX",19)); obj.add(new String("rose")); System.out.println(obj.contains(new Student("HSpringX",19))); System.out.println(obj.contains(new String("rose"))); } } class Student{ String name; int age;
//equals方法的重写 @Override public boolean equals(Object o) { System.out.println("equals被使用了"); if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Student student = (Student) o; return age == student.age && Objects.equals(name, student.name); } public Student(String name, int age) { this.name = name; this.age = age; } } 输出: equals被使用了 equals被使用了 equals被使用了 true true
containsAll:是否包含集合中的所有元素
public class CollectionTest { public static void main(String[] args) { Collection<Object> obj = new ArrayList<>(); obj.add(123); obj.add("haha"); obj.add(new Student("HSpringX",19)); obj.add(new String("rose")); Collection<Object> obj1 = new ArrayList<>(); obj1.add(345); obj1.add("haha"); System.out.println(obj.containsAll(obj1)); obj.addAll(obj1); System.out.println(obj.containsAll(obj1)); } } 输出 false true
equals方法:判断相等性
public class CollectionTest { public static void main(String[] args) { Collection<Object> obj = new ArrayList<>(); obj.add(123); obj.add("haha"); obj.add(new Student("HSpringX",19)); obj.add(new String("rose")); Collection<Object> obj1 = new ArrayList<>(); obj1.add(123); obj1.add("haha"); obj1.add(new Student("HSpringX",19));//重写了equals方法 obj1.add(new String("rose")); System.out.println(obj.equals(obj1)); } }
输出:
equals被调用了
true
hashCode:返回哈希值
isEmpty:集合是否为空
iterator:返回集合中的所有元素的迭代器(遍历)
public class CollectionTest { public static void main(String[] args) { Collection<Object> obj = new ArrayList<>(); obj.add(123); obj.add("haha"); obj.add(new Student("HSpringX",19)); obj.add(new String("rose")); //建议这样迭代,其他方法可能报错 Iterator<Object> iterator = obj.iterator(); //hasNext()判断是否有下一个元素 while (iterator.hasNext()){ //next()指针下移,返回元素 System.out.println(iterator.next()); } } } 输出 123 haha name='HSpringX', age=19 rose
remove:移除元素(实例而不是索引)
!!!移除一个
public class CollectionTest { public static void main(String[] args) { Collection<Object> obj = new ArrayList<>(); obj.add(123); obj.add("haha"); obj.add(new Student("HSpringX",19)); obj.add(new String("rose")); System.out.println(obj.remove(123)); System.out.println(obj); } } 输出: true [haha, name='HSpringX', age=19, rose]
removeAll:移除集合之间的相同元素(差集)
!!!不看顺序与数量
public class CollectionTest { public static void main(String[] args) { Collection<Object> obj = new ArrayList<>(); obj.add(123); obj.add("haha"); obj.add(new Student("HSpringX",19)); obj.add(new String("rose")); Collection<Object> obj1 = new ArrayList<>(); obj1.add(345); obj1.add("haha"); obj1.add(123); obj1.add("haha"); obj1.add(new Student("HSpringX",19)); obj1.add(new String("rose")); System.out.println(obj1.removeAll(obj)); System.out.println(obj1); } } 输出: true [345]
retainAll:保留两个集合的共同元素(交集)
!!!不看顺序与数量
public class CollectionTest { public static void main(String[] args) { Collection<Object> obj = new ArrayList<>(); obj.add(123); obj.add("haha"); obj.add(new Student("HSpringX",19)); obj.add(new String("rose")); Collection<Object> obj1 = new ArrayList<>(); obj1.add(345); obj1.add("haha"); obj1.add(123); obj1.add("haha"); obj1.add(new Student("HSpringX",19)); obj1.add(new String("rose")); System.out.println(obj1.retainAll(obj)); System.out.println(obj1); } } 输出: true [haha, 123, haha, name='HSpringX', age=19, rose]
size:集合中的元素数量
toArray:返回一个包含此集合中所有元素的数组(需要遍历输出)
public class CollectionTest { public static void main(String[] args) { Collection<Object> obj = new ArrayList<>(); obj.add(123); obj.add("haha"); obj.add(new Student("HSpringX",19)); obj.add(new String("rose")); Object[] objects = obj.toArray(); for(Object o:objects){ System.out.println(o); } } } 输出: 123 haha name='HSpringX', age=19 rose
数组---->集合
Arrays类中的类方法asList
String[] s = {"aa", "bb"}; List<String> strings = Arrays.asList(s); System.out.println(strings); //输出[aa, bb] int[] i = {11, 22, 33}; List<int[]> ints = Arrays.asList(i); //集合不能存储基本类型变量 System.out.println(ints); //输出[[I@5fd0d5ae] Integer[] integers = {11,22,33}; List<Integer> integers1 = Arrays.asList(integers); System.out.println(integers1); //输出[11, 22, 33]