更新时间:2022-11-10 10:42:22 来源:极悦 浏览1294次
1.创建一个HashMap对象,并在其中添加一些学员的姓名和他们的分数:张三,90分,李四,88分。然后从HashMap对象获取这两个人的成绩并显示出来,接着把张三的成绩改为99分,再把他们的成绩显示出来。
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
public class First {
public static void main(String[] args) {
HashMap<String, String> hp =new HashMap<String, String>();
hp.put("张三", "90分");
hp.put("李四", "88分");
Collection<String> con = hp.values();
Iterator<String> it1 = con.iterator();
while(it1.hasNext()) {
System.out.println(it1.next());
}
hp.replace("张三", "99分");
Collection<String> con1 = hp.values();
Iterator<String> it2 = con.iterator();
while(it2.hasNext()) {
System.out.println(it2.next());
}
}
}
2.定义一个ArrayList集合类,然后放入多个字符串,排序,反序(Collections 集合工具类中包含了排序等功能)
用Iterator遍历集合
import java.util.*;
public class Second {
public static void main(String[] args) {
System.out.println("请随意输入一堆字符串:(输入0则结束)");
Scanner sc = new Scanner(System.in);
ArrayList<String> list = new ArrayList<String>();
for(;;) {
String s = sc.next();
if(s.equals("0")) {
break;
}
list.add(s);
}
Collections.sort(list);
Iterator iterator = list.iterator();
System.out.println("排序后");
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
iterator = list.iterator();
System.out.println("反序后");
Collections.reverse(list);
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
sc.close();
}
}
3.定义一个Map集合,key是Integer类型,value是Cat类型,然后使用values()方法遍历每个元素,调用每个元素的叫的方法(定义Cat类,起码包含一个Cry()方法,建议结合泛型操作)
Cat类
public class Cat {
public void Cry() {
System.out.println("猫喵喵~~");
}
}
测试方法
import java.util.*;
public class Third {
public static void main(String[] args) {
Map<Integer, Cat> map = new HashMap<Integer, Cat>();
map.put(new Integer(1), new Cat());
map.put(new Integer(2), new Cat());
map.put(new Integer(3), new Cat());
map.put(new Integer(4), new Cat());
map.put(new Integer(5), new Cat());
Collection coll = map.values();
Iterator iterator = coll.iterator();
while (iterator.hasNext()) {
Cat cat = (Cat) iterator.next();
cat.Cry();
}
}
}
4.定义一个HashSet集合,只能添加Student类型的对象。往集合内添加多个元素,分别使用foreach语句,Iterator方式循环
Student类
public class Student {
String name;
int 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 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 Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
}
测试类
import java.util.HashSet;
import java.util.Iterator;
public class Fourth {
public static void main(String[] args) {
HashSet<Student> hs = new HashSet<Student>();
hs.add(new Student("张三",20));
hs.add(new Student("李四",19));
hs.add(new Student("王五",16));
hs.add(new Student("张大",18));
hs.add(new Student("李二",20));
Iterator<Student> iterator = hs.iterator();
System.out.println("Iterator输出");
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
System.out.println("*************************************");
System.out.println("for-each输出");
for(Student s : hs) {
System.out.println(s);
}
}
}
5.**随机生成不重复的26个小写字母,生成26个后就停止生成。
import java.util.*;
public class Fifth {
/**
* @Description:
* @author LYL
* @date 2021-01-10 12:48:38
*/
public static void main(String[] args) {
HashSet hs = new HashSet();
Random r = new Random();
for(;;) {
int i = (r.nextInt(122-97+1)+97);
char ch = (char)i;
if(hs.size()>=26) {
break;
}
hs.add(ch);
}
Iterator iterator = hs.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
6.**随机生成100个小写字母,同时统计每个字母生成了多少个
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
public class Sixth {
public static void main(String[] args) {
ArrayList list = new ArrayList();
for(int i=0;i<100;i++) {
int x = 97+(int)(Math.random()*26);
list.add((char)x);
}
HashMap map = new HashMap();
for(int i=0;i<list.size();i++) {
if(map.containsKey(list.get(i))) {
int times = (int)(map.get(list.get(i)))+1;
map.put(list.get(i), times);
}else {
map.put(list.get(i),1);
}
}
Iterator it = map.entrySet().iterator();
while(it.hasNext()){
Entry e = (Entry) it.next();
System.out.println("字母:"+e.getKey()+"次数:"+e.getValue());
}
}
}
0基础 0学费 15天面授
Java就业班有基础 直达就业
业余时间 高薪转行
Java在职加薪班工作1~3年,加薪神器
工作3~5年,晋升架构
提交申请后,顾问老师会电话与您沟通安排学习