之前我们博客所讲的集合到这里就告一段落,我们来做一些练习结束集合。
按要求实现:
新闻一:新冠确诊病例超千万,数百万印度教信徒赴恒河"圣浴"引民众担忧
新闻二:男子突然想起2个月前钓的鱼还在网兜李,捞起一看赶紧放生
public class Homework01 {
public static void main(String[] args) {
ArrayList arrayList = new ArrayList();
arrayList.add(new News("新冠确诊病例超千万,数百万印度教信徒赴恒河\"圣浴\"引民众担忧"));
arrayList.add(new News("男子突然想起2个月前钓的鱼还在网兜李,捞起一看赶紧放生"));
int size = arrayList.size();
for (int i = size - 1;i>=0;i--){
//System.out.println(arrayList.get(i));
News news = (News)arrayList.get(i);
System.out.println(processTitle(news.getTitle()));
}
}
//专门写一个方法,处理实现新闻标题
public static String processTitle(String title){
if(title == null){
return "";
}
if(title.length()>15){
return title.substring(0,15)+"...";
}else{
return title;
}
}
}
class News{
private String title;
private String content;
public News(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return "News{" +
"title='" + title + '\'' +
'}';
}
}
使用ArraryList 完成对 对象Car{name,price}的各种操作
使用增强for和迭代器来遍历所有的car,需要重写Car的toString方法
public class Homework02 {
public static void main(String[] args) {
ArrayList arrayList = new ArrayList();
Car car = new Car("宝马",400000);
Car car1 = new Car("宾利",5000000);
arrayList.add(car);
arrayList.add(car1);
System.out.println(arrayList);
arrayList.remove(car);
System.out.println(arrayList);
System.out.println(arrayList.contains(car1));
System.out.println(arrayList.size());
System.out.println(arrayList.isEmpty());
//arrayList.clear();
arrayList.addAll(arrayList);
System.out.println(arrayList);
System.out.println(arrayList.containsAll(arrayList));
//arrayList.removeAll(arrayList);
System.out.println("=====增强for======");
for (Object o : arrayList) {
System.out.println(o);
}
System.out.println("======迭代器=====");
Iterator iterator = arrayList.iterator();
while (iterator.hasNext()) {
Object next = iterator.next();
System.out.println(next);
}
}
}
class Car{
private String name;
private double price;
public Car(String name, double price) {
this.name = name;
this.price = price;
}
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;
}
@Override
public String toString() {
return "Car{" +
"name='" + name + '\'' +
", price=" + price +
'}';
}
}
按要求完成下列任务:
public class Homework03 {
public static void main(String[] args) {
Map m = new HashMap();
m.put("jack",650);
m.put("tom",1200);
m.put("smith",2900);
System.out.println(m);
m.put("jack",2600);//替换,更新
System.out.println(m);
//为所有员工工资加薪100元;
Set keySet = m.keySet();
for (Object key : keySet) {
//更新
m.put(key,(Integer)m.get(key)+100);
}
System.out.println(m);
System.out.println("=====遍历=======");
//遍历 Entry
Set entrySet = m.entrySet();
//迭代器
Iterator iterator = entrySet.iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
System.out.println(entry.getKey()+"-"+entry.getValue());
}
System.out.println("======遍历所有的工资=====");
Collection values = m.values();
for (Object value : values) {
System.out.println("工资="+value);
}
}
}
试分析HashSet和TreeSet分别如何实现去重的?
下面代码运行会不会抛出异常,并从源码层面说明原因。[考察 读源码+接口编程+动态绑定]
TreeSet treeSet = new TreeSet();
treeSet.add(new Person());
class Person{}
分析源码:
add 方法,因为 TreeSet() 构造器没有传入Comparator接口的匿名内部类
所以在底层Comparable<? super K> k = (Comparable<? super K>) key;(向上转型)
即把 Person转成 Comparable类型,没有实现Comparable所以会抛出异常
public class Homework05 {
public static void main(String[] args) {
/*
分析源码:
add 方法,因为 TreeSet() 构造器没有传入Comparator接口的匿名内部类
所以在底层Comparable super K> k = (Comparable super K>) key;(向上转型)
即把 Person转成 Comparable类型,没有实现Comparable所以会抛出异常
*/
TreeSet treeSet = new TreeSet();
treeSet.add(new Person());
System.out.println(treeSet);
//解决方法:Person类实现Comparable接口,重写compareTo方法
}
}
class Person implements Comparable{
@Override
public int compareTo(Object o) {
return 0;
}
}
已知:Person类按照id和name重写了hashCode和equals方法,问下面代码输出什么?
HashSet set = new HashSet();
Person1 p1 = new Person1(1001,"AA");
Person1 p2 = new Person1(1002,"BB");
set.add(p1);
set.add(p2);
p1.name = "CC";
set.remove(p1);
System.out.println(set);//2
set.add(new Person1(1001,"CC"));
System.out.println(set);//3
set.add(new Person1(1001,"AA"));
System.out.println(set);//4
[Person1{id=1002, name='BB'}, Person1{id=1001, name='CC'}]
[Person1{id=1002, name='BB'}, Person1{id=1001, name='CC'}, Person1{id=1001, name='CC'}]
[Person1{id=1002, name='BB'}, Person1{id=1001, name='CC'}, Person1{id=1001, name='CC'}, Person1{id=1001, name='AA'}]
试写出Vector和ArrayList的比较?
底层结构 | 版本 | 线程安全(同步)效率 | 扩容倍数 | |
---|---|---|---|---|
ArrayList | 可变数组 | jdk1.2 | 不安全,效率高 | 如果有参构造1.5倍;如果是无参,第一次扩容10,第二次开始按1.5倍扩容 |
Vector | 可变数组Object[] | jdk1.0 | 安全,效率不高 | 如果有参构造2倍,如果是无参,第一次扩容10,第二次开始按2倍扩容 |