目录
集合、数组都是对多个数据进行存储操作的结构,简称Java容器。
remove(Object obj):从当前集合中移除obj元素。
retainAll(Collection coll1):交集:获取当前集合和coll1集合的交集,并返回给当前集合。
数组 —>集合:调用Arrays类的静态方法asList()
-
集合、数组都是对多个数据进行存储操作的结构,简称Java容器。
① 数组:
①一旦初始化之后,其长度就确定了。
②数组一旦定义好,其元素的类型也就确定了。我们只能操作指定类型的数据。比如:String[] arr;int[] arr1;Object[] arr2;
② 数组存储的弊端:
①一旦初始化之后,其长度就不可修改。
②数组中提供的方法非常有限,对于添加、删除、插入数据等操作,非常不便,同时效率不 高。
③获取数组中实际元素的个数的需求,数组没有现成的属性或方法可用。
④数组存储数据的特点:有序、可重复。对于无序、不可重复的需求,不能满足。
③ 集合存储的优点:
解决数组存储数据方面的弊端。
-
集合框架
|—-Collection接口:单列集合,用来存储一个一个的对象
|—-List接口:存储有序的、可重复的数据。 –>“动态”数组
|—-ArrayList、LinkedList、Vector
|—-Set接口:存储无序的、不可重复的数据 –>高中讲的“集合”
|—-HashSet、LinkedHashSet、TreeSet
|—-Map接口:双列集合,用来存储一对(key – value)一对的数据 –>高中函数:y = f(x) |—-HashMap、LinkedHashMap、TreeMap、Hashtable、Properties
public void test1(){
Collection coll = new ArrayList();
//add(Object e):将元素e添加到集合coll中
coll.add("AA");
coll.add("BB");
coll.add(123);//自动装箱
coll.add(new Date());
//size():获取添加的元素的个数
System.out.println(coll.size());//4
//addAll(Collection coll1):将coll1集合中的元素添加到当前的集合中
Collection coll1 = new ArrayList();
coll1.add(456);
coll1.add("CC");
coll.addAll(coll1);
System.out.println(coll.size());//6
System.out.println(coll);//[AA, BB, 123, Wed Mar 30 19:26:46 CST 2022, 456, CC]
//clear():清空集合元素
coll.clear();
//isEmpty():判断当前集合是否为空
System.out.println(coll.isEmpty());//true
}
-
向接口中声明方法的测试(contains()方法)
public void test1(){
Collection coll = new ArrayList();
coll.add(123);
coll.add(456);
// Person p = new Person("Jerry",20);
// coll.add(p);
coll.add(new Person("Jerry",20));
coll.add(new String("Tom"));
coll.add(false);
//1.contains(Object obj):判断当前集合中是否包含obj
//我们在判断时会调用obj对象所在类的equals()。
boolean contains = coll.contains(123);
System.out.println(contains);//true
System.out.println(coll.contains(new String("Tom")));//true
// System.out.println(coll.contains(p));//true
System.out.println(coll.contains(new Person("Jerry",20)));//false(需要重写一下equals,否则一直用的就是==) -->true
//最后输出结果中,之前每添加一个,都会调用一次equals()
//2.containsAll(Collection coll1):判断形参coll1中的所有元素是否都存在于当前集合中。
Collection coll1 = Arrays.asList(123,4567);
System.out.println(coll.containsAll(coll1));//false
}
第二个contains需要重写一下equals 方法,然后false就会变成true。
public boolean equals(Object o) {
System.out.println("Person equals()....");
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return age == person.age &&
Objects.equals(name, person.name);
}
-
remove(Object obj):从当前集合中移除obj元素。
public void test2(){
//3.remove(Object obj):从当前集合中移除obj元素。
Collection coll = new ArrayList();
coll.add(1234);
coll.add(456);
coll.add(new Person("Jerry",20));
coll.add(new String("Tom"));
coll.add(false);
System.out.println(coll);//[1234, 456, Person{name='Jerry', age=20}, Tom, false]
coll.remove(1234);
System.out.println(coll);//[456, Person{name='Jerry', age=20}, Tom, false]
coll.remove(new Person("Jerry",20));//再次调用equals()方法
System.out.println(coll);//[456, Tom, false]
//4. removeAll(Collection coll1):差集:从当前集合中移除coll1中所有的元素。
Collection coll1 = Arrays.asList(123,456);
coll.removeAll(coll1);
System.out.println(coll);//[Tom, false]
}
-
retainAll(Collection coll1):交集:获取当前集合和coll1集合的交集,并返回给当前集合。
public void test3(){
Collection coll = new ArrayList();
coll.add(123);
coll.add(456);
coll.add(new Person("Jerry",20));
coll.add(new String("Tom"));
coll.add(false);
System.out.println(coll);
//5.retainAll(Collection coll1):交集:获取当前集合和coll1集合的交集,并返回给当前集合
Collection coll1 = Arrays.asList(123,456,789);
coll.retainAll(coll1);
System.out.println(coll);
System.out.println("************************");
//6.equals(Object obj):要想返回true,需要当前集合和形参集合的元素都相同。
Collection coll2 = new ArrayList();
coll2.add(123);
coll2.add(456);
coll2.add(new Person("Jerry",20));
coll2.add(new String("Tom"));
coll2.add(false);
System.out.println(coll.equals(coll2));//false
}
[123, 456, Person{name=’Jerry’, age=20}, Tom, false]
Person equals()….
Person equals()….
Person equals()….
[123, 456]
************************
false
-
数组 —>集合:调用Arrays类的静态方法asList()
-
集合 —>数组:toArray()
public void test4(){
Collection coll = new ArrayList();
coll.add(123);
coll.add(456);
coll.add(new Person("Jerry",20));
coll.add(new String("Tom"));
coll.add(false);
//7.hashCode():返回当前对象的哈希值
System.out.println(coll.hashCode());
//8.集合 --->数组:toArray()
Object[] arr = coll.toArray();
for(int i = 0;i < arr.length;i++){
System.out.print(arr[i]+" ");
}
System.out.println();
//拓展:数组 --->集合:调用Arrays类的静态方法asList()
List<String> list = Arrays.asList(new String[]{"AA", "BB", "CC"});
System.out.println(list);
List arr1 = Arrays.asList(new int[]{123, 456});
System.out.println(arr1.size());//1
List arr2 = Arrays.asList(new Integer[]{123, 456});//包装类的对象
System.out.println(arr2.size());//2
//9.iterator():返回Iterator接口的实例,用于遍历集合元素。放在IteratorTest.java中测试
}
-1200490100
123 456 Person{name=’Jerry’, age=20} Tom false
[AA, BB, CC]
1
2
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/83305.html