1.
分析以下需求,并用代码实现:
(1)有如下代码:
List list = new ArrayList<>();
list.add(“a”);
list.add(“a”);
list.add(“a”);
list.add(“b”);
list.add(“b”);
list.add(“c”);
list.add(“d”);
list.add(“d”);
list.add(“d”);
list.add(“d”);
list.add(“d”);
System.out.println(frequency(list, “a”)); // 3
System.out.println(frequency(list, “b”)); // 2
System.out.println(frequency(list, “c”)); // 1
System.out.println(frequency(list, “d”)); // 5
System.out.println(frequency(list, “xxx”)); // 0
(2)定义方法统计集合中指定元素出现的次数,如"a" 3,“b” 2,“c” 1
@Test
public void test3(){
List<String> list = new ArrayList<>();
list.add("a");
list.add("a");
list.add("a");
list.add("b");
list.add("b");
list.add("c");
list.add("d");
list.add("d");
list.add("d");
list.add("d");
list.add("d");
System.out.println(frequency(list, "a")); // 3
System.out.println(frequency(list, "b")); // 2
System.out.println(frequency(list, "c")); // 1
System.out.println(frequency(list, "d")); // 5
System.out.println(frequency(list, "xxx")); // 0
//定义方法统计集合中指定元素出现的次数,如"a" 3,"b" 2,"c" 1
}
public int frequency(List<String> list,String n){
int count=0;
for (String s:list){
if(s.equals(n)){
count++;
}
}
return count;
}
2
分析以下需求,并用代码实现:
(1)生成10个1至100之间的随机整数(不能重复),存入一个List集合
(2)编写方法对List集合进行排序
(2)然后利用迭代器遍历集合元素并输出
(3)如:15 18 20 40 46 60 65 70 75 91
@Test
public void test4(){
// (1)生成10个1至100之间的随机整数(不能重复),存入一个List集合
// (2)编写方法对List集合进行排序
// (2)然后利用迭代器遍历集合元素并输出
// (3)如:15 18 20 40 46 60 65 70 75 91
HashSet<Integer> set = new HashSet<>();
Random random = new Random();
boolean tmp=true;
while (tmp){
int num = random.nextInt(100) + 1;
set.add(num);
if(set.size()==10){
tmp=false;
}
}
List<Integer> list = new ArrayList<>();
for (Integer i:set){
list.add(i);
}
System.out.println(list);
// (2)编写方法对List集合进行排序
sort(list);
System.out.println(list);
//(2)然后利用迭代器遍历集合元素并输出
Iterator<Integer> iterator = list.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
} }
public void sort(List list){
Collections.sort(list, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o1-o2;//升序
}
});
}
3
编写一个类Book,具有name,price,press,author 然后创建5个对象放入ArrayList中,并实现按照price大小排序,
然后遍历ArrayList输出每个Book对象, 使用toString 方法打印。
package demo2;
import java.util.Objects;
/**
* @creat 2020-09-01-19:23
*/
//编写一个类Book,具有name,price,press,author 然后创建5个对象放入ArrayList中,
// 并实现按照price大小排序,
// 然后遍历ArrayList输出每个Book对象, 使用toString 方法打印。
public class Book{
private String name;
private double price;
private String press;//出版社
private String author;
public Book() {
}
public Book(String name, double price, String press, String author) {
this.name = name;
this.price = price;
this.press = press;
this.author = author;
}
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;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Book book = (Book) o;
return Double.compare(book.price, price) == 0 &&
name.equals(book.name) &&
press.equals(book.press) &&
author.equals(book.author);
}
@Override
public int hashCode() {
return Objects.hash(name, price, press, author);
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", price=" + price +
", press='" + press + '\'' +
", author='" + author + '\'' +
'}';
}
}
@Test
public void test5(){
ArrayList<Book> list = new ArrayList<>();
list.add(new Book("狼王梦",50,"少年出版社","沈石溪"));
list.add(new Book("格林童话",39.9,"少年出版社","格林"));
list.add(new Book("三国演义",41.5,"人民出版社","罗贯中"));
list.add(new Book("伊索寓言",25,"上海出版社","伊索"));
list.add(new Book("安徒生童话",25,"青年出版社","安徒生"));
Collections.sort(list, new Comparator<Book>() {
@Override
public int compare(Book o1, Book o2) {
return Double.compare(o1.getPrice(),o2.getPrice());
}
});
for (Book book:list){
System.out.println(book);
}
}
4
把如下元素存入List集合
“aaa” “bbb” “aaa” “abc” “xyz” “123” “xyz”
去掉重复元素
@Test
public void test6(){
// 把如下元素存入List集合
// "aaa" "bbb" "aaa" "abc" "xyz" "123" "xyz"
//
// 去掉重复元素
HashSet<String> set = new HashSet<>();
set.add("aaa");
set.add("bbb");
set.add("aaa");
set.add("abc");
set.add("xyz");
set.add("123");
set.add("xyz");
ArrayList<String> list = new ArrayList<>();
for (String s:set){
list.add(s);
}
System.out.println(list);
}
5
已知 Student类 属性包括 int id,String name
创建 5个Student对象存入 List中
Student s1 = new Student(1,“aaa”);
Student s2 = new Student(2,“bbb”);
Student s3 = new Student(3,“ccc”);
Student s4 = new Student(4,“ddd”);
Student s5 = new Student(1,“aaa”);
去掉重复元素:当Student的id和name值都相同时,任务是重复元素
package demo2;
import java.util.Objects;
/**
* @creat 2020-09-01-19:43
*/
public class Student {
private int id;
private String name;
public Student() {
}
public Student(int id, String name) {
this.id = id;
this.name = name;
}
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;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return id == student.id &&
Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(id, name);
}
}
@Test
public void test7(){
Student s1 = new Student(1,"aaa");
Student s2 = new Student(2,"bbb");
Student s3 = new Student(3,"ccc");
Student s4 = new Student(4,"ddd");
Student s5 = new Student(1,"aaa");
ArrayList<Student> list = new ArrayList<>();
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
list.add(s5);
//去掉重复元素:当Student的id和name值都相同时,任务是重复元素
HashSet<Student> set = new HashSet<>();
for (Student s:list){
set.add(s);
}
for (Student s:set){
System.out.println(s);
}
}