day07(Set接口,HashSet类,hashcoad(),Collections工具类,Map集合)
Set接口
set接口的实现类特点
1.无序(取出来的顺序和存进去的数据的顺序不一致)
2.唯一(数据不能存相同的)
底层是用Map集合写的
HashSet类 实现了 set接口
唯一性
public class HashSetTest {
public static void main(String[] args) {
HashSet<String> hs=new HashSet<String>();
hs.add("hello");
hs.add("world");
hs.add("java");
hs.add("java");
for (String str : hs) {
System.out.println(str);
}
}
}
输出结果:
hello
java
world
最后一个 没有添加进去 因为已经存在了
hashcoad(),
为什么要重写:在没有重写前 我们在加入对象中时对象的地址值不同,但内容相同 。这样满足不了我们的需求,我们比较对象时一般会比较内容是否相同,很少的情况下会比较的是地址值 ,所以我们为了比较对象的内容 我们需要调用重写之后hashCoad();
public class Student {
private String name;
private int age;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
public class Demo01 {
public static void main(String[] args) {
Set<Student> set=new HashSet<Student>();
Student s1=new Student("赵云",15);
Student s2=new Student("关羽",16);
Student s3=new Student("张飞",12);
Student s4=new Student("张飞",12);
set.add(s1);
set.add(s2);
set.add(s3);
set.add(s4);
//Collections.sort(set);
for (Student student : set) {
System.out.println(student);
}
}
}
输出结果:
Student [name=赵云, age=15]
Student [name=张飞, age=12]
Student [name=张飞, age=12]
Student [name=关羽, age=16]
在Student类中重写hashCoad()之后 它会比较对象的内容
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
输出结果:
Student [name=赵云, age=15]
Student [name=张飞, age=12]
Student [name=关羽, age=16]
Collections工具类,
对集合进行操作的工具类
//使用sort();进行排序
三种方法
1. 让类实现comparable接口 并且实现 compareTo();
使用规则Collections.sort(List<T> list);
2.自己定义一个比较器 让这个类实现comparator<T>
public class MyCompare implements Comparator<Student>{
@Override
public int compare(Student o1, Student o2) {
return o1.getAge()-o2.getAge();
}
}
调用方法 Collections.sort(List<T> list,new Comparator);
3.使用匿名内部类
Collections.sort(set,new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o1.getAge()-o2.getAge();
}
});
//使用二分查找法进行集合中的元素(必须是排完序之后的集合)binarySearch(List list,T Key)
首先在让Student实现compatable接口 然后实现compareTo();
public class Student implements Comparable<Student>{
private String name;
private int age;
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public int compareTo(Student o) {//按照名字进行排序 名字相同再进行年龄进行排序。
Collator cot=Collator.getInstance(Locale.CHINA);
if (this.getName().equals(o.getName())) {
return this.getAge()-o.getAge();
}else {
return cot.compare(this.getName(),o.getName());
}
}
}
public class Demo01 {
public static void main(String[] args) {
List<Student> set=new ArrayList<Student>();
Student s1=new Student("赵云",15);
Student s2=new Student("关羽",16);
Student s3=new Student("张飞",12);
Student s4=new Student("张飞",12);
set.add(s1);
set.add(s2);
set.add(s3);
set.add(s4);
Collections.sort(set);//进行排序
int i = Collections.binarySearch(set,new Student("赵云",15));
System.out.println("索引位置为:"+i);
}
}
输出结果:
索引位置为:3
//集合的复制(copy(des,src))
public class Demo01 {
public static void main(String[] args) {
List<Student> set=new ArrayList<Student>();
Student s1=new Student("赵云",15);
Student s2=new Student("关羽",16);
Student s3=new Student("张飞",12);
Student s4=new Student("张飞",12);
set.add(s1);
set.add(s2);
set.add(s3);
set.add(s4);
Collections.sort(set);
List<Student> list=new ArrayList<Student>();
list.add(s1);
list.add(s1);
list.add(s1);
list.add(s1);
Collections.copy(list, set);//将set集合中的值复制到list集合中
for (Student student : list) {//遍历新集合
System.out.println(student);
}
}
}
//使用指定对象填充指定集合的所有元素 fill();
public class Demo01 {
public static void main(String[] args) {
List<Student> set=new ArrayList<Student>();
Student s1=new Student("赵云",15);
Student s2=new Student("关羽",16);
Student s3=new Student("张飞",12);
Student s4=new Student("张飞",12);
set.add(s1);
set.add(s2);
set.add(s3);
set.add(s4);
Collections.sort(set);
List<Student> list=new ArrayList<Student>();
list.add(s1);
list.add(s1);
list.add(s1);
list.add(s1);
Collections.fill(list, s2);
for (Student student : list) {//遍历新集合
System.out.println(student);
}
}
}
输出结果:
Student [name=关羽, age=16]
Student [name=关羽, age=16]
Student [name=关羽, age=16]
Student [name=关羽, age=16]
// (打乱:不是按照存储顺序输出 )
public class Demo01 {
public static void main(String[] args) {
List<Student> set=new ArrayList<Student>();
Student s1=new Student("赵云",15);
Student s2=new Student("关羽",16);
Student s3=new Student("张飞",12);
Student s4=new Student("张飞",12);
set.add(s1);
set.add(s2);
set.add(s3);
set.add(s4);
Collections.shuffle(set);
for (Student student : set) {//遍历新集合
System.out.println(student);
}
}
}
输出结果一:
Student [name=张飞, age=12]
Student [name=赵云, age=15]
Student [name=张飞, age=12]
Student [name=关羽, age=16]
输出结果二:
Student [name=关羽, age=16]
Student [name=张飞, age=12]
Student [name=赵云, age=15]
Student [name=张飞, age=12]
两次打乱顺序不一致。
//集合翻转
public class Demo01 {
public static void main(String[] args) {
List<Student> set=new ArrayList<Student>();
Student s1=new Student("赵云",15);
Student s2=new Student("关羽",16);
Student s3=new Student("张飞",12);
Student s4=new Student("张飞",12);
set.add(s1);
set.add(s2);
set.add(s3);
set.add(s4);
Collections.reverse(set);
for (Student student : set) {//遍历新集合
System.out.println(student);
}
}
}
输出结果:
Student [name=张飞, age=12]
Student [name=张飞, age=12]
Student [name=关羽, age=16]
Student [name=赵云, age=15]
Map集合
键值对,元素是成对存在的,每个元素由两个部分组成,键 和 值组成。通过键可以找对应的值
特点(注意事项):键是唯一的 值可以重复
功能:
获取功能:
get(Object key);//获取指定键对应的值
Collection<T> values();//获取所有值
eg:01
public class MapTest {
public static void main(String[] args) {
Map<String,String> map=new HashMap<String,String>();
map.put("it001", "赵云");
map.put("it002", "关羽");
map.put("it001", "我替换了it001的值");
String str = map.get("it001");
System.out.println(str);
}
}
输出结果:
我替换了it001的值
eg:02
public class MapTest {
public static void main(String[] args) {
Map<String,String> map=new HashMap<String,String>();
map.put("it001", "赵云");
map.put("it002", "关羽");
map.put("it001", "我替换了it001的值");
Collection<String> values= map.values();
for (String value : values) {
System.out.println(value);
}
}
}
输出结果:
关羽
我替换了it001的值
映射:
put(Object o1,Object o2)
如果键不重复,则视为添加。如果键重复,则视为修改
public class MapTest {
public static void main(String[] args) {
Map<String,String> map=new HashMap<String,String>();
map.put("it001", "赵云");//添加成功返回null
map.put("it002", "关羽");
map.put("it001", "我替换了it001的值");//替换会返回被替换的键对应的值 即("赵云")
System.out.println(map);
}
}
输出结果:
{it002=关羽, it001=我替换了it001的值}
判断:
public class MapTest {
public static void main(String[] args) {
Map<String,String> map=new HashMap<String,String>();
map.put("it001", "赵云");
map.put("it002", "关羽");
map.put("it001", "我替换了it001的值");
boolean b = map.isEmpty();//判断是否为空
System.out.println(b);
boolean c = map.containsKey("it001");//判断是否存在该键
System.out.println(c);
boolean d = map.containsValue("关羽");//判断是否存在该值
System.out.println(d);
}
}
输出结果:
false
true
true
删除:
public class MapTest {
public static void main(String[] args) {
Map<String,String> map=new HashMap<String,String>();
map.put("it001", "赵云");
map.put("it002", "关羽");
map.put("it001", "我替换了it001的值");
String str = map.remove("it001");//返回为被移除的键对应的值
System.out.println(str);
System.out.println(map);
}
}
输出结果:
我替换了it001的值
{it002=关羽}
遍历:
两种方式: keyset(); 返回所有键 entrySet();返回所有的键值集合
public class MapTest {
public static void main(String[] args) {
Map<String,String> map=new HashMap<String,String>();
map.put("it001", "赵云");
map.put("it002", "关羽");
map.put("it001", "我替换了it001的值");
Set<String> keys = map.keySet();
for (String key : keys) {
System.out.println(key+"对应的值:"+map.get(key));
}
System.out.println("--------------------------");
Set<Entry<String,String>> entrys = map.entrySet();
for (Entry<String, String> entry : entrys) {
String key=entry.getKey();
String value=entry.getValue();
System.out.println(key+"对应的值:"+value);
}
}
}
输出结果:
it002对应的值:关羽
it001对应的值:我替换了it001的值
--------------------------
it002对应的值:关羽
it001对应的值:我替换了it001的值
day07(Set接口,HashSet类,hashcoad(),Collections工具类,Map集合)的更多相关文章
- LinkedHashSet、Map、Map接口HashMap、Hashtable,TreeSet、TreeMap、如何选择使用集合实现类,Collections工具类
一.Set接口实现类LinkedHashSet 实现继承图: 1.LinkedHashSet的全面说明 1) LinkedHashSet是 HashSet的子类 2) LinkedHashSet底层是 ...
- Arrays工具类和Collections工具类
集合知识点总结 Arrays工具类 .binarySearch() .sort() .fill() //填充 int[] array = new int[10]; Arrays.fill(array, ...
- Arrays工具类与Collections工具类
Arrays工具类 : Arrays.sort():对指定数组进行排序,从小到大 Arrays.toString():返回数组的内容的字符串表示形式 Arrays.asList():数组转List,但 ...
- Java精选笔记_集合概述(Collection接口、Collections工具类、Arrays工具类)
集合概述 集合有时又称为容器,简单地说,它是一个对象,能将具有相同性质的多个元素汇聚成一个整体.集合被用于存储.获取.操纵和传输聚合的数据. 使用集合的技巧 看到Array就是数组结构,有角标,查询速 ...
- 双列集合Map接口 & Collections工具类
HashMap 常用方法 遍历方式 iterator迭代器 ITIT HashTable 继承字典 Hashtable--Properties 文件读写 总结 Collections工具类
- JAVA Collections工具类sort()排序方法
主要分析内容: 一.Collections工具类两种sort()方法 二.示例 一.Collections工具类两种sort()方法 格式一: public static <T extends ...
- Java:集合,Collections工具类用法
Collections工具类提供了大量针对Collection/Map的操作,总体可分为四类,都为静态(static)方法: 1. 排序操作(主要针对List接口相关) reverse(List li ...
- [19/03/27-星期三] 容器_Iterator(迭代器)之遍历容器元素(List/Set/Map)&Collections工具类
一.概念 迭代器为我们提供了统一的遍历容器的方式 /* *迭代器遍历 * */ package cn.sxt.collection; import java.security.KeyStore.Ent ...
- Java集合——Collections工具类
Java集合——Collections工具类 摘要:本文主要学习了Collections工具类的常用方法. 概述 Collections工具类主要用来操作集合类,比如List和Set. 常用操作 排序 ...
随机推荐
- 原生nodejs 学习笔记1
网上许多nodejs教程或书藉都是教你调用第三方模块来编写nodejs应用的,虽然这是非常便捷的,但是封装太厚,你基本一点东西还是没有学到.人家的模块,人家想怎么改就行,可以下一版本就改了接口,你的应 ...
- 罗伯特•盖洛博士(Dr. Robert Charles Gallo)是世界著名的美国生物医学家,他以共同发现了人类免疫缺陷病毒(HIV)――这一导致获得性免疫缺陷综合症(AIDS)的致病源而闻名于世。
罗伯特•盖洛 开放分类:各国生物学家|生物学家罗伯特•盖洛博士(Dr. Robert Charles Gallo)是世界著名的美国生物医学家,他以共同发现了人类免疫缺陷病毒(HIV)――这一导致获得性 ...
- T分布(T-Distribution)
1.What is the T Distribution? T分布(也叫Student 's T分布)是一组与正态分布曲线几乎相同的分布,只是更短更胖一点.当有小样本时,使用t分布而不是正态分布.样本 ...
- css3将图片、内容换为灰色
直接用filter属性-webkit-filter: grayscale(100%);-moz-filter: grayscale(100%);-ms-filter: grayscale(100%); ...
- 53. Maximum Subarray (Array; DP)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- 【英宝通Unity4.0公开课学习 】(一)资源管理
经过多次面试后发现自己对Unity3D的框架缺乏一个整体的认识. 而前面由于离职等原因总是忙于修修补补,疲于奔命,感觉相当疲惫. 还好,后来经过调整,开始淡定了起来.得特别感谢一本书哇:<高效人 ...
- .net使用httpHandler添加图片防盗链
.net使用httpHandler添加图片防盗链1. 配置web.config: <!--图片添加水印的配置--> <httpHandlers> <add verb=&q ...
- 使用DW工具给图片添加热点MAP
一.准备一张图片. 准备一张需要给不同区域添加不同热点的图片. 二.插入图片: 打开Dreamweaver,新建一个网页,将图片插入到页面中. 三.找到地图工具: 单击鼠标左键点击图片,这时候 ...
- iOS - OC - 字典快速遍历
1. [dic enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop ...
- 20-java 对象链表空没空呢
写了一个 对象链表,往里面add了一些对象,最后我想看下链表是否为空,用 == null 为假,也看不出, 看下长度? 好吧, size() = 1: 打印 null , 那到底是不是空 啊, ...