关于Java Vector 类的讲解
Vector 类实现了一个动态数组。和 ArrayList 很相似,但是两者是不同的:
Vector 主要用在事先不知道数组的大小,或者只是需要一个可以改变大小的数组的情况。
Vector 类支持 4 种构造方法。
第一种构造方法创建一个默认的向量,默认大小为 10:Vector()
第二种构造方法创建指定大小的向量:Vector(int size)
第三种构造方法创建指定大小的向量,并且增量用 incr 指定。增量表示向量每次增加的元素数目:Vector(int size,int incr)
第四种构造方法创建一个包含集合 c 元素的向量:Vector(Collection c)
接下来回顾一下昨天的没有完成的ArrayList,今天重新整理了一下,昨天一开始不适合理解那个任务要求:
1:使用ArrayList
ArrayList追加元素耗时:5
ArrayList添加元素到任意位置耗时:1453
ArrayList之for 循环遍历;耗时:379
ArrayList之foreach耗时:796
ArrayList迭代器遍历耗时:782
ArrayList删除任意位置的元素耗时:1400
package 任务集合.ArrayLiat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;
/**
* @author ${范涛之}
* @Description
* @create 2021-11-30 11:32
*/
public class Test1 {
public static void main(String[] args) {
ArrayList<Integer> arrayList = new ArrayList<Integer>();
Long start1 = System.currentTimeMillis();
/**
* 追加元素
*/
for (int i = 0; i <100000; i++) {
arrayList.add(0);
}
Long end1 = System.currentTimeMillis();
/**
* 添加元素到任意位置
*/
Long start2 = System.currentTimeMillis();
for (int i = 0; i <100000; i++) {
arrayList.add(0,1);
}
Long end2 = System.currentTimeMillis();
/**
* for 循环遍历;
*/
Long start3 = System.currentTimeMillis();
for (int i = 0; i <100000; i++) {
System.out.println(arrayList.get(i));
}
Long end3 = System.currentTimeMillis();
/**
* foreach循环遍历
*/
Long start4 = System.currentTimeMillis();
for (int i:arrayList){
System.out.println(i);
}
Long end4 = System.currentTimeMillis();
/**
* 使用迭代器遍历
*/
Long start5 = System.currentTimeMillis();
Iterator iterator = arrayList.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
Long end5 = System.currentTimeMillis();
/**
* 删除任意位置的元素
*/
Long start6 = System.currentTimeMillis();
for (int i = 0; i <100000; i++) {
arrayList.remove(0);
}
Long end6 = System.currentTimeMillis();
System.out.println("ArrayList追加元素耗时:"+(end1-start1));
System.out.println("ArrayList添加元素到任意位置耗时:"+(end2-start2));
System.out.println("ArrayList之for 循环遍历;耗时:"+(end3-start3));
System.out.println("ArrayList之foreach耗时:"+(end4-start4));
System.out.println("ArrayList迭代器遍历耗时:"+(end5-start5));
System.out.println("ArrayList删除任意位置的元素耗时:"+(end6-start6));
}
}
打印结果:
2:使用Vector
package 任务集合.Vector;
import java.util.Iterator;
import java.util.Vector;
/**
* @author ${范涛之}
* @Description
* @create 2021-11-30 11:51
*/
public class Test1 {
public static void main(String[] args) {
Vector<Integer> vector = new Vector<Integer>();
/**
* 追加元素
*/
Long start1 = System.currentTimeMillis();
for (int i = 0; i <100000; i++) {
vector.add(0);
}
Long end1 = System.currentTimeMillis();
/**
* 添加元素到任意位置
*/
Long start2 = System.currentTimeMillis();
for (int i = 0; i <100000; i++) {
vector.add(0,1);
}
Long end2 = System.currentTimeMillis();
/**
* for 循环遍历;
*/
Long start3 = System.currentTimeMillis();
for (int i = 0; i <100000; i++) {
System.out.println(vector.get(i));
}
Long end3 = System.currentTimeMillis();
/**
* foreach循环遍历
*/
Long start4 = System.currentTimeMillis();
for (int i:vector){
System.out.println(i);
}
Long end4 = System.currentTimeMillis();
/**
* 使用迭代器遍历
*/
Long start5 = System.currentTimeMillis();
Iterator iterator = vector.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
Long end5 = System.currentTimeMillis();
/**
* 删除任意位置的元素
*/
Long start6 = System.currentTimeMillis();
for (int i = 0; i <100000; i++) {
vector.remove(0);
}
Long end6 = System.currentTimeMillis();
System.out.println("vector追加元素耗时:"+(end1-start1));
System.out.println("vectort添加元素到任意位置耗时:"+(end2-start2));
System.out.println("vector之for 循环遍历;耗时:"+(end3-start3));
System.out.println("vector之foreach耗时:"+(end4-start4));
System.out.println("vector迭代器遍历耗时:"+(end5-start5));
System.out.println("vector删除任意位置的元素耗时:"+(end6-start6));
}
}
运行结果:
LinkedList:
package 任务集合.LinkList;
import java.util.Iterator;
import java.util.LinkedList;
/**
* @author ${范涛之}
* @Description
* @create 2021-11-30 19:47
*/
public class Test1 {
public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<Integer>();
Long start1 = System.currentTimeMillis();
/**
* 追加元素
*/
for (int i = 0; i <100000; i++) {
list.add(0);
}
Long end1 = System.currentTimeMillis();
/**
* 添加元素到任意位置
*/
Long start2 = System.currentTimeMillis();
for (int i = 0; i <100000; i++) {
list.add(0,1);
}
Long end2 = System.currentTimeMillis();
/**
* for 循环遍历;
*/
Long start3 = System.currentTimeMillis();
for (int i = 0; i <100000; i++) {
System.out.println(list.get(i));
}
Long end3 = System.currentTimeMillis();
/**
* foreach循环遍历
*/
Long start4 = System.currentTimeMillis();
for (int i:list){
System.out.println(i);
}
Long end4 = System.currentTimeMillis();
/**
* 使用迭代器遍历
*/
Long start5 = System.currentTimeMillis();
Iterator iterator = list.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
Long end5 = System.currentTimeMillis();
/**
* 删除任意位置的元素
*/
Long start6 = System.currentTimeMillis();
for (int i = 0; i <100000; i++) {
list.remove(0);
}
Long end6 = System.currentTimeMillis();
System.out.println(" LinkedList追加元素耗时:"+(end1-start1));
System.out.println(" LinkedList添加元素到任意位置耗时:"+(end2-start2));
System.out.println(" LinkedList之for 循环遍历;耗时:"+(end3-start3));
System.out.println(" LinkedList之foreach耗时:"+(end4-start4));
System.out.println(" LinkedList迭代器遍历耗时:"+(end5-start5));
System.out.println(" LinkedList删除任意位置的元素耗时:"+(end6-start6));
}
}
运行结果:
三次打印结果统计:
ArrayList追加元素耗时:5
ArrayList添加元素到任意位置耗时:1453
ArrayList之for 循环遍历;耗时:379
ArrayList之foreach耗时:796
ArrayList迭代器遍历耗时:782
ArrayList删除任意位置的元素耗时:1400
vector追加元素耗时:5
vectort添加元素到任意位置耗时:1458
vector之for 循环遍历;耗时:394
vector之foreach耗时:812
vector迭代器遍历耗时:812
vector删除任意位置的元素耗时:1466
LinkedList追加元素耗时:5
LinkedList添加元素到任意位置耗时:2
LinkedList之for 循环遍历;耗时:9137
LinkedList之foreach耗时:818
LinkedList迭代器遍历耗时:786
LinkedList删除任意位置的元素耗时:3
通过比较我们可以发现:
LinkList的for 循环遍历耗时是特别长的!但是LinkList的删除任意元素位置超级快!
分割线·······················································································
关于HashSet和TreeSet类的讲解:
Set 集合类似于一个罐子,程序可以依次把多个对象“丢进”Set 集合,而 Set 集合通常不能记住元素的添加顺序。也就是说 Set 集合中的对象不按特定的方式排序,只是简单地把对象加入集合。Set 集合中不能包含重复的对象,并且最多只允许包含一个 null 元素。
Set 实现了 Collection 接口,它主要有两个常用的实现类:HashSet 类和 TreeSet类。
HashSet 类:HashSet 是 Set 接口的典型实现,大多数时候使用 Set 集合时就是使用这个实现类。HashSet 是按照 Hash 算法来存储集合中的元素。因此具有很好的存取和查找性能。
HashSet 具有以下特点:
-
不能保证元素的排列顺序,顺序可能与添加顺序不同,顺序也有可能发生变化。
-
HashSet 不是同步的,如果多个线程同时访问或修改一个 HashSet,则必须通过代码来保证其同步。
-
集合元素值可以是 null。
HashSet hs = new HashSet(); // 调用无参的构造函数创建HashSet对象
HashSet<String> hss = new HashSet<String>(); // 创建泛型的 HashSet 集合对象
编写一个 Java 程序,使用 HashSet 创建一个 Set 集合,并向该集合中添加 4 套教程。具体实现代码如下:
public static void main(String[] args) {
HashSet<String> courseSet = new HashSet<String>(); // 创建一个空的 Set 集合
String course1 = new String("Java入门教程");
String course2 = new String("Python基础教程");
String course3 = new String("C语言学习教程");
String course4 = new String("Golang入门教程");
courseSet.add(course1); // 将 course1 存储到 Set 集合中
courseSet.add(course2); // 将 course2 存储到 Set 集合中
courseSet.add(course3); // 将 course3 存储到 Set 集合中
courseSet.add(course4); // 将 course4 存储到 Set 集合中
System.out.println("C语言中文网教程有:");
Iterator<String> it = courseSet.iterator();
while (it.hasNext()) {
System.out.println("《" + (String) it.next() + "》"); // 输出 Set 集合中的元素
}
System.out.println("有" + courseSet.size() + "套精彩教程!");
如上述代码,首先使用 HashSet 类的构造方法创建了一个 Set 集合,接着创建了 4 个 String 类型的对象,并将这些对象存储到 Set 集合中。使用 HashSet 类中的 iterator() 方法获取一个 Iterator 对象,并调用其 hasNext() 方法遍历集合元素,再将使用 next() 方法读取的元素强制转换为 String 类型。最后调用 HashSet 类中的 size() 方法获取集合元素个数。
TreeSet 类:
TreeSet 类同时实现了 Set 接口和 SortedSet 接口。SortedSet 接口是 Set 接口的子接口,可以实现对集合进行自然排序,因此使用 TreeSet 类实现的 Set 接口默认情况下是自然排序的,这里的自然排序指的是升序排序。
TreeSet 只能对实现了 Comparable 接口的类对象进行排序,因为 Comparable 接口中有一个 compareTo(Object o) 方法用于比较两个对象的大小。例如 a.compareTo(b),如果 a 和 b 相等,则该方法返回 0;如果 a 大于 b,则该方法返回大于 0 的值;如果 a 小于 b,则该方法返回小于 0 的值。
表 1 列举了 JDK 类库中实现 Comparable 接口的类,以及这些类对象的比较方式。
TreeSet 类除了实现 Collection 接口的所有方法之外,还提供了如表 2 所示的方法。:
注意:表面上看起来这些方法很多,其实很简单。因为 TreeSet 中的元素是有序的,所以增加了访问第一个、前一个、后一个、最后一个元素的方法,并提供了 3 个从 TreeSet 中截取子 TreeSet 的方法。
本次有 5 名学生参加考试,当老师录入每名学生的成绩后,程序将按照从低到高的排列顺序显示学生成绩。此外,老师可以查询本次考试是否有满分的学生存在,不及格的成绩有哪些,90 分以上成绩的学生有几名:
public class Test08 {
public static void main(String[] args) {
TreeSet<Double> scores = new TreeSet<Double>(); // 创建 TreeSet 集合
Scanner input = new Scanner(System.in);
System.out.println("------------学生成绩管理系统-------------");
for (int i = 0; i < 5; i++) {
System.out.println("第" + (i + 1) + "个学生成绩:");
double score = input.nextDouble();
// 将学生成绩转换为Double类型,添加到TreeSet集合中
scores.add(Double.valueOf(score));
}
Iterator<Double> it = scores.iterator(); // 创建 Iterator 对象
System.out.println("学生成绩从低到高的排序为:");
while (it.hasNext()) {
System.out.print(it.next() + "\t");
}
System.out.println("\n请输入要查询的成绩:");
double searchScore = input.nextDouble();
if (scores.contains(searchScore)) {
System.out.println("成绩为: " + searchScore + " 的学生存在!");
} else {
System.out.println("成绩为: " + searchScore + " 的学生不存在!");
}
// 查询不及格的学生成绩
SortedSet<Double> score1 = scores.headSet(60.0);
System.out.println("\n不及格的成绩有:");
for (int i = 0; i < score1.toArray().length; i++) {
System.out.print(score1.toArray()[i] + "\t");
}
// 查询90分以上的学生成绩
SortedSet<Double> score2 = scores.tailSet(90.0);
System.out.println("\n90 分以上的成绩有:");
for (int i = 0; i < score2.toArray().length; i++) {
System.out.print(score2.toArray()[i] + "\t");
}
}
}
如上述代码,首先创建一个 TreeSet 集合对象 scores,并向该集合中添加 5 个 Double 对象。接着使用 while 循环遍历 scores 集合对象,输出该对象中的元素,然后调用 TreeSet 类中的 contains() 方法获取该集合中是否存在指定的元素。最后分别调用 TreeSet 类中的 headSet() 方法和 tailSet() 方法获取不及格的成绩和 90 分以上的成绩。
------------学生成绩管理系统-------------
第1个学生成绩:
53
第2个学生成绩:
48
第3个学生成绩:
85
第4个学生成绩:
98
第5个学生成绩:
68
学生成绩从低到高的排序为:
48.0 53.0 68.0 85.0 98.0
请输入要查询的成绩:
90
成绩为: 90.0 的学生不存在!
不及格的成绩有:
48.0 53.0
90 分以上的成绩有:
98.0
自我小总结:总之就是你在录入信息的时候子系统就自动按照升序排列,然后合理根据需求调用对应的方法即可!上面的两个表便说明了一切!
开始任务:
hashset:
package 任务集合.HashSet;
import java.util.HashSet;
import java.util.Iterator;
/**
* @author ${范涛之}
* @Description
* @create 2021-11-30 19:55
*/
public class Test1 {
public static void main(String[] args) {
HashSet<Integer> hashSet = new HashSet<Integer>();
/**
* 追加元素
*/
Long start1 = System.currentTimeMillis();
for (int i = 0; i <100000; i++) {
hashSet.add(i);
}
Long end1 = System.currentTimeMillis();
/**
* foreach循环遍历
*/
Long start4 = System.currentTimeMillis();
for (int i:hashSet){
System.out.println(i);
}
Long end4 = System.currentTimeMillis();
/**
* 使用迭代器遍历
*/
Long start5 = System.currentTimeMillis();
Iterator iterator = hashSet.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
Long end5 = System.currentTimeMillis();
/**
* 删除任意位置的元素
*/
Long start6 = System.currentTimeMillis();
for (int i = 0; i <100000; i++) {
hashSet.remove(0);
}
Long end6 = System.currentTimeMillis();
System.out.println(" HashSet追加元素耗时:"+(end1-start1));
System.out.println(" HashSet之foreach耗时:"+(end4-start4));
System.out.println(" HashSet迭代器遍历耗时:"+(end5-start5));
System.out.println(" HashSet删除任意位置的元素耗时:"+(end6-start6));
}
}
运行结果:
HashSet追加元素耗时:10
HashSet之foreach耗时:444
HashSet迭代器遍历耗时:511
HashSet删除任意位置的元素耗时:4
treeset:
package 任务集合.TreeSet;
import java.util.Iterator;
import java.util.TreeSet;
/**
* @author ${范涛之}
* @Description
* @create 2021-11-30 20:13
*/
public class Test1 {
public static void main(String[] args) {
TreeSet<Integer> treeSet = new TreeSet<Integer>();
/**
* 追加元素
*/
Long start1 = System.currentTimeMillis();
for (int i = 0; i <100000; i++) {
treeSet.add(i);
}
Long end1 = System.currentTimeMillis();
/**
* foreach循环遍历
*/
Long start4 = System.currentTimeMillis();
for (int i:treeSet){
System.out.println(i);
}
Long end4 = System.currentTimeMillis();
/**
* 使用迭代器遍历
*/
Long start5 = System.currentTimeMillis();
Iterator iterator = treeSet.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
Long end5 = System.currentTimeMillis();
/**
* 删除任意位置的元素
*/
Long start6 = System.currentTimeMillis();
for (int i = 0; i <100000; i++) {
treeSet.remove(0);
}
Long end6 = System.currentTimeMillis();
System.out.println(" HashSet追加元素耗时:"+(end1-start1));
System.out.println(" HashSet之foreach耗时:"+(end4-start4));
System.out.println(" HashSet迭代器遍历耗时:"+(end5-start5));
System.out.println(" HashSet删除任意位置的元素耗时:"+(end6-start6));
}
}
运行结果:
TreeSet追加元素耗时:22
TreeSet之foreach耗时:462
TreeSet迭代器遍历耗时:536
TreeSet删除任意位置的元素耗时:7
结果比较:
HashSet追加元素耗时:10
HashSet之foreach耗时:465
HashSet迭代器遍历耗时:517
HashSet删除任意位置的元素耗时:3
TreeSet追加元素耗时:22
TreeSet之foreach耗时:462
TreeSet迭代器遍历耗时:536
TreeSet删除任意位置的元素耗时:7
Map:
Java中有四种常见的Map实现,HashMap,TreeMap,HashTable和LinkedHashMap,我们可以使用一句话来描述各个Map,如下:
package 任务集合.Map;
import java.util.HashMap;
import java.util.Iterator;
/**
* @author ${范涛之}
* @Description
* @create 2021-11-30 20:18
*/
public class HashMapTest {
public static void main(String[] args) {
HashMap<Integer, Integer> hashMap = new HashMap<>();
/**
* 添加键值对i与i的平方
*/
Long start1 = System.currentTimeMillis();
for (int i =0;i<100000;i++){
hashMap.put(i,i*i);
}
Long end1 = System.currentTimeMillis();
/**
* 替换键值对i与i的平方
*/
Long start2 = System.currentTimeMillis();
for (int i =0;i<100000;i++){
hashMap.put(i,-i);
}
Long end2 = System.currentTimeMillis();
/**
* entrySet进行遍历;
*/
Long start3 = System.currentTimeMillis();
System.out.println(hashMap.entrySet());
Long end3 = System.currentTimeMillis();
/**
* keySet进行遍历;
*/
Long start4 = System.currentTimeMillis();
System.out.println(hashMap.keySet());
Long end4 = System.currentTimeMillis();
/**
* values进行遍历;
*/
Long start5 = System.currentTimeMillis();
System.out.println(hashMap.values());
Long end5 = System.currentTimeMillis();
/**
* 迭代器进行遍历;
*/
Long start6 = System.currentTimeMillis();
Iterator iterator = hashMap.entrySet().iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
Long end6 = System.currentTimeMillis();
//删除十万对键值对
Long start7 = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
hashMap.remove(i);
}
Long end7 = System.currentTimeMillis();
System.out.println(" HashMap添加键值对耗时:"+(end1-start1));
System.out.println(" HashMap替换键值对耗时:"+(end2-start2));
System.out.println(" HashSet通过entrySet进行遍历耗时:"+(end3-start3));
System.out.println(" HashSet通过keySet进行遍历耗时:"+(end4-start4));
System.out.println(" HashSet通过 values进行遍历耗时:"+(end5-start5));
System.out.println(" HashSet通过迭代器进行遍历耗时:"+(end6-start6));
System.out.println(" HashSet删除十万对键值对耗时:"+(end7-start7));
}
}
输出结果:
其他三种方法类似,只需要修改名字即可!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/81039.html