集合

Java集合框架



list接口和ArrayList类




示例:

public class FirstLevelTitle {
private int id;                     //ID
private String titleName;  //名称
private String creater;      //创建者
private Date createTime;  //创建时间

public FirstLevelTitle(int id, String titleName, String creater,Date createTime) {
this.id = id;
this.titleName = titleName;
this.creater = creater;
this.createTime = createTime;
}
public String getTitleName() {
	return titleName;
}
public void setTitleName(String titleName) {
	this.titleName = titleName;
}
…..
}

示例:

public class FirstLevelTitleDB1 {
public static void main(String[] args) {
FirstLevelTitle car = new FirstLevelTitle(1, "汽车", "管理员", new Date());
FirstLevelTitle test = new FirstLevelTitle(2, "高考", "管理员", new Date());
List newsTitleList = new ArrayList();
newsTitleList.add(car);
newsTitleList.add(test);	
System.out.println("新闻标题数目为:" + newsTitleList.size() + "条");
print(newsTitleList);
}
public static void print(List newsList) {
for (int i = 0; i < newsList.size(); i++) {
FirstLevelTitle title = (FirstLevelTitle) newsList.get(i);
System.out.println(i + 1 + ":" + title.getTitleName());
}
}
}


示例:

public class FirstLevelTitleDB2 {
  public static void main(String[] args) {
FirstLevelTitle car = new FirstLevelTitle(1, "汽车", "网站管理员", new Date());
FirstLevelTitle test = new FirstLevelTitle(2, "高考", "网站管理员", new Date());
FirstLevelTitle house = new FirstLevelTitle(3, "房产", "网站管理员", new Date());
List newsTitleList = new ArrayList();
newsTitleList.add(car);
newsTitleList.add(test);
newsTitleList.add(2,house);
if(newsTitleList.contains(test)){
	System.out.println("有高考的新闻");
}else{
	System.out.println("没有高考的新闻");
}
newsTitleList.remove(1);
System.out.println("新闻标题数目为:" + newsTitleList.size() + "条");
  }
}
List接口和LinkedList类





示例:

public class FirstLevelTitleDB3 {
  public static void main(String[] args) {
FirstLevelTitle car = new FirstLevelTitle(1, "汽车", "管理员", new Date());
FirstLevelTitle medical = new FirstLevelTitle(2, "医学", "管理员",new Date());

LinkedList newsTitleList = new LinkedList();
newsTitleList.addFirst(car);
newsTitleList.addLast(medical);

FirstLevelTitle first = (FirstLevelTitle) newsTitleList.getFirst();
System.out.println("头条的新闻标题为:" + first.getTitleName());
FirstLevelTitle last = (FirstLevelTitle) newsTitleList.getLast();
System.out.println("排在最后的新闻标题为:" + last.getTitleName());

newsTitleList.removeFirst();
newsTitleList.removeLast();
  }
}

Interator接口

Iterator接口隐藏底层集合的数据结构,提供遍历各种类型的集合的统一接口


Set(集)

Set是最简单一种集合,集合的对象不按特定的方式排列,并且没有重复对象

Set接口主要有两个实现类:

                                                HashSet

                                                TreeSet

HashSet类按照哈希算法来存取集合中的对象,具有很好的存取和查找性能

//往集合中加入了两个元素
Set set = new HashSet();
String s1= new String("Hello");
String s2= new String("World");
set.add(s1);
set.add(s2);
System.out.println(set.size());
//往集合中加入了一个元素
Set set = new HashSet();
String s1= new String(“Hello”);
String s2= new String(“Hello”);
set.add(s1);
set.add(s2);
System.out.println(set.size());

遍历集合中的元素

    Iterator it = set.iterator();     	//取得Iterator对象
    while (it.hasNext()) {              //遍历集合中所有元素
        Object element = it.next(); //取出集合中一个元素
        System.out.println(element);
    }
}
TreeSet

TreeSet类实现了SortedSet接口,能够对集合中的对象进行排序

Set set = new TreeSet();
set.add(new Integer(8));
set.add(new Integer(7));
set.add(new Integer(6));
set.add(new Integer(9));

Iterator it = set.iterator();
while (it.hasNext()) {
    System.out.print(it.next() + " ");
}

TreeSet中添加自定义类的对象,则必须实现Comparable接口

