核心技术java基础_JAVA核心技术I---JAVA基础知识(集合set)

一:集合了解

(一)确定性,互异性,无序性

确定性:对任意对象都能判定其是否属于某一个集合

互异性:集合内每个元素都是无差异的,注意是内容差异

无序性:集合内的顺序无关

(二)集合接口HashSet,TreeSet,LinkedHashSet

–HashSet (基于散列函数的集合,无序,不支持同步)

–TreeSet (基于树结构的集合,可排序的,不支持同步)

–LinkedHashSet(基于散列函数和双向链表的集合,可排序的,不支持同步

3e01e692354509759451a02b36498d23.png

二:HashSet

(一)基础方法

–基于HashMap实现的,可以容纳null元素, 不支持同步

Set s= Collections.synchronizedSet(newHashSet(...));

–add 添加一个元素

–clear 清除整个HashSet

–contains 判定是否包含一个元素

–remove 删除一个元素 size 大小

–retainAll 计算两个集合交集

(二)HashSet实现

HashSet hs = new HashSet(); //<>是泛型编程,类似于C++模板

hs.add(null);

hs.add(10000);

hs.add(22);

hs.add(1010);

hs.add(50001010);

hs.add(101035);

hs.add(3);

System.out.println(hs.size());if(!hs.contains(6)) {

hs.add(6);

}

System.out.println(hs.size());

hs.remove(4);  //存在,则删除,不存在,则不操作for(Integer item : hs) {

System.out.println(item);

}

7

8

null  //无序性

10000

1010

3

22

6

50001010

101035

(三)性能测试:因为无序性,无索引操作。for效率高

public static void trverseByIterator(HashSeths) {//使用迭代器遍历

System.out.println("==========迭代器遍历===========");long startTime = System.nanoTime(); //获取开始时间,以纳秒为单位返回正在运行的Java虚拟机的高分辨率时间源的当前值。

Iterator iter = hs.iterator(); //获取迭代指针

while(iter.hasNext()) {

iter.next();

}long endTime =System.nanoTime();long duration = endTime-startTime;

System.out.println("iterator使用纳秒:"+duration);

}public static void trverseByFor(HashSeths) {//使用迭代器遍历

System.out.println("==========for索引遍历===========");long startTime = System.nanoTime(); //获取开始时间,以纳秒为单位返回正在运行的Java虚拟机的高分辨率时间源的当前值。

for(Integer item : hs) {

;

}long endTime = System.nanoTime(); //获取开始时间,以纳秒为单位返回正在运行的Java虚拟机的高分辨率时间源的当前值。

long duration = endTime-startTime;

System.out.println("for使用纳秒:"+duration);

}

==========迭代器遍历===========iterator使用纳秒:5738665

==========for索引遍历===========for使用纳秒:2721950

(四)retainAll交集测试

//测试交集

HashSet hs1 = new HashSet();

HashSet hs2 = new HashSet();

hs1.add("a");

hs1.add("b");

hs1.add("c");

hs2.add("c");

hs2.add("d");

hs2.add("e");

hs1.retainAll(hs2);//将交集保存在hs1中

for(String item : hs1) {

System.out.println(item);

}

c

三:LinkedHashSet(与HashSet一致)

–继承HashSet,也是基于HashMap实现的,可以容纳null元素,按照插入顺序有序

–不支持同步

Set s= Collections.synchronizedSet(newLinkedHashSet(...));

–方法和HashSet基本一致

add, clear, contains, remove, size

–通过一个双向链表维护插入顺序

四:TreeSet

(一)基本方法

–基于TreeMap实现的,不可以容纳null元素,不支持同步

SortedSet s= Collections.synchronizedSortedSet(newTreeSet(...));

–add 添加一个元素

–clear 清除整个TreeSe

–contains 判定是否包含一个元素

–remove 删除一个元素 size 大小

–根据compareTo方法或指定Comparator排序

(二)实现(有序,会自动排序,红黑树)

