1.Collection接口
package jihe;
import java.util.*;
public class Muster {
public static void main(String args[]){
Collection<String>list = new ArrayList<>();
//实例化集合类对象
list.add("a");//向集合添加元素
list.add("b");
list.add("c");
Iterator<String>it = list.iterator();
//创建迭代器
while(it.hasNext()){
//判断是否有下一个元素
String str = (String)it.next();
//获取集合中的元素
System.out.println(str);
}
}
}
2.List接口继承Collection接口,允许元素重复,各元素的顺序就是对象插入的顺序。
package jihe;
import java.util.*;
public class Gather {
public static void main(String[] args){
List<String>list = new ArrayList<>();
//创建集合对象还可以为LinkedList;
list.add("a");
list.add("b");
list.add("c");
int i = (int)(Math.random()*list.size());
//获得0到2之间的随机数
System.out.println("随机获取数组中的元素:" + list.get(i));
list.set(0, "e");//修改指定索引位置的对象
for(int j = 0; j<list.size();j++){//循环遍历结合
System.out.println(list.get(j));
}
}
}
3.Set集合继承Collection接口,不允许元素重复,不按特定的方式排序 (HashSet类 TreeSet类)
package jihe;
import java.util.*;
//存入TreeSet类实现的Set集合必须实现Comparable接口,该接口中的compareTo(Object o)方法比较此对象与指定对象的顺序。
public class UpdateStu implements Comparable<Object>{
String name;
long id;
public UpdateStu(String name, long id){//构造方法
this.id = id;
this.name = name;
}
public int compareTo(Object o){
UpdateStu upstu = (UpdateStu)o;
int result = id>upstu.id?1:(id == upstu.id?0:-1);
//如果该对象小于,等于或大于指定对象,则分别返回负整数,0或正整数
return result;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public long getld(){
return id;
}
public void setId(long id){
this.id = id;
}
public static void main(String[] args){
UpdateStu stu1 = new UpdateStu("李同学", 01011);//创建UpdateStu对象
UpdateStu stu2 = new UpdateStu("陈同学", 01021);
UpdateStu stu3 = new UpdateStu("王同学", 01051);
UpdateStu stu4 = new UpdateStu("马同学", 01012);
TreeSet<UpdateStu>tree = new TreeSet<>();
tree.add(stu1);//像集合添加对象
tree.add(stu2);
tree.add(stu3);
tree.add(stu4);
Iterator<UpdateStu>it = tree.iterator();//Set集合中所有对象的迭代器
System.out.println("Set集合中的所有元素:");
while(it.hasNext()){//集合遍历
UpdateStu stu = (UpdateStu)it.next();
System.out.println(stu.getld()+" "+stu.getName());
}
it = tree.headSet(stu2).iterator();
System.out.println("截取前面部分元素:");
while(it.hasNext()){
UpdateStu stu = (UpdateStu)it.next();
System.out.println(stu.getld()+" " + stu.getName());
}
it = tree.subSet(stu2, stu3).iterator();
System.out.println("截取中间部分的集合");
while(it.hasNext()){
UpdateStu stu = (UpdateStu)it.next();
System.out.println(stu.getld()+" "+stu.getName());
}
it = tree.tailSet(stu2).iterator();
System.out.println("截取后面部分的集合");
while(it.hasNext()){
UpdateStu stu = (UpdateStu)it.next();
System.out.println(stu.getld()+" "+stu.getName());
}
}
}
Set集合中的所有元素:
521 李同学
522 马同学
529 陈同学
553 王同学
截取前面部分元素:
521 李同学
522 马同学
截取中间部分的集合
529 陈同学
截取后面部分的集合
529 陈同学
553 王同学
4.Map集合不继承Collection接口,(HashMap类和TreeMap类)
(1)创建Emp类
package jihe;
public class Emp {
private String e_id;
private String e_name;
public Emp(String e_id, String e_name){
this.e_id = e_id;
this.e_name = e_name;
}
public String getE_Name(){
return e_name;
}
public void setE_Name(String e_name){
this.e_name = e_name;
}
public String getE_ld(){
return e_id;
}
public void setE_Id(long id){
this.e_id = e_id;
}
}
(2)创建主类
package jihe;
import java.util.*;
public class MapText {
public static void main(String[] args){
Map<String,String>map = new HashMap<>();//由HashMap实现的Map对象
Emp emp = new Emp("351", "张三");//创建Emp对象
Emp emp2 = new Emp("512", "李四");
Emp emp3 = new Emp("853", "王一");
Emp emp4 = new Emp("125", "赵六");
Emp emp5 = new Emp("341", "黄七");
map.put(emp4.getE_ld(), emp4.getE_Name());//将对象添加到集合中
map.put(emp5.getE_ld(), emp5.getE_Name());
map.put(emp.getE_ld(), emp.getE_Name());
map.put(emp2.getE_ld(), emp2.getE_Name());
map.put(emp3.getE_ld(), emp3.getE_Name());
Set<String>set = map.keySet();//获取集合中的key对象集合
//values()获取该集合中所有的值对象形成的Collection集合
Iterator<String>it = set.iterator();
System.out.println("HashMap类实现的Map集合,无序:");
while(it.hasNext()){//遍历Map集合
String str = (String)it.next();
String name = (String)map.get(str);
System.out.println(str + " " + name);
}
TreeMap<String,String>treemap = new TreeMap<>();//创建TreeMap集合对象
treemap.putAll(map);//向集合添加对象
Iterator<String>iter = treemap.keySet().iterator();
System.out.println("Treemap类实现的Map集合,键对象升序:");
while(iter.hasNext()){//遍历TreeMap集合对象
String str = (String)iter.next();//获取集合中的所有Key对象
String name = (String)treemap.get(str);//获取集合中的所有values值
System.out.println(str + " " + name);
}
}
}
HashMap类实现的Map集合,无序:
341 黄七
125 赵六
512 李四
853 王一
351 张三
Treemap类实现的Map集合,键对象升序:
125 赵六
341 黄七
351 张三
512 李四
853 王一