java.util.Set<E>:
特点:
1.Set提供一个无序的集合
2.不能有重复的对象
3.无法通过索引取得特定的对象,只能通过迭代器取出对象(详见下面代码)
常见实现子类:
HashSet、TreeSet
常用方法:
boolean | add(E e) 如果 set 中尚未存在指定的元素,则添加此元素(可选操作)。 |
void | clear() 移除此 set 中的所有元素(可选操作)。 |
boolean | contains(Object o) 如果 set 包含指定的元素,则返回 true。 |
Iterator<E> | iterator() 返回在此 set 中的元素上进行迭代的迭代器。 |
int | size() 返回 set 中的元素数(其容量)。 |
代码示例:
功能:将对象添加到集合框架并对对象进行排序
Student类:
package lesson3;
//定义一个学生类
public class Student {
// 定义一个姓名属性,默认值是null
private String name;
// 定义一个学分属性,默认值是0.0
private double score;
public Student() {
}
public Student(String name, int score) {
this.name = name;
this.score = score;
}
// 定义设置姓名属性值的方法
public void setName(String strName) {
// 将strName的值赋给name
name = strName;
}
// 定义获取姓名属性值的方法
public String getName() {
return name;
}
public double getScore() {
return score;
}
// 定义一个学习的方法
public void study() {
// 学习的时候学分加1
score++;
// 输出结果
System.out.println(name + "学习中,学分是" + score);
}
public void showInfo() {
System.out.println("姓名: " + name + " 学分: " + score);
}
}
将Student类对象添加到Set集合框架并对所有Student对象按学分从高到低进行排序
package lesson3;
import java.util.Collection;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
public class SetSort {
// 存放的随机数数组
public static double[] array = new double[26];
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SetSort test = new SetSort();
Collection<Student> sSet = test.getUserSet(26);
System.out.println("集合中共有元素:" + sSet.size());
// 将Set中所有元素取出打印;
// set是无序的,所以不能根据索引取,得到set的迭代器
java.util.Iterator<Student> it = sSet.iterator();
// 遍历
while (it.hasNext()) {
Student st = it.next();
st.showInfo();
}
// 对数组进行排序
for (int i = 0; i < 26; i++) {
for (int j = i; j < 26; j++) {
if (array[j] > array[i]) {
double t = array[i];
array[i] = array[j];
array[j] = t;
}
}
}
System.out.println("==============分割线==================");
for (int i = 0; i < array.length; i++) {
// System.out.println(" " + array[i]);
java.util.Iterator<Student> iter = sSet.iterator();
while (iter.hasNext()) {
Student st = iter.next();
if (st.getScore() == array[i]) {
st.showInfo();
}
}
}
}
public Collection<Student> getUserSet(int userCount) {
Set<Student> sSet = new HashSet<Student>();
Random rand = new Random();
for (int i = 0; i < userCount; i++) {
int j = rand.nextInt(100);
Student ui = new Student("用户: " + ((char) (65 + i)), j);
array[i] = (double) j;
sSet.add(ui);
}
return sSet;
}
}
java.util.List<E>:
特点:
1.对象的存储是有序的,即线性存储
2.可以通过索引值得到指定的对象
常见实现子类:
ArrayList、Vector
常用方法:
boolean | add(E e) 向列表的尾部添加指定的元素(可选操作)。 |
E | get(int index) 返回列表中指定位置的元素。 |
int | size() 返回列表中的元素数。 |
int | indexOf(Object o) 返回此列表中第一次出现的指定元素的索引;如果此列表不包含该元素,则返回 -1。 |
代码示例:
功能:同以上Set部分
(Student类见Set部分的代码)
使用子类:Vector
package lesson3;
import java.util.Random;
import java.util.Vector;
public class VectorTest {
// 存放的随机数数组
public static double[] array = new double[26];
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
VectorTest test = new VectorTest();
Vector<Student> sSet = test.getUserSet(26);
System.out.println("集合中共有元素:" + sSet.size());
// 遍历
for (int i = 0; i < sSet.size(); i++) {
Student st = sSet.get(i);
st.showInfo();
}
// 对数组进行排序
for (int i = 0; i < 26; i++) {
for (int j = i; j < 26; j++) {
if (array[j] > array[i]) {
double t = array[i];
array[i] = array[j];
array[j] = t;
}
}
}
System.out.println("==============分割线==================");
for (int i = 0; i < array.length; i++) {
// System.out.println(" " + array[i]);
for (int j = 0; j < sSet.size(); j++) {
if (array[i] == sSet.get(j).getScore()) {
sSet.get(j).showInfo();
}
}
}
}
public Vector<Student> getUserSet(int userCount) {
Vector<Student> sSet = new Vector<Student>(26);
Random rand = new Random();
for (int i = 0; i < userCount; i++) {
int j = rand.nextInt(100);
Student ui = new Student("用户: " + ((char) (65 + i)), j);
array[i] = (double) j;
sSet.add(ui);
}
return sSet;
}
}
java.util.Map<K,V>:
特点:
1.以键值对方式存储,每一个key对应一个value
2.不可以形成一个key对多个value的形式
3.可以根据key来索引value
常见实现子类:
HashMap、Hashtable、TreeMap
常用方法:
V | put(K key, V value) 将指定的值与此映射中的指定键关联(可选操作)。 |
V | get(Object key) 返回指定键所映射的值;如果此映射不包含该键的映射关系,则返回 null 。 |
Set<K> | keySet() 返回此映射中包含的键的 Set 视图。 |
int | size() 返回此映射中的键-值映射关系数 |
代码示例:
package lesson3;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Random;
import java.util.Set;
public class MapTest {
public static void main(String[] args) {
HashMap<String, Student> m = MapTest();
// 要得到键,就得把所有的键装到一个Set框架中
Set<String> set = m.keySet();
// 得到迭代器
Iterator<String> it = set.iterator();
// 迭代器循环输出
while (it.hasNext()) {
String str = it.next();
Student stu = m.get(str);
stu.showInfo();
}
}
// map测试方法
public static HashMap<String, Student> MapTest() {
HashMap<String, Student> map = new HashMap<String, Student>();
String str = "1";
Random rand = new Random();
for (int i = 0; i < 5; i++) {
str += i + 2;
// 得到随机数
int j = rand.nextInt(100);
// 创建学生类对象
Student stu = new Student(str, j);
// 将对象加到map集合中
map.put(str, stu);
}
return map;
}
}