TreeSet ts = new TreeSet(); //<>是泛型编程,类似于C++模板

ts.add(1000);

ts.add(15300);

ts.add(100);

ts.add(3);

ts.add(566000);if(!ts.contains(4)) {

ts.add(4);

}

for(Integer item : ts) {

System.out.println(item);;

}

4

100

1000

15300

566000

(三)性能测试:for更加高效

==========迭代器遍历===========iterator使用纳秒:9246423

==========for索引遍历===========for使用纳秒:3366874

五:HashSet, LinkedHashSet, TreeSet对象比较(元素重复)《重点》

(一)HashSet和LinkedHashSet判定元素重复的原则

–判定两个元素的hashCode返回值是否相同,若不同,返回false

–若两者hashCode相同,判定equals方法,若不同,返回false;否则返回true。

–hashCode和equals方法是所有类都有的,因为Object类有

比较之前会先调用hashCode,之后是equals方法

1.正常执行,含重复

classDog{intage;public Dog(inta) {this.age=a;

}

}public classCompareTest {public static voidmain(String[] args) {

Dog d1=new Dog(10);

Dog d2=new Dog(10);

HashSet hs=new HashSet();

hs.add(new Dog(10));

hs.add(new Dog(1));

hs.add(new Dog(3));

hs.add(new Dog(10));

hs.add(new Dog(10));

System.out.println(hs.size());

}

}

5

Dog类本身没有hashCode方法,继承于Object,而Object类的hashCOde会返回对象信息和内存地址经过运算后的一个值。两个不同对象,其值必然不一致

2.实现对象的hashCode方法和equals方法实现去重

import java.util.*;classDog{intage;public Dog(inta) {this.age=a;

}public intgetAge() {return this.age;

}public inthashCode() {

System.out.println("hashCode exec...");return this.age;

}publicboolean equals(Object obj2) {

System.out.println("equals exec...");if(0==this.age-((Dog)obj2).getAge())return true;else

return false;

}

}public classCompareTest {public static voidmain(String[] args) {

Dog d1=new Dog(10);

Dog d2=new Dog(10);

HashSet hs=new HashSet();

hs.add(new Dog(10));

hs.add(new Dog(1));

hs.add(new Dog(3));

hs.add(new Dog(10));

hs.add(new Dog(10));

System.out.println(hs.size());

}

}

hashCode exec...

hashCode exec...

hashCode exec...

hashCode exec...

equals exec...

hashCode exec...

equals exec...3  //去重实现

先执行hashCode,只有hashCode通过,才会执行equals方法

publicString toString() {

System.out.println("toString exec...");return age+"";

}

要保持equals,hashCode和toString三位一体。都应该各自相同

(二) TreeSet去重

添加到TreeSet,需要实现Comparable接口,即实现compareTo方法

与hashCode和equals无关,只与compareTo有关

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

import java.util.*;classDog implements Comparable{intage;public Dog(inta) {this.age=a;

}public intgetAge() {return this.age;

}public inthashCode() {

System.out.println("hashCode exec...");return this.age;

}publicboolean equals(Object obj2) {

System.out.println("equals exec...");if(0==this.age-((Dog)obj2).getAge())return true;else

return false;

}publicString toString() {

System.out.println("toString exec...");return age+"";

}public intcompareTo(Object obj2) {

System.out.println("compareTo exec...");return this.age -((Dog)obj2).getAge();

}

}public classCompareTest {public static voidmain(String[] args) {

Dog d1=new Dog(10);

Dog d2=new Dog(10);

TreeSet hs=new TreeSet();

hs.add(new Dog(10));

hs.add(new Dog(1));

hs.add(new Dog(3));

hs.add(new Dog(10));

hs.add(new Dog(10));

System.out.println(hs.size());

}

}

View Code

compareTo exec...

compareTo exec...

compareTo exec...

compareTo exec...

compareTo exec...

compareTo exec...

compareTo exec...

compareTo exec...3

可以知道,去重和hashCode与equals无关,不执行。而是直接去找compareTo方法