class Student implements Comparable {
    private int id;   //学号
    private String name;  姓名
    ……
   //实现接口方法,指定按学号排序
    public int compareTo(Object o) {  
        Student s = (Student)o;
        if (id < s.getId()) return -1;
        if (id > s.getId()) return 1;
        return 0;
    }
}
Set set = new TreeSet ();
set.add(new Student(3,"Tom"));
set.add(new Student(1,"Eddie"));
set.add(new Student(4,"Jane"));
set.add(new Student(2,"Mike"));
Iterator it = set.iterator();
while (it.hasNext()) {
  Student s =(Student) it.next();
  System.out.println(s.getId()+ "  " +s.getName());
}

列表排序

Collections类是Java集合类库中的辅助类,它提供了操纵集合的各种静态方法,其中sort( )方法用于对List中的对象进行排序

List list = new ArrayList();
list.add(new Integer(3));
list.add(new Integer(4));
list.add(new Integer(2));

Collections.sort(list);
for (int i=0; i<list.size(); i++){
    System.out.print( list.get(i) + " " );
}

该sort方法要求集合中的元素实现了Comparable接口

Map接口和HashMap类



public class Student {
private String name;    // 学员姓名
private String school;  // 中心名称

public Student(String name, String school) {
this.name = name;
this.school = school;
}
public String toString() {
	return school+"毕业的"+name;
}
}

示例:

public class MapTest {
      public static void main(String[] args) {
    Student student1 = new Student("李明", "北京中心");
    Student student2 = new Student("刘丽", "天津中心");
    Map students = new HashMap();
    students.put("Jack", student1);
    students.put("Rose", student2);
    System.out.println("键集:"+students.keySet());
    System.out.println("值集:"+students.values());
    System.out.println("键-值对集合:"+students);

    String key = "Jack";
    if(students.containsKey(key))
  System.out.println(students.get(key));
    students.remove(key);
    System.out.println("键-值对集合:"+students);
      }
}

总结

Collection:集合层次中的根接口,JDK没有提供这个接口直接的实现类;

Set:不能包含重复的元素,实现类有HashSet。SortedSet是一个按照升序排列元素的Set,实现类有TreeSet;

List:是一个有序的集合,可以包含重复的元素。提供按索引访问的方式。实现类有ArrayList和LinkedList;

Map:包含了key-value对。key不能重复,value可重复,实现类有HashMap。SortedMap是一个按照升序排列key的Map,实现类有TreeMap;

集合对象的主要操作有:增加元素,删除元素,对元素排序,访问(遍历)元素等。



1. Set(集合)里面的元素是无序的,但没有重复的元素 2. 两个实现类HashSet(LinkHashSet)和TreeSet,TreeSet有排序功能(Set set=new TreeSet();set.add(new Integer(8)); set.add(new Integer(4)); set.add(new Integer(7));)输出后的结果是:4 7 8 Eg: package test; import java.util.*; public class Set{ public static void main(String[] args) { //先实例化一个set Set<String> stringSet=new HashSet<String>(); //向set里面添加元素 stringSet.add("123"); stringSet.add("wer"); stringSet.add("345"); //将set里的元素取出 Iterator<String> stringIter=stringSet.iterator(); while(stringIter.hasNext()){ String str=stringIter.next(); System.out.println(str); System.out.println("~~~~~~~~~~~"); } System.out.println("stringSet里面有"+stringSet.size()+"元素"); } } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~2.List(列表<接口>)以线性方式存储,有序,允许重复主要实现类有LinkList(采用链表数据结构)和ArrayList(代表可大可小的数组) Eg: package test; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; public class Map { public static void main(String[] args) { List list=new ArrayList(); list.add(10); list.add(2); list.add(34); //对list数组进行自然排序 Collections.sort(list); //依次检索输出list的所有对象 // for(int i=0;i<list.size();i++){ // System.out.println(list.get(i)); // } Iterator Iter=list.iterator(); while(Iter.hasNext()){ System.out.println(Iter.next()); } } } 3.Map(映射<集合>)是无序的,是一种把键对象和值对象进行映射的集合,它每一个元素都包含一对键对象和值对象,给出键对象就可以得到值对象,键对象不允许重复,对值没有要求,多个任意键对象可以映射到一个值对象上;如果有相同键对象,最后一次加入的键对象和值对象将会覆盖以前的; Eg: package test; import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.TreeMap; public class NewMap { public static void main(String[] args) { //向map里添加键值对 //如果要对键进行排序Map map=new TreeMap(); Map<Integer,String> map=new TreeMap<Integer,String>(); //Map map=new HashMap(); map.put(1, "yi"); map.put(23, "er"); map.put(12, "san"); map.put(3, "si"); //遍历map Set keys=map.keySet(); Iterator<Integer> stringIter=keys.iterator(); while(stringIter.hasNext()){ int key=stringIter.next(); String value=(String) map.get(key);//根据键key得到value的值 System.out.println(key+"---"+value); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值