Java读书笔记四(集合类)

编程入门 行业动态 更新时间:2024-10-13 00:35:53

Java<a href=https://www.elefans.com/category/jswz/34/1768764.html style=读书笔记四(集合类)"/>

Java读书笔记四(集合类)

 

 1.介绍

本篇博客阐述一下java中的集合类,在java集合中提供的主要的接口有6个,主要类有10个,详见下图。




 2.List接口及实现类操作

List接口继承自Collection接口,其中的元素可以按照索引的顺序访问,并且元素的顺序均是按照添加的先后顺序进行的。

1.ArrayList实现类:在开发中经常用到的集合类,可以向其中添加包括null值在内的所有对象引用型的元素,甚至可以搭建一个树形的结构。该类内部是依赖数组实现,因此对元素进行随机访问的性能很好。如果进行大量插入和删除操作,性能很差。

package com.Collection;import java.util.ArrayList;public class Sample14_4 {public static void main(String[] args) {// 创建列表ArrayList的对象ArrayList al = new ArrayList();// 初始化ArrayList对象中的元素for (int i = 0; i < 50; i++) {al.add(String.valueOf(i));}// 对ArrayList进行操作for (int i = 60; i < 75; i++) {al.set(i - 45, String.valueOf(i));}// 打印ArrayList列表中的内容System.out.println("这里是ArrayList操作后的结果:");System.out.println(al);// 取出指定索引的元素并处理Object o = al.get(22);String s = (String) o;System.out.println("索引为22的元素长度为:" + s.length());}
}


2.Vector向量类:性能特点与ArrayList基本上相同,不同的是该类的功能方法时同步的,同一时刻只能有一个线程访问。

3.LinkedList链接类:功能与上面两个相同,都是List的实现类,只不过其内部是通过依赖双链表来实现的,因此插入删除性能很好,但是随机访问性能不是很好。

package com.Collection;import java.util.LinkedList;public class Sample14_6 {public static void main(String[] args) {// 创建列表LinkedList类的对象LinkedList ll = new LinkedList();// 初始化LinkedList对象for (int i = 0; i < 50; i++) {ll.add(String.valueOf(i));}// 对LinkedList进行插入操作for (int i = 15; i < 30; i++) {ll.add(i, String.valueOf(30 - i + 15));}// 打印LinkedList列表System.out.println("这里是LinkedList操作后的结果:");System.out.println(ll);}
}


 3.Set接口及其实现类

Set接口也继承自Collection接口,与List接口不同的之处

1.Set集合中不能出现两个内容相同的对象引用.

2.Set中的元素没有顺序。

3.HashSet类:元素在其中存储不能保证任何顺序。可以向其中添加null值,但只能添加一次。

4.LinkedHashSet类:与HashSet功能相似,只不过采用了双链表的操作。

package com.Collection;import java.util.LinkedHashSet;public class Sample14_10 {public static void main(String[] args) {// 创建了LinkedHashSet对象LinkedHashSet lhs = new LinkedHashSet();// 向LinkedHashSet对象中依次添加了内容为5、1、3、2、4的字符串lhs.add(String.valueOf(5));lhs.add(String.valueOf(1));lhs.add(String.valueOf(3));lhs.add(String.valueOf(2));lhs.add(String.valueOf(4));// 移除了HashSet对象中内容为"5"的字符串lhs.remove(String.valueOf(5));// 将null值添加了进HashSet对象lhs.add(null);// 打印输出LinkedHashSet中的元素内容System.out.print("这里是LinkedHashSet操作后的结果:");System.out.println(lhs);}
}


5.SortedSet接口与TreeSet类

SortedSet接口继承了Set接口,该类中的元素是按照天然顺序进行自动排序的。其中TreeSet实现类中采用了二叉树的排序方式操作。

6.集合的遍历

迭代器操作:Collection  cset=new HashSet()   Iterator i1=cset.iterator()

for-each循环:HashSet hs=new HashSet    for(object o : hs){}


 4.Map映射集操作

实现了该接口的集合,都是以一种键值对的形式进行存储的,并且键对象在Map中是唯一标识,不允许重复出现。

1.HashMap类:是Map接口最常用的实现之一,该类的键值运行为null,但只能出现一次。

package com.Collection;import java.util.*;
public class Sample14_16
{public static void main(String[] args){//创建了HashMap对象HashMap hm=new HashMap();//向HashMap对象中添加内容不同的键值对hm.put(Integer.valueOf(97005),"Tom");hm.put(Integer.valueOf(97003),"Jerry");hm.put(Integer.valueOf(97004),"Lucy");hm.put(Integer.valueOf(97001),"Smith");hm.put(Integer.valueOf(97002),"Jc");System.out.print("这里是HashMap操作前的结果:");System.out.println(hm);//移除了HashMap对象中键为97001的值hm.remove(Integer.valueOf(97001));//替换键97002对应的值hm.put(Integer.valueOf(97002),"David");//打印输出HashMap中的内容System.out.print("这里是HashMap操作后的结果:");System.out.println(hm);//取出指定键对应的值Object o=hm.get(97003);//使用了自动打包功能String s=(String)o;System.out.println("键97003对应的值为:"+s+",长度为:"+s.length()+"。");}
}


2.HashTable类:与HashMap类相同,不同的是该类某一时刻只能有一个线程访问。

3.LinkedHashMap类:通过双链表的方式进行实现的Map,插入删除的效率比HashMap略差,遍历的效率比HashMap高。


 5.Collections工具类

在实际开发中经常需要对集合中的元素进行排序、搜索等操作,java中为我们提供了Collections工具类,该类提供的方法都是静态方法。

package com.Collection;import java.util.*;
public class Sample14_27
{public static void main(String[] args){//分别创建3个空的ArrayList对象List l1=new ArrayList();List l2=new ArrayList();List l3=new ArrayList();//对ArrayList对象l1与l2进行初始化操作for(int i=0;i<12;i++){l1.add(Integer.valueOf(i));l2.add(Integer.valueOf(i+50));}//使用copy方法System.out.println("===================================copy"+"=====================================");System.out.println("方法使用前元素为:"+l1);Collections.copy(l1,l2);System.out.println("方法使用后元素为:"+l1);//使用disjoint方法System.out.println("==================================disjoint"+"==================================");if(!Collections.disjoint(l1,l2))System.out.println("列表l1与l2中有相同的元素!!!");System.out.println("====================================addAll"+"==================================");System.out.println("方法使用前元素为:"+l1);Collections.addAll(l1,new String[]{"tom","jc"});System.out.println("方法使用后元素为:"+l1);//使用fill方法System.out.println("=======================================fill"+"=================================");System.out.println("方法使用前元素为:"+l2);Collections.fill(l2,"0");System.out.println("方法使用后元素为:"+l2);//使用frequency方法System.out.println("=================================frequency"+"==================================");int i=Collections.frequency(l3,"0");System.out.println("l3中有元素'0' "+i+" 个!!!");//使用replaceAll方法System.out.println("=================================replaceAll"+"=================================");System.out.println("方法使用前元素为:"+l2);Collections.replaceAll(l2,"0","3");System.out.println("方法使用后元素为:"+l2);//使用reverse方法System.out.println("====================================reverse"+"=================================");System.out.println("方法使用前元素为:"+l1);Collections.reverse(l1);System.out.println("方法使用后元素为:"+l1);//使用rotate方法System.out.println("=====================================rotate"+"=================================");System.out.println("方法使用前元素为:"+l1);Collections.rotate(l1,6);System.out.println("方法使用后元素为:"+l1);//使用swap方法System.out.println("=======================================swap"+"==================================");System.out.println("方法使用前元素为:"+l1);Collections.swap(l1,6,10);System.out.println("方法使用后元素为:"+l1);     }
}


 6.小结

本篇博客主要阐述了java常用到的集合类,一般分为两种,一种是Collection类;一种是Map类;前者是依赖数组内部机制实现,后者是依赖键值对的形式实现。以后在开发过程中,根据自己的需要选择合适的集合类来使用。



更多推荐

Java读书笔记四(集合类)

本文发布于:2024-02-25 19:20:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1700040.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:读书笔记   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!