前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >类集框架(一)

类集框架(一)

作者头像
MaybeHC
发布2024-04-23 18:08:49
700
发布2024-04-23 18:08:49
举报
文章被收录于专栏:技术之路
1.单值保存的最大父接口:Collection

在Collection中定义了15个方法,在所以的方法中,只有两个方法最为常用:add(),iterator().从开发来讲,很少直接使用Collection。

2.允许重复的子接口:LIst

public interface List extends Collection List接口继承了Collection接口,但是List接口对Collection接口进行了大量的扩充。

No

方法名称

描述

1

public E get(int index)

取得指定索引位置上的数据

2

public E set(int index,E element)

修改指定索引位置上的数据

3

public ListIteratorlistIterator

为ListIterator接口实例化

List接口有两个常用的子类ArrayList和Vector

2.1 ArrayList

public class ArrayList extends AbstractList implements List ,RandomAccess,Coloneable,Serializable 使用ArrayList的主要目的是为List接口实例化,所以操作方法都以List接口为主

例:使用ArrayList进行List接口的功能验证 import java.util.ArrayList; import java.util.List; public class test{ public static void main(String[] args) { List<String>all=new ArrayList<String>();//实例化List接口 all.add("hello");//添加内容 all.add("hello"); all.add("World"); for(int x = 0 ;x<all.size();x++){ System.out.println(all.get(x)); } 程序运行结果:[hello, hello,World]

2.2 Vector

例:使用Vector进行List接口的功能验证 import java.util.Vector; import java.util.List; public class test{ public static void main(String[] args) { List<String>all=new Vector<String>();//实例化List接口 all.add("hello");//添加内容 all.add("hello"); all.add("World"); for(int x = 0 ;x<all.size();x++){ System.out.println(all.get(x)); } 程序运行结果:[hello, hello,World]

ArrayList 和Vector 都是List接口的子类

ArrayList和Vector区别

No

区别

ArrayList

Vector

1

推出时间

JDK1.2

JDK1.0

2

性能

采用异步处理方式,性能更高

采用同步处理方式,性能相对较低

3

安全性

非线程安全

线程安全

4

输出

Iterator、ListIterator、foreach

Iterator、ListIterator、foreach、Enumeration

3.不允许重复的子接口:Set

public interface Setextends Collection Set子接口完整的继承了Collection接口,Set子接口常用的两个子类为HashSet和TreeSet

3.1散列存放的子类:HashSet

HashSet使用一种散列(无序)的方式保存集合数据

例:使用Set接口 import java.util.HashSet; import java.util.Set; public class test{ public static void main(String[] args) { Set<String> all=new HashSet<String>(); all.add("hello"); all.add("hello"); all.add("world"); System.out.println(all); } } 程序运行结果:[world, hello]

使用Set保存数据的时,集合中重复数据没有保存,并且无序

3.2排序存放的子类:TreeSet

使用TreeSet import java.util.Set; import java.util.TreeSet; public class test{ public static void main(String[] args) { Set<String> all=new TreeSet<String>(); all.add("c"); all.add("b"); all.add("a"); System.out.println(all); } } 程序运行结果:[a, b, c]

TreeSet排序,自定义类排序,使用Comparable

Source>Generate hashSet

4.集合的输出操作

4种输出方式:Iterator,ListIterator,Enumeration,foreach

4.1迭代输出:Iterator

Iterator中常用方法

No

方法名称

描述

1

public boolean hasNext()

判断是否有下一个元素

2

public E next()

取出当前元素

3

public void remove()

移除当前元素

如何取得Iterator的实例化对象?Collection继承了一个Iterator接口,在Iterator接口中定义了一个方法“public Iteratoriterator()

例:使用Iterator输出集合数据 import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class test{ public static void main(String[] args) { List<String> all=new ArrayList<String>(); all.add("hello"); all.add("hello"); all.add("world"); Iterator<String>iter =all.iterator(); while(iter.hasNext()){ String str = iter.next(); System.out.print(str+" "); } } } 程序运行结果:hello hello world

在项目开发中,只要遇到集合对象输出问题,一定要使用Iterator接口完成

4.2 双向迭代输出:ListIterator(了解)

Iterator可以完成由前向后的单向输出操作,如果希望完成由前向后和由后向前输出的话可以利用ListIterator接口完成

ListIterator接口的扩充方法

No

方法名称

描述

1

public boolean hasPrevious()

判断是否有前一个元素

2

public E previous()

取出前一个元素

如果要取得ListIterator接口的实例化对象,只能后依靠List接口,在List接口中存在方法,为ListIterator接口实例化:public ListIteratorlistIterator()

例:执行双向迭代 import java.util.ArrayList; import java.util.List; import java.util.ListIterator; public class test{ public static void main(String[] args) { List<String> all=new ArrayList<String>(); all.add("hello"); all.add("hello"); all.add("world"); ListIterator<String>iter =all.listIterator(); System.out.print("从前向后输出"); while(iter.hasNext()){ String str = iter.next(); System.out.print(str+" "); } System.out.print("\n"+"从后向前输出"); while(iter.hasPrevious()){ String str =iter.previous(); System.out.print(str+" "); } } } 程序运行结果: 从前向后输出hello hello world 从后向前输出world hello hello

对于由后向前的输出操作,在进行前一定要首先发生由前向后的输出。由于此输出接口只有List可以使用,所以在开发中几乎不会出现

4.3 Enumeration

Enumeration是最早的输出接口,最早称为枚举,在JDK1.0时已经推出,在JDK1.5的时候进行了扩充,主要增加了泛型,在Enumeration接口里面只定义了两个方法

No

方法名称

描述

1

public boolean hasMoreElements()

判断是否有下一个值

2

public E nextElement()

取出当前元素

要取得Enumeration的实例化对象,不能依靠Collection接口,只能依靠Vector,在Vector中定义了方法public Enumreationelements()

例:使用Enumreation进行输出 import java.util.Enumeration; import java.util.Vector; public class test{ public static void main(String[] args) { Vector<String> all=new Vector<String>(); all.add("hello"); all.add("hello"); all.add("world"); Enumeration<String>enu =all.elements(); System.out.print("从前向后输出"); while(enu.hasMoreElements()){ String str = enu.nextElement(); System.out.print(str+" "); } } } 程序结果输出:从前向后输出hello hello world

4.4 foreach

例foreach import java.util.ArrayList; import java.util.List; public class test{ public static void main(String[] args) { List<String> all=new ArrayList<String>(); all.add("hello"); all.add("hello"); all.add("world"); for(String str:all){ System.out.println(str+" "); } } }

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-04-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.单值保存的最大父接口:Collection
  • 2.允许重复的子接口:LIst
    • 2.1 ArrayList
      • 2.2 Vector
      • 3.不允许重复的子接口:Set
        • 3.1散列存放的子类:HashSet
          • 3.2排序存放的子类:TreeSet
            • TreeSet排序,自定义类排序,使用Comparable
        • 4.集合的输出操作
          • 4.1迭代输出:Iterator
            • 4.2 双向迭代输出:ListIterator(了解)
              • 4.3 Enumeration
                • 4.4 foreach
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档