day17作业
1.java.util 2.队列先进先出,栈堆先进后出 3.链表 4.LinkedList 5.TreeSet 6.Comparable 7.Map 8.next()
1.AC 2.A 3.D 4.B 5.D 6.C 7.C 8.C 9.CD
1.× 2.√ 3.× 4.× 5.√ 6.√ 7.√ 8.× 9.√ 10.× 11.× 12.√
2.List是有序的集合,使用此接口能够精确控制每个元素插入的位置,用户能够用索引来访问List中的元素,这类似于java的数组。
Set是一种不包含重复的元素的集合,即任意的两个元素都有equals()方法,Set最多有一个null元素。
Map接口,将键映射到值的对象。一个映射不能包含重复的键;每个键最多只能映射到一个值。
3.区别:Vector是线程安全的,效率低。ArrayList是线程不安全的,效率高。
联系:底层都是数组实现的。
4.Hashtable是JDK1.0版本出现的,是线程安全的,效率低;
HashMap是JDK1.2版本出现的,是线程不安全的,效率高;
Hashtable不可以存储null键和null值;
HashMap可以存储null键和null值(目的是为了让后续代码可以继续执行)
package com.zuikc.bean;
public class Book {
private int id;
private String name;
private double price;
private String press;
public Book() {
super();
// TODO Auto-generated constructor stub
}
public Book(int id, String name, double price, String press) {
super();
this.id = id;
this.name = name;
this.price = price;
this.press = press;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getPress() {
return press;
}
public void setPress(String press) {
this.press = press;
}
@Override
public String toString() {
return "Book [id=" + id + ", name=" + name + ", price=" + price + ", press=" + press + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((press == null) ? 0 : press.hashCode());
long temp;
temp = Double.doubleToLongBits(price);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Book other = (Book) obj;
if (id != other.id) {
return false;
}
if (name == null) {
if (other.name != null) {
return false;
}
} else if (!name.equals(other.name)) {
return false;
}
if (press == null) {
if (other.press != null) {
return false;
}
} else if (!press.equals(other.press)) {
return false;
}
if (Double.doubleToLongBits(price) != Double.doubleToLongBits(other.price)) {
return false;
}
return true;
}
}
package com.zuikc.kehoutest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import com.zuikc.bean.Book;
public class Test6 {
public static void main(String[] args) {
// demo1();
Map<Integer, Book> m = new HashMap<>();
m.put(100, new Book(100,"b1",20.5,"aaa"));
m.put(101, new Book(101,"b2",21.5,"bbb"));
m.put(102, new Book(102,"b3",22.5,"ccc"));
m.put(103, new Book(103,"b4",23.5,"ddd"));
for(Entry<Integer, Book> entry : m.entrySet()) {
System.out.println(entry.getKey() + "..." + entry.getValue());
}
}
private static void demo1() {
List<Book> list = new ArrayList<>();
list.add(new Book(100,"b1",20.5,"aaa"));
list.add(new Book(101,"b2",21.5,"bbb"));
list.add(new Book(102,"b3",22.5,"ccc"));
list.add(new Book(103,"b4",23.5,"ddd"));
for (Book book : list) {
System.out.println(book.toString());
}
}
}
package com.zuikc.bean;
public class Book {
private int id;
private String name;
private double price;
private String press;
public Book() {
super();
// TODO Auto-generated constructor stub
}
public Book(int id, String name, double price, String press) {
super();
this.id = id;
this.name = name;
this.price = price;
this.press = press;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getPress() {
return press;
}
public void setPress(String press) {
this.press = press;
}
@Override
public String toString() {
return "Book [id=" + id + ", name=" + name + ", price=" + price + ", press=" + press + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((press == null) ? 0 : press.hashCode());
long temp;
temp = Double.doubleToLongBits(price);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Book other = (Book) obj;
if (id != other.id) {
return false;
}
if (name == null) {
if (other.name != null) {
return false;
}
} else if (!name.equals(other.name)) {
return false;
}
if (press == null) {
if (other.press != null) {
return false;
}
} else if (!press.equals(other.press)) {
return false;
}
if (Double.doubleToLongBits(price) != Double.doubleToLongBits(other.price)) {
return false;
}
return true;
}
}
package com.zuikc.kehoutest;
import java.util.HashMap;
import java.util.HashSet;
import java.util.TreeSet;
import com.zuikc.bean.Book;
public class Test7 {
public static void main(String[] args) {
// demo1();
TreeSet<Book> ts = new TreeSet<>();
ts.add(new Book(100,"b1",20.5,"aaa"));
ts.add(new Book(100,"b1",20.5,"aaa"));
ts.add(new Book(101,"b2",21.5,"bbb"));
ts.add(new Book(101,"b2",21.5,"bbb"));
ts.add(new Book(102,"b3",22.5,"ccc"));
ts.add(new Book(103,"b4",23.5,"ddd"));
for(Book book : ts) {
System.out.println(book);
}
}
private static void demo1() {
HashSet<Book> hs = new HashSet<>();
hs.add(new Book(100,"b1",20.5,"aaa"));
hs.add(new Book(101,"b2",21.5,"bbb"));
hs.add(new Book(102,"b3",22.5,"ccc"));
hs.add(new Book(103,"b4",23.5,"ddd"));
for(Book book : hs) {
System.out.println(book);
}
}
}
package com.zuikc.bean;
public class StudentEntry {
private int key;
private Student2 s;
public StudentEntry() {
super();
// TODO Auto-generated constructor stub
}
public StudentEntry(int key, Student2 s) {
super();
this.key = key;
this.s = s;
}
public int getKey() {
return key;
}
public void setKey(int key) {
this.key = key;
}
public Student2 getS() {
return s;
}
public void setS(Student2 s) {
this.s = s;
}
}
package com.zuikc.kehoutest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import com.zuikc.bean.Student2;
import com.zuikc.bean.StudentEntry;
public class Test8 {
public static void main(String[] args) {
// listToMap();
Map<Integer, Student2> m = new HashMap<>();
m.put(100, new Student2(100, "张三", 23, "男"));
m.put(101, new Student2(101, "李四", 24, "男"));
m.put(102, new Student2(100, "王五", 25, "男"));
m.put(103, new Student2(100, "赵六", 26, "男"));
List<StudentEntry> list = new ArrayList<>();
for(Entry<Integer, Student2> entry : m.entrySet()) {
StudentEntry se = new StudentEntry();
se.setKey(entry.getKey());
se.setS(entry.getValue());
list.add(se);
}
for(StudentEntry se : list) {
System.out.println(se.getKey() + "..." + se.getS());
}
}
private static void listToMap() {
List<Student2> list = new ArrayList<>();
list.add(new Student2(100, "张三", 23, "男"));
list.add(new Student2(101, "李四", 24, "男"));
list.add(new Student2(100, "王五", 25, "男"));
list.add(new Student2(100, "赵六", 26, "男"));
for (Student2 s : list) {
System.out.println(s);
}
Map<Integer, Student2> m = new HashMap<>();
Iterator<Student2> it = list.iterator();
while (it.hasNext()) {
Student2 s1 = it.next();
m.put(s1.getId(), s1);
}
Set<Entry<Integer, Student2>> entrySet = m.entrySet();
for (Entry<Integer, Student2> entry : entrySet) {
System.out.println(entry.getKey() + "..." + entry.getValue());
}
}
}
package com.zuikc.kehoutest; import java.util.HashMap;
import java.util.Map; public class Test9 { public static void main(String[] args) {
String str = "aa@sohu.com,bb@163.com,cc@sina.com";
String strs[] = str.split(",");
Map<String, String> emailMap = new HashMap<String, String>();
for (String email : strs) {
String temp[] = email.split("@");
emailMap.put(temp[0], temp[1]);
}
System.out.println(emailMap.toString());
} }
package com.zuikc.bean;
public class Student3 implements Comparable<Student3>{
private int id;
private String name;
private int age;
public Student3() {
super();
// TODO Auto-generated constructor stub
}
public Student3(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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 "Student3 [id=" + id + ", name=" + name + ", age=" + age + "]";
}
@Override
public int compareTo(Student3 s) {
return this.age - s.age;
}
}
package com.zuikc.kehoutest;
import java.util.Scanner;
import java.util.Set;
import javax.swing.text.html.HTMLDocument.Iterator;
import com.zuikc.bean.Student3;
public class Test10 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Set<Student3> stuSet = saveStudentInfo();
Iterator<Student3> it = stuSet.iterator();
while (it.hasNext()) {
String info = it.next().toString();
System.out.println(info);
}
}
private static void saveStudentInfo() {
while (true) {
System.out.println("请输入学生信息(编号#姓名#年龄)");
String line = sc.nextLine();
if ("exit".equals(line)) {
break;
}
String[] info = line.split("#");
Student3 stu = new Student3(Integer.parseInt(info[0]), info[1], Integer.parseInt(info[2]));
stuSet.add(stu);
}
return stuSet;
}
}
day17作业的更多相关文章
- 老男孩Day17作业:后台管理平台编辑表格
一.作业需求: 后台管理平台 ,编辑表格: 1. 非编辑模式: 可对每行进行选择: 反选: 取消选择 2. 编辑模式: 进入编辑模式时如果行被选中,则被选中的行万变为可编辑状态,未选中的不改变 退出编 ...
- python基础一 day17 作业
# 3.用map来处理字符串列表,把列表中所有人都变成sb,比方alex_sbname=['alex','wupeiqi','yuanhao','nezha']# def func(item):# r ...
- Day17作业及默写
正则表达式练习 1.匹配一篇英文文章的标题 类似 The Voice Of China ([A-Z][a-z]*)( [A-Z][a-z]*)* 2.匹配一个网址 https://www.baidu. ...
- day17 作业
目录 一.编写函数(函数执行的时间用time.sleep(n)模拟) 二.编写装饰器,为函数加上统计时间的功能 三.编写装饰器,为函数加上认证的功能 四.编写装饰器,为多个函数加上认证的功能(用户的账 ...
- 【转】django 正则URL 匹配
django 正则URL 匹配 转自:https://www.cnblogs.com/chenkeven/articles/9305260.html 一.引子 在day17 作业中,我们查看主机详细 ...
- python27期day17:re、logging日志模块、作业。
1.re: 正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法.或者说:正则就是用来描述一类事物的规则.(在Python中)它内嵌在Python中,并通过 re 模 ...
- python 作业
Linux day01 计算机硬件知识整理 作业要求:整理博客,内容如下 编程语言的作用及与操作系统和硬件的关系 应用程序->操作系统->硬件 cpu->内存->磁盘 cpu与 ...
- python10作业思路及源码:类Fabric主机管理程序开发(仅供参考)
类Fabric主机管理程序开发 一,作业要求 1, 运行程序列出主机组或者主机列表(已完成) 2,选择指定主机或主机组(已完成) 3,选择主机或主机组传送文件(上传/下载)(已完成) 4,充分使用多线 ...
- SQLServer2005创建定时作业任务
SQLServer定时作业任务:即数据库自动按照定时执行的作业任务,具有周期性不需要人工干预的特点 创建步骤:(使用最高权限的账户登录--sa) 一.启动SQL Server代理(SQL Server ...
随机推荐
- 【设计模式】—— 创建者模式Builder
前言:[模式总览]——————————by xingoo 模式意图 一个对象的创建十分复杂,为了区分构建过程和使用过程,因此分开.使用一个Director类进行对象的创建,Builder规定了这个创建 ...
- P3254 圆桌问题
题目链接 非常简单的一道网络流题 我们发现每个单位的人要坐到不同餐桌上,那也就是说每张餐桌上不能有同一单位的人.这样的话,我们对于每个单位向每张餐桌连一条边权为1的边,表示同一餐桌不得有相同单位的人. ...
- 奔小康赚大钱 HDU - 2255(最大权值匹配 KM板题)
奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- SPOJ DQUERY - D-query (莫队算法|主席树|离线树状数组)
DQUERY - D-query Given a sequence of n numbers a1, a2, ..., an and a number of d-queries. A d-query ...
- 【BZOJ1452】[JSOI2009]Count(树状数组)
[BZOJ1452][JSOI2009]Count(树状数组) 题面 BZOJ 洛谷 题解 数据范围这么小?不是对于每个颜色开一个什么东西记一下就好了吗. 然而我不会二维树状数组? 不存在的,凭借多年 ...
- 【洛谷P1471】方差
题目大意:维护一个有 N 个元素的序列,支持以下操作:区间加,区间询问均值,区间询问方差. 题解:可知区间均值和区间和有关,即:维护区间和就等于维护了区间均值.区间方差表达式为 \(\frac{\Si ...
- 960CSS框架,之前有用过 了解下框架基本原理
http://blog.sina.com.cn/s/blog_8173443e010160b8.html CSS框架已经出现很长时间了,关于这些框架的用处也被我们讨论了很多遍了.有人说,CSS框架不够 ...
- 五大常见的MySQL高可用方案
1. 概述 我们在考虑MySQL数据库的高可用的架构时,主要要考虑如下几方面: 1.1 如果数据库发生了宕机或者意外中断等故障,能尽快恢复数据库的可用性,尽可能的减少停机时间,保证业务不会因为数据 ...
- Flink流处理操作符
一.工程创建与准备 使用maven进行工程创建,且采用提供的flink-quickstart模版,便利很多.
- Java远程访问接口的几种方式
一.Java访问远程url接口并获取结果 1.原生JavaAPI获取 package com.util; import java.io.DataOutputStream; import java.io ...