实验目的:掌握集合的常规操作
说明:
- 集合的操作对内存的分配是动态的,内存数据,电脑关了就没有了。
- 数据库的数据是持久数据
- 了解redis(最新)
实验内容:
1、LinkedList---链表(适合于添加,删除)
2、ArrayList---数组集合(适合于查询数据)
3、HandMap---key-value形式,可以通过key快速存,取value
实验步骤:
一. LinkedList---链表(适合于添加,删除)
1. 工程图
2. 实现步骤
package com.xl.linkedlist;
import java.util.*;
public class LinkedListDemo {
public static void main(String[] args) {
// TODO 自动生成的方法存根
List<String> list = new LinkedList<String>();
for(int i = 0;i < 10;i++) {
list.add("data"+i);
}
for(int i = 0;i< list.size();i++) {
System.out.print(list.get(i)+" ");
}
list.add(8, "txt");
Collections.reverse(list);
System.out.println();
for(int i = 0;i< list.size();i++) {
System.out.print(list.get(i)+" ");
}
Collections.shuffle(list);
System.out.println();
for(int i = 0;i< list.size();i++) {
System.out.print(list.get(i)+" ");
}
System.out.println();
//快速打印(推荐)
Iterator<String> iter = list.iterator();
while(iter.hasNext()) {
System.out.print(iter.next()+" ");
}
}
}
3. 结果
data0 data1 data2 data3 data4 data5 data6 data7 data8 data9
data9 data8 txt data7 data6 data5 data4 data3 data2 data1 data0
data5 data3 data2 data4 data9 data7 data0 data1 data6 txt data8
data5 data3 data2 data4 data9 data7 data0 data1 data6 txt data8
二. ArrayList---数组集合(适合于查询数据)
1. 工程图
2. 实现步骤
//Student类
package com.xl.arraylist;
public class Student implements Comparable<Object> {
private String id;
private String name;
private int age;
public Student() {
super();
}
public Student(String id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
}
@Override
public int compareTo(Object arg0) {
// TODO 自动生成的方法存根
Student st = (Student)arg0;
if(this.age-st.age>0) return 1;
else if(this.age-st.age<0) return -1;
else return 0;
}
}
//主函数
package com.xl.arraylist;
import java.util.*;
public class ArrayListDemo {
//@SuppressWarnings("unchecked")
public static void main(String[] args) {
// TODO 自动生成的方法存根
Random rd = new Random();
List<Student> list = new ArrayList<Student>();//对象集合
for(int i = 0;i < 20;i++) {
Student stu = new Student("1000"+i,"name"+i,rd.nextInt(10)+10);
list.add(stu);
}
//传统输出
for(Student stu:list) {
System.out.print(stu+" ");
}
System.out.println();
Collections.sort(list);
Iterator<Student> iter = list.iterator();
while(iter.hasNext()) {
System.out.print(iter.next()+" ");
}
}
}
3. 结果
Student [id=10000, name=name0, age=10] Student [id=10001, name=name1, age=16] Student [id=10002, name=name2, age=17] Student [id=10003, name=name3, age=17] Student [id=10004, name=name4, age=18] Student [id=10005, name=name5, age=18] Student [id=10006, name=name6, age=11] Student [id=10007, name=name7, age=12] Student [id=10008, name=name8, age=14] Student [id=10009, name=name9, age=12] Student [id=100010, name=name10, age=12] Student [id=100011, name=name11, age=10] Student [id=100012, name=name12, age=17] Student [id=100013, name=name13, age=10] Student [id=100014, name=name14, age=12] Student [id=100015, name=name15, age=15] Student [id=100016, name=name16, age=14] Student [id=100017, name=name17, age=10] Student [id=100018, name=name18, age=12] Student [id=100019, name=name19, age=17]
Student [id=10000, name=name0, age=10] Student [id=100011, name=name11, age=10] Student [id=100013, name=name13, age=10] Student [id=100017, name=name17, age=10] Student [id=10006, name=name6, age=11] Student [id=10007, name=name7, age=12] Student [id=10009, name=name9, age=12] Student [id=100010, name=name10, age=12] Student [id=100014, name=name14, age=12] Student [id=100018, name=name18, age=12] Student [id=10008, name=name8, age=14] Student [id=100016, name=name16, age=14] Student [id=100015, name=name15, age=15] Student [id=10001, name=name1, age=16] Student [id=10002, name=name2, age=17] Student [id=10003, name=name3, age=17] Student [id=100012, name=name12, age=17] Student [id=100019, name=name19, age=17] Student [id=10004, name=name4, age=18] Student [id=10005, name=name5, age=18]
三. HandMap---key-value形式,可以通过key快速存,取value
1. 工程图
2. 实现步骤
//User类
package com.xl.hashmap;
public class User {
private int Password;
public User() {
super();
}
public User(int password) {
super();
Password = password;
}
public int getPassword() {
return Password;
}
public void setPassword(int password) {
Password = password;
}
@Override
public String toString() {
return "User [Password=" + Password + "]";
}
}
//主函数
package com.xl.hashmap;
import java.util.*;
import java.util.Map.Entry;
public class HashMapDemo {
public static void main(String[] args) {
// TODO 自动生成的方法存根
HashMap<String,User> map = new HashMap<String,User>();
User user1 = new User(1001);
User user2 = new User(1002);
map.put("str1", user1);
map.put("str2", user2);
Set<Entry<String,User>> set = map.entrySet();//获得Set集合对象
Iterator<Entry<String,User>> it = set.iterator();//获得迭代器
//遍历迭代器
while(it.hasNext()) {
System.out.println(it.next().getValue().toString());
}
}
}
3. 结果
User [Password=1001]
User [Password=1002]