实验1 集合遍历
一、实验目的
实现字符串类型集合元素的遍历并输出
二、实验内容
编写程序,分别使用List、set集合存放学生的姓名,遍历并输出。
现有班级学生5人,姓名分别是:zhangsan、lisi、tom,jack、mick,又转来一名学生,姓名为zhangsan。将学生姓名存储到ArrayList集合、HashSet集合、TreeSet集合,采用普通for循环、增强for循环、Iterator迭代器等方法分别对集合信息遍历输出。
import java.util.*;
public class demo {
public static void main(String[] args) {
List<String>studentList = new ArrayList<>();
studentList.add("zhansan");
studentList.add("lisi");
studentList.add("tom");
studentList.add("jack");
studentList.add("mick");
studentList.add("zhangsan");
System.out.println("使用普通for循环遍历List集合:");
for(int i=0;i<studentList.size();i++) {
System.out.println(studentList.get(i));
}
System.out.println("使用增强for循环遍历List集合:");
for(String student : studentList) {
System.out.println(student);
}
System.out.println("使用Iterator迭代器遍历List集合:");
Iterator<String> it1 = studentList.iterator();
while (it1.hasNext()) {
System.out.println(it1.next());
}
Set<String>studentSet1 = new HashSet<>();
studentSet1.add("zhansan");
studentSet1.add("lisi");
studentSet1.add("tom");
studentSet1.add("jack");
studentSet1.add("mick");
studentSet1.add("zhangsan");
System.out.println("使用普通for循环遍历HashSet集合:");
for(int i=0;i<studentSet1.size();i++) {
System.out.println(studentSet1.toString());
}
System.out.println("使用增强for循环遍历HashSet集合:");
for(String student : studentSet1) {
System.out.println(student);
}
System.out.println("使用Iterator迭代器遍历HashSet集合:");
Iterator<String> it2 = studentSet1.iterator();
while (it2.hasNext()) {
System.out.println(it2.next());
}
Set<String>studentSet2 = new TreeSet<>();
studentSet2.add("zhansan");
studentSet2.add("lisi");
studentSet2.add("tom");
studentSet2.add("jack");
studentSet2.add("mick");
studentSet2.add("zhangsan");
System.out.println("使用普通for循环遍历TreeSet集合:");
for(int i=0;i<studentSet2.size();i++) {
System.out.println(studentSet2.toString());
}
System.out.println("使用增强for循环遍历TreeSet集合:");
for(String student : studentSet2) {
System.out.println(student);
}
System.out.println("使用Iterator迭代器遍历TreeSet集合:");
Iterator<String> it3 = studentSet2.iterator();
while (it3.hasNext()) {
System.out.println(it3.next());
}
}
}
实验2 图书信息查看
一、实验目的
实现对象类型集合元素的遍历并输出
二、实验内容
分别使用List、set集合和Map存放多个图书信息,遍历并输出。其中商品属性:编号,名称,单价,出版社。
1.用ArrayList实现图书信息的遍历并输出
Book
public class Book//创建图书类
{
public int id;
public String name;
public double price;
public String press;
public Book()
{
super();
}
public Book(int id, String name, double price, String press)
{
super();
this.id = id;
this.name = name;
this.price = price;
this.press = press;
}
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;
}
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;
}
@Override
public String toString()
{
return "Book [id=" + id + ", name=" + name + ", press=" + press
+ ", price=" + price + "]";
}
}
TestList
import java.util.*;
public class TestList
{
public static void main(String[] args)
{//创建集合中的元素图书对象
Book b1 = new Book(1000, "b1", 30.5, "bjsxt");
Book b1_1 = new Book(1000, "b1", 30, "bjsxt");
Book b2 = new Book(1000, "b2", 50, "bjsxt");
Book b3 = new Book(1001, "b3", 30.5, "bjsxt");
Book b4 = new Book(1002, "b4", 30.5, "bjsxt");
Book b5 = new Book(1003, "b5", 50, "bjsxt");
//使用ArrayList存储图书并遍历
List<Book> bookList = new ArrayList<Book>();
bookList.add(b1);
bookList.add(b1);
bookList.add(b2);
bookList.add(b3);
bookList.add(b4);
bookList.add(b5);
bookList.add(b1_1);
//问题1:观察结果是否有id重复的图书信息?
System.out.println("遍历输出ArrayList");
System.out.println(bookList.size());
for (Book book : bookList)
{
System.out.println(book.toString());
}
}
}
2.使用HashSet和TreeSet存储多个商品信息,遍历并输出
Book
public class Book implements Comparable<Book>
{
public int id;
public String name;
public double price;
public String press;
public Book()
{
super();
}
public Book(int id, String name, double price, String press)
{
super();
this.id = id;
this.name = name;
this.price = price;
this.press = press;
}
public int compareTo(Book o)//重写compareTo方法
{
return this.id - o.id;//图书id不一致则为不同图书,并且按id值由大到小排序
}
@Override
public int hashCode()//重写hashCode方法,确保id不同返回不同
{
final int prime = 31;
int result = 1;
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((press == null) ? 0 : press.hashCode());
long temp;
temp = Double.doubleToLongBits(price);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj)//重写equals方法,保证两个内容不同的两个对象返回false
{
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
Book other = (Book) obj;
if (id != other.id)
{
return false;
}
if (name == null)
{
if (other.name != null)
{
return false;
}
} else if (!name.equals(other.name))
{
return false;
}
if (press == null)
{
if (other.press != null)
{
return false;
}
} else if (!press.equals(other.press))
{
return false;
}
if (Double.doubleToLongBits(price) != Double
.doubleToLongBits(other.price))
{
return false;
}
return true;
}
@Override
public String toString()
{
return "Book [id=" + id + ", name=" + name + ", press=" + press
+ ", price=" + price + "]";
}
}
TestSet
import java.util.*;
public class TestSet
{
public static void main(String[] args)
{//创建图书对象
Book b1 = new Book(1000, "b1", 30.5, "bjsxt");
Book b1_1 = new Book(1000, "b1", 30, "bjsxt");
Book b2 = new Book(1000, "b2", 50, "bjsxt");
Book b3 = new Book(1001, "b3", 30.5, "bjsxt");
Book b4 = new Book(1002, "b4", 30.5, "bjsxt");
Book b5 = new Book(1003, "b5", 50, "bjsxt");
//使用HashSet存储图书并遍历
Set<Book> hashSet = new HashSet<Book>();
hashSet.add(b1);
hashSet.add(b1);//问题2:观察是否b1被添加两次
hashSet.add(b2);
hashSet.add(b3);
hashSet.add(b4);
hashSet.add(b5);
hashSet.add(b1_1);//问题3:观察id为1000的图书是否被重复添加
System.out.println("遍历输出hashset");
System.out.println(hashSet.size());
for (Book book : hashSet)
{
System.out.println(book.toString());
}
//使用TreeSet存储图书并遍历
Set<Book> treeSet = new TreeSet<Book>();
treeSet.add(b1);
treeSet.add(b1);
treeSet.add(b2);
treeSet.add(b3);
treeSet.add(b4);
treeSet.add(b5);
treeSet.add(b1_1);
System.out.println("遍历输出treeset");//问题4:观察遍历输出的结果是否排序,如果排序,按什么数据进行的?
for (Book book : treeSet)
{
System.out.println(book.toString());
}
}
}
3.使用Map类实现存储图书信息并遍历输出
将图书id作为键,图书对象作为值,实现Map集合存储数据
Book
public class Book
{
public int id;
public String name;
public double price;
public String press;
public Book()
{
super();
}
public Book(int id, String name, double price, String press)
{
super();
this.id = id;
this.name = name;
this.price = price;
this.press = press;
}
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;
}
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;
}
@Override
public String toString()
{
return "Book [id=" + id + ", name=" + name + ", press=" + press
+ ", price=" + price + "]";
}
}
TestMap
import java.util.Map;
import java.util.HashMap;
import java.util.Map.Entry;
public class TestMap
{
public static void main(String[] args)
{//创建集合中的元素图书对象
Book b1 = new Book(1000, "b1", 30.5, "bjsxt");
Book b1_1 = new Book(1000, "b1", 30, "bjsxt");
Book b2 = new Book(1000, "b2", 50, "bjsxt");
Book b3 = new Book(1001, "b3", 30.5, "bjsxt");
Book b4 = new Book(1002, "b4", 30.5, "bjsxt");
Book b5 = new Book(1003, "b5", 50, "bjsxt");
//使用Map存储图书并遍历
Map<Integer, Book> bookMap = new HashMap<Integer, Book>();
bookMap.put(b1.getId(), b1);
bookMap.put(b1.getId(), b1);
bookMap.put(b2.getId(), b2);
bookMap.put(b3.getId(), b3);
bookMap.put(b4.getId(), b4);
bookMap.put(b5.getId(), b5);
bookMap.put(b1_1.getId(), b1_1);
System.out.println("遍历输出Map");//问题5:观察图书id为1000的数据在集合中出现几次?原因是什么?
for (Entry<Integer, Book> entry : bookMap.entrySet())
{
System.out.println(entry.getKey() + "----------->" + entry.getValue());
}
//问题6:Map存储的结果是否按照id值进行排序,如果没有,怎么修改程序,要求结果按照id值排序?
}
}
实验3 集合应用
一、实验目的
掌握HashMap集合的数据存储方法
二、实验内容
假如有以下email数据“aa@sohu.com,bb@163.com,cc@sina.com,..”现需要把email中的用户部分和邮件地址部分分离,分离后以键值对应的方式放入HashMap。
import java.util.Map;
import java.util.HashMap;
import java.util.Map.Entry;
public class EmailSplit
{
public static void main(String[] args)
{
String str = "aa@sohu.com,bb@163.com,cc@sina.com";
//得到每一个email
String strs[] = str.split(",");
//存放分离后email的信息
Map<String, String> emailMap = new HashMap<String, String>();
for (String email : strs)
{
String temp[] = email.split("@");
//分割email存入map
emailMap.put(temp[0], temp[1]);
}
for (Map.Entry<String, String> entry : emailMap.entrySet()) {
System.out.println("用户部分:" + entry.getKey() + ",邮件地址部分:" + entry.getValue());
//依次遍历emailMap中的所有元素并输出
}
}
}