六:总结

(一)HashSet, LinkedHashSet, TreeSet的元素都只能是对象

会进行自动装箱

(二)HashSet和LinkedHashSet判定元素重复的原则《重点》

–判定两个元素的hashCode返回值是否相同,若不同,返回false

–若两者hashCode相同,判定equals方法,若不同,返回false;否则返回true。

–hashCode和equals方法是所有类都有的,因为Object类有

(三)TreeSet判定元素重复的原则《重点》

–需要元素继承自Comparable接口

–比较两个元素的compareTo方法

(四)注意:对于基本类型的包装类。本来就实现了compareTo接口和其他比较方法,所以HashSet,LinkedHashSet,TreeSet中对于包装类是默认去重的

c87bb14eea71500140e9d0e14c223e1e.png

cb2f1a26d66cacd6d8ad1953727ac81e.png

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/392410.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

nba数据库统计_NBA板块的价值-从统计学上讲

nba数据库统计The idea is not to block every shot. The idea is to make your opponent believe that you might block every shot. — Bill Russel这个想法不是要阻止每一个镜头。 这个想法是让你的对手相信你可能会阻挡每一个投篮。 —比尔罗素 The block in basketball ha…

leetcode 330. 按要求补齐数组(贪心算法)

给定一个已排序的正整数数组 nums&#xff0c;和一个正整数 n 。从 [1, n] 区间内选取任意个数字补充到 nums 中&#xff0c;使得 [1, n] 区间内的任何数字都可以用 nums 中某几个数字的和来表示。请输出满足上述要求的最少需要补充的数字个数。 示例 1: 输入: nums [1,3], …

【炼数成金 NOSQL引航 三】 Redis使用场景与案例分析

验证redis的主从复制&#xff0c;将实验过程抓图 复制配置文件 更改slave的端口 和相关master配置 主从复制测试 研究在OAuth中的“一次数”nonce有什么用途&#xff1f;怎样使用&#xff1f;以此熟悉OAuth的全流程 nonce &#xff0c;一个随机的混淆字符串&#xff0c;仅仅被…

SQL Server需要监控哪些计数器 ---指尖流淌

http://www.cnblogs.com/zhijianliutang/p/4174697.html转载于:https://www.cnblogs.com/zengkefu/p/7044095.html

akka 简介_Akka HTTP路由简介

akka 简介by Miguel Lopez由Miguel Lopez Akka HTTP路由简介 (An introduction to Akka HTTP routing) Akka HTTP’s routing DSL might seem complicated at first, but once you get the hang of it you’ll see how powerful it is.Akka HTTP的路由DSL乍一看似乎很复杂&…

leetcode 1046. 最后一块石头的重量(堆)

有一堆石头&#xff0c;每块石头的重量都是正整数。 每一回合&#xff0c;从中选出两块 最重的 石头&#xff0c;然后将它们一起粉碎。假设石头的重量分别为 x 和 y&#xff0c;且 x < y。那么粉碎的可能结果如下&#xff1a; 如果 x y&#xff0c;那么两块石头都会被完全…

java2d方法_Java SunGraphics2D.fillRect方法代码示例

import sun.java2d.SunGraphics2D; //导入方法依赖的package包/类/*** Return a non-accelerated BufferedImage of the requested type with the* indicated subimage of the original image located at 0,0 in the new image.* If a bgColor is supplied, composite the orig…

js建立excel表格_建立Excel足球联赛表格-传统vs动态数组方法

js建立excel表格介绍 (Introduction) I am going to show you the different ways you can build a football league table in Excel. Some of the methods are old school but others utilise Excel’s new capabilities.我将向您展示在Excel中建立足球联赛表格的不同方法。 其…

postman+newman生成html报告

作为测试菜鸟,在学习postmannewman的使用过程中真的是颇费周折......没办法技术太菜,只能多学习. postman的下载安装不多言说,下载地址:https://www.getpostman.com/downloads/ newman的安装过程: 1.首先需要安装node.js,可以去官网下载,地址:https://nodejs.org/en/#download …

