《Java基础入门第2版》--黑马程序员 课后答案及其详解 第6章 集合

一、填空题

1、Comparator
2、hashNext()、next()
3、键、值
4、ArrayList、LinkedList,HashSet、TreeSet,HashMap、TreeMap
5、forEach(Consumer action)

二、判断题

1、错   2、对   3、对   4、错   5、对

三、选择题

1、BC      2、D      3、C      4、D     5、ABC   

四、简答题

1、为了使程序能方便的存储和操作数目不固定的一组数据,JDK提供了一套类库,这些类都位
于java.util包中,统称为集合。集合框架中常用的接口和类有,List、Set、ArrayList、HashSet、Map、HashMap、TreeMap。

2、List的特点是元素有序、可重复。List接口的主要实现类有ArrayList和LinkedList。Set的特点是元素无序、不可重复。Set接口的主要实现类有HashSet和TreeSet。Map的特点是存储的元素是键(Key)、值(Value)映射关系,元素都是成对出现的。Map接口的主要实现类有HashMap和TreeMap。

3、Collection是一个单例集合接口。它提供了对集合对象进行基本操作的通用方法。Collections是一个工具类。它包含各种有关集合操作的方法。

五、编程题

1.import java.util.*;
public class Test02 {
	public static void main(String[] args) {
		HashSet hashSet = new HashSet();
		Person p1 = new Person("Jack",25);
		Person p2 = new Person("Rose",23);
		Person p3 = new Person("Jack",27);
		hashSet.add(p1);
		hashSet.add(p2);
		hashSet.add(p3);
		for(Object obj:hashSet){
			Person p=(Person)obj;
			System.out.println(p.name+":"+p.age);
		}
	}
}
class Person{
	String name;
	int age;
	public Person(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	public int hashCode() {
	   return name.hashCode();
	}
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
return false;
		Person p = (Person) obj;
		return p.name.equals(this.name);
	}
}	
2.import java.util.*;
public class Test03 {
	public static void main(String[] args) {
		TreeMap map = new TreeMap(new MyComparator());
		map.put("1", "Lucy");
		map.put("2", "Lucy");
		map.put("3", "John");
		map.put("4", "Smith");
		map.put("5", "Amanda");
		for (Object key : map.keySet()) {
			System.out.println(key + ":" + map.get(key));
		}
	}
}
class MyComparator implements Comparator {
	public int compare(Object obj1, Object obj2) {
		String ele1 = (String) obj1;
		String ele2 = (String) obj2;
		return ele2.compareTo(ele1);
	}
}

六、原题及其解析

一.填空题
1.在创建TreeSet对象时,可以传入自定义比较器,自定义比较器需实现()接口。Comparator

2.使用Interator遍历集合时,首先需要调用()方法判断是否存在下一个元素,若存在下一个元素,则调用()方法取出该元素。
hashNext() next()

3.Map集合中的元素都是成对出现的,并且都是以()、()的映射关系存在。键 值

4.List集合的主要实现类有()、(),Set集合的主要实现类有()、(),Map集合的主要实现类有()、()。
ArrayList、LinkedList,HashSet、TreeSet,HashMap、TreeMap

5.在JDK8中,根据Lambda表达式特性还增加了一个()方法来遍历集合。forEach(Consumer action)

二.判断题
1.Set集合是通过键值对的方式来存储对象的。() 错×

2.集合中不能存放基本数据类型,而只能存放引用数据类型。()对√

3.如果创建的TreeSet集合中没有传入比较器,则该集合中存入得元素需要实现Comparable接口。()对√

4.使用Iterator迭代集合元素时,可以调用集合对象的方法增删元素。()错×

5.JDK8中增加的一个Stream接口,该接口可以将集合、数组中的元素转换为Stream流的形式。()对√

三.选择题
1.要想保存具有映射关系的数据,可以使用以下哪些集合?(多选)( ) BC
A.ArrayList B.TreeMap C.HashMap D.TreeSet

