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. 常用操作 排序 ...
随机推荐
- dmidecode详解
1.DMI简介 DMI (Desktop Management Interface, DMI)就是帮助收集电脑系统信息的管理系统,DMI信息的收集必须在严格遵照SMBIOS规范的前提下进行. SMBI ...
- 软件工程导论复习 如何画系统流程图和数据流图 part2
数据流图(DFD) 数据流图,简称DFD,是SA方法中用于表示系统逻辑模型的一种工具,它以图形的方式描绘数据在系统中流动和处理的过程,由于它只反映系统必须完成的逻辑功能,所以它是一种功能模型.下 ...
- java 可伸缩阻塞队列实现
最近一年多写的最虐心的代码.必须好好复习java并发了.搞了一晚上终于测试都跑通过了,特此纪念,以资鼓励! import java.util.ArrayList; import java.util.L ...
- KKT条件的物理意义(转)
最好的解释:https://www.quora.com/What-is-an-intuitive-explanation-of-the-KKT-conditions# 作者:卢健龙链接:https:/ ...
- azkaban编译安装配置文档
azkaban编译安装配置文档 参考官方文档: http://azkaban.github.io/azkaban/docs/latest/ azkaban的配置文件说明:http://azkaban. ...
- 利用python实现冒泡排序
1.先生存一个随机数组成的list 2.然后进行排序,把大的元素放在后面,小的元素放在前面,最终实现从小到大排列 首先生存一个随机数组成的list import random # print(sys. ...
- stuff函数(转)
在上篇博文中提到了stuff函数 在这篇博文中对stuff函数进行了详解 本片博文系转载,但对原文顺序做了下调整 示例 以下示例在第一个字符串 abcdef 中删除从第 2 个位置(字符 b)开始的三 ...
- 133克隆图 · Clone Graph
[抄题]: 克隆一张无向图,图中的每个节点包含一个 label 和一个列表 neighbors. [思维问题]: [一句话思路]: 先BFS克隆点(一个点+扩展所有邻居),再克隆邻居(一个点+扩展所有 ...
- [leetcode]283. Move Zeroes移零
Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...
- [leetcode]113. Path Sum II路径和(返回路径)
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...