java jdk1.9新特性_JDK1.9-新特性

1. Java平台级模块系统该特性使Java9最大的一个特性&#xff0c;Java提供该功能的主要的动机在于&#xff0c;减少内存的开销&#xff0c;JVM启动的时候&#xff0c;至少会有30~60MB的内存加载&#xff0c;主要原因是JVM需要加载rt.jar&#xff0c;不管其中的类是否被classload…

如何在10分钟内让Redux发挥作用

Hi everyone ❤️大家好❤️ For a while now I’ve been hearing my friends and colleagues complaining about how hard it was to get into Redux.一段时间以来&#xff0c;我一直在听我的朋友和同事抱怨进入Redux有多困难。 I run a freeCodeCamp Study Group in the So…

两个链接合并_如何找到两个链接列表的合并点

两个链接合并了解问题 (Understand the Problem) We are given two singly linked lists and we have to find the point at which they merge.我们给了两个单链表&#xff0c;我们必须找到它们合并的点。 [SLL 1] 1--->3--->5 \ …

安装veket到移动硬盘NTFS分区

如果你已经看过《手动安装veket到硬盘》和《简单的将veket安装到U盘的方法》两篇文章并且安装成功的话&#xff0c;说明不适用本文的安装环境&#xff0c;就不用往下看了。 《手动安装veket到硬盘》一文采用grub4dos来引导硬盘上的veket&#xff0c;主要是用来在本机已安装Wind…

简书使用小技巧

1、不同字体  在 设置->基础设置->富文本 模式下可以实现 2、添加图片&#xff0c;让文章更生动 3、添加代码框 &#xff01;注意&#xff1a;设置为Markdown模式后&#xff0c;只对新创建的文章起作用。转载于:https://www.cnblogs.com/HMJ-29/p/7049540.html

掩码 项目编码_每天进行20天的编码项目

掩码 项目编码by Angela He通过何安佳 每天进行20天的编码项目 (A coding project a day for 20 days) 我如何在20天内自学Web开发 (How I taught myself web development in 20 days) It was the first day of winter break for Stanford students. Back at home, I opened a…

java循环一年月份天数和_javawhile循环编写输入某年某月某日,判断这一天是这一年的第几…...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼public class ZuoYe9 {public static void main(String[] args) {int days0; //存储变量这一年的第几天//1.输入年&#xff0c;月&#xff0c;日Scanner inputnew Scanner(System.in);System.out.println("请输入年份&#xf…

leetcode 605. 种花问题(贪心算法)

假设你有一个很长的花坛&#xff0c;一部分地块种植了花&#xff0c;另一部分却没有。可是&#xff0c;花卉不能种植在相邻的地块上&#xff0c;它们会争夺水源&#xff0c;两者都会死去。 给定一个花坛&#xff08;表示为一个数组包含0和1&#xff0c;其中0表示没种植花&…

工程师的成熟模型_数据工程师的成熟度

工程师的成熟模型数据科学与机器学习 (DATA SCIENCE AND MACHINE LEARNING) What does a data engineer do?数据工程师做什么&#xff1f; Let’s start with three big wars that we need to understand before understanding what a data engineer does.让我们从理解数据工…

杭电2064

此题是一道简单的递归 此题是一道递归运算题&#xff0c;这题又是一道汉诺塔问题&#xff01;&#xff01;&#xff01;只要了解其规律&#xff0c;呵呵&#xff0c;你就可以很快AC了&#xff01;&#xff01; 这是一般的汉诺塔问题的解题方法照片&#xff01;&#xff01;&…

/ ./ ../ 的区别

/ 根目录 &#xff08;绝对路径&#xff09; ./ 当前目录 ../父级目录 &#xff08;相对路径&#xff09; ./home是当前目录下的一个叫home的目录/home是绝对路径的/home就是根下的home目录转载于:https://www.cnblogs.com/sjd1118/p/7055475.html