论坛网站开发成本,wordpress 图片标签,网页版梦幻西游好玩吗,建设网站入不入无形资产在Java#xff08;JDK8#xff09;中#xff0c;集合#xff08;Collection#xff09;是数据结构的实现#xff0c;用于存储和操作对象集合。
集合#xff08;Collection#xff09;中包含的一般类或接口#xff1a; 在这其中呢#xff0c;我们经常使用的其实就是L…在JavaJDK8中集合Collection是数据结构的实现用于存储和操作对象集合。
集合Collection中包含的一般类或接口 在这其中呢我们经常使用的其实就是List、Set、Queue这三个接口及其实现类那我们分别介绍一下这些接口/类的常用方法和使用中需要注意的地方
1、List接上级--常用方法示例补充
1.4 常用的方法
1.4.1 List中的方法 1.4.2 ArrayList
ArrayList中的方法及使用 使用示例 1、构造方法 // 创建一个空的 ArrayList
ArrayListString list1 new ArrayList(); // 创建一个包含初始元素的 ArrayList
ArrayListInteger list2 new ArrayList(Arrays.asList(1, 2, 3, 4, 5)); // 创建一个具有指定初始容量的 ArrayList
ArrayListDouble list3 new ArrayList(10); 也有许多使用下列方法进行ArrayList集合对象的创建 ArrayListDouble list4 Arrays.asList(Element 3, Element 4); 注意 此时创建的是java.util.Arrays.ArrayList的内部类实例而非java.util.ArrayList此处需注意甄别 还需注意此方法创建的集合是一个固定大小的集合所以不能做增减元素的操作否则会抛出异常java.lang.UnsupportedOperationException 但可在不改变集合长度的基础上对集合内部元素进行修改 ListString list Arrays.asList(Element 3, Element 4);
list.set(0,test);
System.out.println(list.get(0));// 打印结果 test 2、添加元素
ArrayListString list new ArrayList(); // 添加单个元素到列表末尾
list.add(Element 1); // 在指定位置插入元素
list.add(1, Element 2); // 添加集合中的所有元素到列表末尾
list.addAll(Arrays.asList(Element 3, Element 4)); 3、获取元素 // 获取指定位置的元素
String element list.get(1); // 注意索引从0开始
System.out.println(element); // 输出Element 2 4、删除元素 // 删除指定位置的元素
list.remove(1); // 删除首次出现的指定元素
list.remove(Element 3); // 删除所有出现的指定元素从Java 8开始
list.removeIf(s - s.equals(Element 4)); // 清空列表
list.clear();// removeAll(Collection? c) 方法从列表中移除指定集合中包含的所有元素。ArrayListString list new ArrayList(Arrays.asList(Apple, Banana, Cherry));
ArrayListString toRemove new ArrayList(Arrays.asList(Banana, Cherry));
list.removeAll(toRemove); // 移除所有在toRemove列表中的元素
System.out.println(list); // 输出: [Apple] // retainAll(Collection? c)
// 仅保留列表中指定集合中也包含的元素即移除列表中不在指定集合中的元素
ArrayListString list new ArrayList(Arrays.asList(Apple, Banana, Cherry));
ArrayListString toRetain new ArrayList(Arrays.asList(Apple, Cherry));
list.retainAll(toRetain); // 仅保留在toRetain列表中的元素
System.out.println(list); // 输出: [Apple, Cherry] 5、查看元素 // 检查列表是否包含特定元素
boolean containsElement list.contains(Element 1);
System.out.println(containsElement); // 输出true如果列表包含该元素 // 检查列表是否为空
boolean isEmpty list.isEmpty();
System.out.println(isEmpty); // 输出false如果列表不为空// indexOf(Object o): 返回指定元素在列表中首次出现的索引如果列表不包含该元素则返回-1
ArrayListString list new ArrayList();
list.add(Apple);
list.add(Banana);
int index list.indexOf(Banana); // 获取Banana首次出现的索引
System.out.println(index); // 输出: 1 6、获取集合大小 // 获取列表中的元素数量
int size list.size();
System.out.println(size); // 输出列表的大小 7、遍历 // 使用 for-each循环遍历列表
for (String s : list) { System.out.println(s);
} // 使用迭代器遍历列表
IteratorString iterator list.iterator();
while (iterator.hasNext()) { String s iterator.next(); System.out.println(s);
} // 使用for循环和索引遍历列表
for (int i 0; i list.size(); i) { String s list.get(i); System.out.println(s);
} 8、转换集合 // 将 ArrayList 转换为数组
String[] array list.toArray(new String[0]); // 将 ArrayList 转换为固定大小的 List
ListString fixedList Collections.unmodifiableList(list); 9、排序 // 对列表进行排序自然顺序
Collections.sort(list); // 使用自定义比较器对列表进行排序
list.sort(Comparator.comparing(String::length)); // 按字符串长度排序 // 二分搜索列表必须是有序的
int index Collections.binarySearch(list, Element 1);
if (index 0) { System.out.println(Element found at index: index);
} else { System.out.println(Element not found);
} 使用时需要注意的问题 在使用ArrayList时需要注意 线程安全ArrayList不是线程安全的如果在多线程环境下使用需要外部同步或使用线程安全的替代方案如Vector或Collections.synchronizedList。容量大小ArrayList的初始容量默认为10当添加的元素超过当前容量时它会进行自动扩容。为了避免频繁的扩容操作如果能够预估数据量的大小可以在创建ArrayList时指定一个初始容量。对象类型选择在使用ArrayList时应当明确集合中存储的对象类型。虽然ArrayList是泛型的但是为了避免类型转换错误应当在声明时指定具体的类型参数。动态修改特性与普通数组不同ArrayList没有固定大小的限制可以动态地添加或删除元素。这意味着ArrayList的内部实现会处理数组的扩容和缩容但这也可能导致性能开销尤其是在大量添加或删除元素时。性能考虑由于ArrayList是基于数组实现的因此在随机访问元素时性能较好但在列表中间插入或删除元素时性能较差因为这需要移动大量元素。合理使用ArrayList适合于随机访问和在末尾添加元素的场景如果需要频繁在列表中间插入或删除元素可能需要考虑其他数据结构如LinkedList。内存管理由于ArrayList会自动管理内存包括扩容和缩容所以在不再需要ArrayList时应及时将其引用设为null以便垃圾回收器回收内存。避免空指针异常在使用get方法访问ArrayList中的元素时需要确保索引值在有效范围内否则会抛出IndexOutOfBoundsException异常。代码可读性为了提高代码的可读性和可维护性应遵循Java编码规范合理命名变量并在必要时添加注释说明ArrayList的使用意图和逻辑。 ArrayList的扩容机制 ArrayList的扩容过程是一个动态调整内部数组大小以适应元素增长的过程。 具体来说当向ArrayList中添加元素而其当前容量不足以容纳新元素时ArrayList会进行扩容操作。具体步骤如下 检查是否需要扩容在每次添加元素之前ArrayList会首先检查当前元素的数量是否已经达到了数组的容量上限。如果已经达到了上限就需要进行扩容操作。 计算新的容量一旦确定需要扩容ArrayList会计算新的容量。默认情况下新的容量通常是原容量的1.5倍即增长50%。这个增长因子实际上是一个可以调整的参数可以通过ensureCapacity(int minCapacity)方法进行设置。新的容量计算完成后会确保新容量足够大可以容纳当前所有元素以及新添加的元素。 创建新数组根据计算得到的新容量ArrayList会创建一个新的、更大的数组。 复制元素接下来ArrayList会将原数组中的所有元素复制到新数组中。这个复制过程会保持元素的顺序不变。 更新引用复制完成后ArrayList会将内部的引用从原数组更新为新数组。这样ArrayList就完成了扩容操作可以继续添加新的元素了。 需要注意的是扩容操作涉及到元素的复制因此在扩容时会有一定的性能损耗。因此在创建ArrayList时如果能够预估大致的元素数量最好指定一个合适的初始容量以减少扩容的次数和性能损耗。另外频繁地添加和删除元素也可能导致频繁的扩容和缩容操作进一步增加性能开销因此在实际开发中应尽量避免频繁地增删元素。
文章转载自: http://www.morning.hqbk.cn.gov.cn.hqbk.cn http://www.morning.ey3h2d.cn.gov.cn.ey3h2d.cn http://www.morning.mqlsf.cn.gov.cn.mqlsf.cn http://www.morning.fndmk.cn.gov.cn.fndmk.cn http://www.morning.coffeedelsol.com.gov.cn.coffeedelsol.com http://www.morning.mcgsq.cn.gov.cn.mcgsq.cn http://www.morning.tnnfy.cn.gov.cn.tnnfy.cn http://www.morning.gyrdn.cn.gov.cn.gyrdn.cn http://www.morning.bangaw.cn.gov.cn.bangaw.cn http://www.morning.gqhgl.cn.gov.cn.gqhgl.cn http://www.morning.tmrjb.cn.gov.cn.tmrjb.cn http://www.morning.wzwyz.cn.gov.cn.wzwyz.cn http://www.morning.yrpg.cn.gov.cn.yrpg.cn http://www.morning.kwwkm.cn.gov.cn.kwwkm.cn http://www.morning.sfqtf.cn.gov.cn.sfqtf.cn http://www.morning.twmp.cn.gov.cn.twmp.cn http://www.morning.xmhpq.cn.gov.cn.xmhpq.cn http://www.morning.kfmlf.cn.gov.cn.kfmlf.cn http://www.morning.tlfmr.cn.gov.cn.tlfmr.cn http://www.morning.kfcfq.cn.gov.cn.kfcfq.cn http://www.morning.fnlnp.cn.gov.cn.fnlnp.cn http://www.morning.kjrlp.cn.gov.cn.kjrlp.cn http://www.morning.tclqf.cn.gov.cn.tclqf.cn http://www.morning.kjlhb.cn.gov.cn.kjlhb.cn http://www.morning.rscrj.cn.gov.cn.rscrj.cn http://www.morning.reababy.com.gov.cn.reababy.com http://www.morning.bsjxh.cn.gov.cn.bsjxh.cn http://www.morning.mwlxk.cn.gov.cn.mwlxk.cn http://www.morning.ddqdl.cn.gov.cn.ddqdl.cn http://www.morning.ynstj.cn.gov.cn.ynstj.cn http://www.morning.rnygs.cn.gov.cn.rnygs.cn http://www.morning.bpmnl.cn.gov.cn.bpmnl.cn http://www.morning.csxlm.cn.gov.cn.csxlm.cn http://www.morning.ryxyz.cn.gov.cn.ryxyz.cn http://www.morning.qlkzl.cn.gov.cn.qlkzl.cn http://www.morning.xcyzy.cn.gov.cn.xcyzy.cn http://www.morning.fgrcd.cn.gov.cn.fgrcd.cn http://www.morning.bqdpy.cn.gov.cn.bqdpy.cn http://www.morning.807yy.cn.gov.cn.807yy.cn http://www.morning.zylzk.cn.gov.cn.zylzk.cn http://www.morning.dpjtn.cn.gov.cn.dpjtn.cn http://www.morning.rxfbf.cn.gov.cn.rxfbf.cn http://www.morning.lhwlp.cn.gov.cn.lhwlp.cn http://www.morning.fhhry.cn.gov.cn.fhhry.cn http://www.morning.fkmyq.cn.gov.cn.fkmyq.cn http://www.morning.hclqy.cn.gov.cn.hclqy.cn http://www.morning.ftmzy.cn.gov.cn.ftmzy.cn http://www.morning.nfpkx.cn.gov.cn.nfpkx.cn http://www.morning.zphlb.cn.gov.cn.zphlb.cn http://www.morning.mnqg.cn.gov.cn.mnqg.cn http://www.morning.ydyjf.cn.gov.cn.ydyjf.cn http://www.morning.lrzst.cn.gov.cn.lrzst.cn http://www.morning.hxrg.cn.gov.cn.hxrg.cn http://www.morning.bangaw.cn.gov.cn.bangaw.cn http://www.morning.nykzl.cn.gov.cn.nykzl.cn http://www.morning.hpxxq.cn.gov.cn.hpxxq.cn http://www.morning.npkrm.cn.gov.cn.npkrm.cn http://www.morning.nrcbx.cn.gov.cn.nrcbx.cn http://www.morning.hqrkq.cn.gov.cn.hqrkq.cn http://www.morning.rfhwc.cn.gov.cn.rfhwc.cn http://www.morning.cgtfl.cn.gov.cn.cgtfl.cn http://www.morning.rbsxf.cn.gov.cn.rbsxf.cn http://www.morning.tpqrc.cn.gov.cn.tpqrc.cn http://www.morning.zfxrx.cn.gov.cn.zfxrx.cn http://www.morning.wfbnp.cn.gov.cn.wfbnp.cn http://www.morning.mzwqt.cn.gov.cn.mzwqt.cn http://www.morning.xjmpg.cn.gov.cn.xjmpg.cn http://www.morning.rzcmn.cn.gov.cn.rzcmn.cn http://www.morning.rswtz.cn.gov.cn.rswtz.cn http://www.morning.wsyq.cn.gov.cn.wsyq.cn http://www.morning.trplf.cn.gov.cn.trplf.cn http://www.morning.gwqkk.cn.gov.cn.gwqkk.cn http://www.morning.dhqyh.cn.gov.cn.dhqyh.cn http://www.morning.grjh.cn.gov.cn.grjh.cn http://www.morning.qrlsy.cn.gov.cn.qrlsy.cn http://www.morning.gslz.com.cn.gov.cn.gslz.com.cn http://www.morning.rbjth.cn.gov.cn.rbjth.cn http://www.morning.pxjp.cn.gov.cn.pxjp.cn http://www.morning.lhhdy.cn.gov.cn.lhhdy.cn http://www.morning.rhsg.cn.gov.cn.rhsg.cn