A:简答题
1、请说明Map接口和Collection接口的区别
Map为双链集合,collection为单链集合
2、请写出Map集合的遍历方式
//1.Set<Map.Entry<K,V>> entrySet()
返回此映射所包含的映射关系的 Set 视图。
// 然后使用 getkey()和getvalue()两个方法找对应的键和值
2. Set keySet()
// 返回此映射中所包含的键的 Set 视图。
//然后通过 V get(Object key)该方法通过key(键)找到对应的值
3、请说明HashMap和Hashtable的区别
HashMap允许存在null值null键
Hashtable不允许存在null键
4、请解释Collection与Collections的区别
Collection为单链集合
Collections为对单链集合操作的工具类
B:看程序写结果(写出自己的分析理由),程序填空,改错,看程序写结果。
class Car {
private String brand;//品牌
private int year; //制造年份
public Car () {}
public Car (String brand, int year) {
this.brand = brand;
this.year = year;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getBrand(){
return brand;
}
public void setYear(int year) {
this.year = year;
}
public int getYear(){
return year;
}
}
1、给出以下代码,请问该程序的运行结果是什么?如有问题,请说明原因。
class Test {
public static void main(String[] args) {
HashMap<Integer, String> hm = new HashMap<Integer, String>();
hm.put(new Integer(23), "Jim");
hm.put(new Integer(23), "Kevin");
Set<Integer> keys = hm.keySet();
for (Integer key : keys) {
String value = hm.get(key);
System.out.println( value );
}
}
}
运行结果为kiven
因为添加kevin时候,替代了之前的Jim,因为他们的键相同
2、给出以下代码,已定义好Car类,请问该程序的运行结果是什么?如有问题,请说明原因。
class Test {
public static void main(String[] args) {
HashMap<Car, String> hm = new HashMap<Car, String>();
hm.put(new Car("宝马x5", 2014), "Jim");
hm.put(new Car("宝马x5", 2014), "Kevin");
Set<Car> cars = hm.keySet();
for (Car car : cars) {
String value = hm.get(car);
System.out.println(value);
}
}
}
运行结果为kiven
Jim
他们的值虽然相同,但是自定义类中并没有重写hashcode()和equals()方法
3、给出以下代码,请问该程序的运行结果是什么?如有问题,请说明原因。
class Test {
public static void main(String[] args) {
TreeMap<Integer, String> tm = new TreeMap<Integer, String>();
tm.put(new Integer(11), "Tom");
tm.put(new Integer(45), "David");
tm.put(new Integer(23), "Jim");
tm.put(new Integer(23), "Kevin");
Set<Integer> keys = tm.keySet();
for (Integer key : keys) {
String value = tm.get(key);
System.out.println( key +"--"+value );
}
}
}
11–Tom 23–Kevin 45—David
Treemap集合会对键进行自然排序,由于jim和kevin的健相同所以kevin替代了jim
4、给出以下代码,已定义好Car类,请问该程序的运行结果是什么?如有问题,请说明原因。
class Test {
public static void main(String[] args) {
TreeMap<Car, String> tm = new TreeMap<Car, String>(
new Comparator<Car>() {
@Override
public int compare(Car c1, Car c2) {
int num1 = c1.getYear() - c2.getYear();
int num2 = (num1 == 0) ? (c1.getBrand().compareTo(c2
.getBrand())) : num1;
return num2;
}
});
tm.put(new Car("宝马X5", 2014), "Tom");
tm.put(new Car("宝马X5", 2014), "David");
tm.put(new Car("奥迪Q7", 2014), "Jim");
tm.put(new Car("奥迪A4L", 2014), "Kevin");
Set<Car> cars = tm.keySet();
for (Car car : cars) {
String value = tm.get(car);
System.out.println(car.getBrand() + "--" + car.getYear() + "--"
+ value);
}
}
}
奥迪A4L–2014–Kevin
奥迪Q7–2014–Jim
宝马X5–2014—David
实现了comparator接口,所以排序按照先年月,后品牌
C:编程题
1、请编写程序,统计一个字符串中每个字符出现的次数
package weekends_9;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
public class StringNumber {
public static void main(String[] args) {
HashMap<Character,Integer > hm = new HashMap<Character,Integer>();
Scanner sc = new Scanner(System.in);
System.out.println("请随意输入一串字符");
String next = sc.next();
char[] ca = next.toCharArray();
for(char s:ca) {
Integer put = hm.put(s, 1);
if(put==null)
put=1;
put++;
hm.put(s, put);
}
Set<Entry<Character,Integer>> entrySet = hm.entrySet();
for(Entry<Character,Integer> s1:entrySet) {
System.out.println(s1.getKey()+"---"+s1.getValue());
}
}
}
运行结果
请随意输入一串字符
dasdma;lkdmasl;kdja;sl
a—5
s—4
d—5
j—2
;—4
k—3
l—4
m—3
2、请编写程序,存储自定义对象到HashMap集合中,并采用两种方式遍历
package weekends_9;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
public class HashMapDemo {
public static void main(String[] args) {
HashMap<Student,Integer> hm = new HashMap<Student,Integer>();
hm.put(new Student(20,"林允儿"), 1);
hm.put(new Student(21,"林允儿"), 2);
hm.put(new Student(21,"林允儿"), 3);
hm.put(new Student(21,"林更新"), 4);
Set<Entry<Student, Integer>> es = hm.entrySet();
for(Entry<Student, Integer> s:es) {
System.out.println(s.getKey()+"==="+s.getValue());
}
Set<Student> keySet = hm.keySet();
for(Student s1:keySet) {
System.out.println(s1+"-----"+hm.get(s1));
}
}
}
运行结果
Student [age=20, name=林允儿]===1
Student [age=21, name=林允儿]===3
Student [age=21, name=林更新]===4
Student [age=20, name=林允儿]—–1
Student [age=21, name=林允儿]—–3
Student [age=21, name=林更新]—–4
3、请编写程序,存储自定义对象到TreeMap集合中,并采用两种方式遍历
package weekends_9;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeMap;
import java.util.Map.Entry;
public class TreeMapDemo {
public static void main(String[] args) {
TreeMap<Student, Integer> tm = new TreeMap<>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
int num=s1.getAge()-s2.getAge();
int num1=num==0?s1.getName().compareTo(s2.getName()):num;
return num1;
}
});
tm.put(new Student(20,"林允儿"), 1);
tm.put(new Student(21,"林允儿"), 2);
tm.put(new Student(21,"林允儿"), 3);
tm.put(new Student(21,"林更新"), 4);
Set<Entry<Student, Integer>> es = tm.entrySet();
for(Entry<Student, Integer> s:es) {
System.out.println(s.getKey()+"==="+s.getValue());
}
System.out.println("------------------------------");
Set<Student> keySet = tm.keySet();
for(Student s1:keySet) {
System.out.println(s1+"=="+tm.get(s1));
}
}
}
运行结果
Student [age=20, name=林允儿]===1
Student [age=21, name=林允儿]===3
Student [age=21, name=林更新]===4
Student [age=20, name=林允儿]==1
Student [age=21, name=林允儿]==3
Student [age=21, name=林更新]==4
4、请编写程序,完成集合嵌套,并遍历
jc 基础班
张三 20
李四 22
jy 就业班
王五 21
赵六 23
package weekends_9;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
public class ArrayListInHashMap {
public static void main(String[] args) {
HashMap<String,HashMap<String, Integer>> bighm = new HashMap<String,HashMap<String, Integer>>();
HashMap<String, Integer> hm = new HashMap<String,Integer>();
HashMap<String, Integer> hm1 = new HashMap<String,Integer>();
hm.put("张三", 20);
hm.put("李四", 22);
bighm.put("基础班",hm);
hm1.put("王五", 23);
hm1.put("赵六", 24);
bighm.put("就业班", hm1);
Set<Entry<String, HashMap<String, Integer>>> bigen = bighm.entrySet();
for(Entry<String, HashMap<String, Integer>> s:bigen) {
System.out.println(s.getKey());
Set<Entry<String,Integer>> entrySet = s.getValue().entrySet();
for(Entry<String,Integer> s1:entrySet) {
System.out.println("\t"+s1.getKey()+"=="+s1.getValue());
}
}
}
}
运行结果
就业班
王五==23
赵六==24
基础班
李四==22
张三==20
5、请编写程序,完成模拟斗地主案例
package weekends_9;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Set;
import java.util.TreeMap;
public class Game {
public static void main(String[] args) {
ArrayList<String> al = new ArrayList<String>();
ArrayList<Integer> al2 = new ArrayList<Integer>();
ArrayList<Integer> play1 = new ArrayList<Integer>();
ArrayList<Integer> play2 = new ArrayList<Integer>();
ArrayList<Integer> play3 = new ArrayList<Integer>();
ArrayList<Integer> end = new ArrayList<Integer>();
TreeMap<Integer, String> tm = new TreeMap<Integer, String>();
String[] number = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
String[] colour = { "♠", "♥", "♣", "◆" };
//生成牌
al.add("小王");
al.add("大王");
for (String s : number) {
for (String s1 : colour) {
al.add(s1+s);
}}
//对每张牌进行编号
for(int i=0;i<al.size();i++)
{tm.put(i+1,al.get(i) );
al2.add(i+1);}
//洗牌
Collections.shuffle(al2);
Collections.shuffle(al2);
Collections.shuffle(al2);
//发牌
for(int i=0;i<al2.size();i++) {
if (i> 50) {
end.add(al2.get(i));
continue;
}
if (i % 3 == 0) {
play1.add(al2.get(i));
continue;
}
if (i% 3 == 1) {
play2.add(al2.get(i));
continue;
}
play3.add(al2.get(i));
}
Collections.sort(play1);
Collections.sort(play2);
Collections.sort(play3);
look(play1, tm,"玩家一");//调用看牌方法
look(play2, tm,"玩家二");
look(play3, tm,"玩家三");
look(end, tm,"底牌");
}
//看牌方法
private static void look(ArrayList<Integer> play, TreeMap<Integer, String> tm,String s) {
Set<Integer> keySet = tm.keySet();
StringBuffer sb = new StringBuffer();
sb.append(s+":");
sb.append("[");
for(int i=0;i<play.size();i++) {
if(i==play.size()-1)
{
sb.append(tm.get(play.get(i)));
continue;
}
sb.append(tm.get(play.get(i))+",");
}
sb.append("]");
System.out.println(sb.toString());
}
}
运行结果
玩家一:[小王,♣1,♥2,◆2,♥3,◆6,♠8,♣8,♠9,♥9,♠10,♥10,◆10,◆J,♥Q,◆Q,♥K]
玩家二:[♠1,♠4,◆4,♥5,◆5,♠6,♥6,♠7,♣7,◆7,♣9,◆9,♠J,♠Q,♣Q,♠K,◆K]
玩家三:[大王,◆1,♠2,♣3,◆3,♥4,♣4,♠5,♣5,♣6,♥7,♥8,◆8,♣10,♥J,♣J,♣K]
底牌:[♣2,♥1,♠3]