2.使用Iterator时,判断是否存在下一个元素可以使用以下哪个方法?( ) D
A.next() B.hash() C.hasPrevious() D.hasNext()

3.在程序开发中,经常会使用以下哪个类来存储程序中所需的配置?( ) C
A.HashMap B.TreeSet C.Properties D.TreeMap

4.要想在集合中保存没有重复的元素并且按照一定的顺序排列,可以使用以下哪个集合?( ) D
A.LinkedList B.ArrayList C.hashSet D.TreeSet

5.以下哪些方法是LinkedList集合中定义的?(多选)( ) ABC
A.getLast( ) B.getFirst( )
C.remove(int index) D.next( )

四.简答题
1.简述什么是集合并列举集合中常用的类和接口。
为了使程序能方便的存储和操作数目不固定的一组数据,JDK提供了一套类库,这些类都位于java.util包中,统称为集合。集合框架中常用的接口和类有,List、Set、ArrayList、HashSet、Map、HashMap、TreeMap。

2.简述集合中的List、Set、Map有什么区别。
List的特点是元素有序、可重复。List接口的主要实现类有ArrayList和LinkedList。Set的特点是元素无序、不可重复。Set接口的主要实现类有HashSet和TreeSet。Map的特点是存储的元素是键(Key)、值(Value)映射关系,元素都是成对出现的。Map接口的主要实现类有HashMap和TreeMap。

3.简述Collection和Collections的区别。
Collection是一个单例集合接口。它提供了对集合对象进行基本操作的通用方法。Collections是一个工具类。它包含各种有关集合操作的方法。

五.编程题
1.在HashSet集合中添加三个Person对象,把姓名相同的人当做同一个人,禁止重复添加。要求如下:
Person类中定义name和age属性,重写hashCode( )方法和equals( )方法,针对Person类的name属性进行比较,如果name相同,hashCode( )方法的返回值相同,equals( )方法返回true。

import java.util.*;
public class Test02 {
	public static void main(String[] args) {
		HashSet hashSet = new HashSet();
		Person p1 = new Person("Jack",25);
		Person p2 = new Person("Rose",23);
		Person p3 = new Person("Jack",27);
		hashSet.add(p1);
		hashSet.add(p2);
		hashSet.add(p3);
		for(Object obj:hashSet){
			Person p=(Person)obj;
			System.out.println(p.name+":"+p.age);
		}
	}
}
class Person{
	String name;
	int age;
	public Person(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	public int hashCode() {
	   return name.hashCode();
	}
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
		return false;
		Person p = (Person) obj;
		return p.name.equals(this.name);
	}
}	

2.选择合适的Map集合保存5位学员的学号和姓名,然后按学号的自然顺序的倒序将这些键值对一 一打印出来。要求如下:
(1)创建TreeMap集合。
(2)使用put方法将学号(“1” “2” “3” “4” “5”)和姓名(“Lucy” “John” “Smith” “Aimee” “Amanda”)存储到Map中,存的时候可以打乱顺序观察排序后的效果。
(3)使用map.keySet()获取键的Set集合。
(4)使用Set集合的iterator( )方法获得Iterator对象用于迭代键。
(5)使用Map集合的get( )方法获取键所对应的值。

import java.util.*;
public class Test03 {
	public static void main(String[] args) {
		TreeMap map = new TreeMap(new MyComparator());
		map.put("1", "Lucy");
		map.put("2", "Lucy");
		map.put("3", "John");
		map.put("4", "Smith");
		map.put("5", "Amanda");
		for (Object key : map.keySet()) {
			System.out.println(key + ":" + map.get(key));
		}
	}
}
class MyComparator implements Comparator {
	public int compare(Object obj1, Object obj2) {
		String ele1 = (String) obj1;
		String ele2 = (String) obj2;
		return ele2.compareTo(ele1);
	}
}
  • 60
    点赞
  • 228
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

香鱼嫩虾

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值