java视频---集合类

来源:互联网 发布:海关大数据数据挖掘 编辑:程序博客网 时间:2024/05/18 01:55

1.ArrayList用法

动态数组,存放对象。

当我们需要从一个列表中获取一个数组:toArray()方法返回一个数组。

当我们需要从一个数组获取一个列表:Arrays.asListObject[] a)方法返回一个固定大小的List类型的索引列表,但可以用List接口中的set()方法去改变索引中每一项的值。

2.迭代器(iterator

     迭代器给我们提供了一种通用的方式来访问集合中的元素。

     方法:hasnext()next()remove()。在调用remove()之前一定调用next()方法,因为它删除的是上一个返回的元素。

     Collection集合类中有一个方法iterator()可以生成一个迭代器:

例如:ArrayList al=new ArrayList()

  Iterator it=al.iterator();

  While(it.hasNext())

  {

System.out.println(it.next());

  }

3.Collections(类似Arrays)

排序:静态方法Collections.sort(List ls),不过排序的对象元素必须实现Comparable接口。另一种方法Collections.sort(List lsComparator cp),通过比较器实现,此时必须实现比较器Comparator接口,可以在比较对象的类定义中定义内部类来实现该接口。

取最大最小元素:Collections.max()Collections.min();

搜索:Collections.binarySearch();

4.LinkedList

LinkedList是采用双向循环链表实现的。我们可以利用LinkedList来实现栈、队列、双向队列等。

各种方法,见文档。

例:用LinkedList实现栈数据结构

Import java.util.*

class MyStack

{

private LinkedList ll=new LinkedList();

public void push(Object o)

{

ll.addFirst(o);//增加元素到链表的第一个位置

}

public Object pop()

{

return ll.removeFirst();//删除链表的第一个元素,并返回第一个元素

}

public Object peek()

{

return ll.getFirst();//获取链表的第一个元素,并返回

}

public boolean empty()

{

return ll.isEmpty();//判断链表是否为空

}

public static void main(String[] args)

{

MyStack ms=new MyStack();

ms.push("one");

ms.push("two");

ms.push("three");

System.out.println(ms.pop());

System.out.println(ms.peek());

System.out.println(ms.pop());

System.out.println(ms.empty());

}

}

例:用LinkedList实现队列数据结构

import java.util.*;

class MyQueue

{

private LinkedList ll=new LinkedList();

public void put(Object o)

{

ll.addLast(o);

}

public Object get()

{

return ll.removeFirst();

}

public boolean empty()

{

return ll.isEmpty();

}

public static void main(String[] args)

{

MyQueue mq=new MyQueue();

mq.put("one");

mq.put("two");

mq.put("three");

System.out.println(mq.get());

System.out.println(mq.get());

System.out.println(mq.get());

System.out.println(mq.empty());

}

}

ArrayListLinkedList比较:如果我们经常在List的开始处增加元素,或者在List中进行插入和删除操作,我们应该使用LinkedList,否则的话(访问某个元素),使用ArrayList将更加快速。

5.HashSet

Set接口的hash table(哈希表)实现类。我们需要为要存放到散列表(哈希表)的各个对象重新定义hashCode()equals()

例子:

import java.util.*;

class HashSetTest

{

public static void main(String[] args)

{

HashSet hs=new HashSet();

hs.add(new Student(1,"zhangsan"));//往哈希表中增加元素对象

hs.add(new Student(2,"lisi"));

hs.add(new Student(3,"wangwu"));

hs.add(new Student(1,"zhangsan"));//因为是重复对象,所以哈希地址一样,HashSet不在接受,所以只打印一遍。

Iterator it=hs.iterator();

while(it.hasNext())

{

System.out.println(it.next());

}

}

}

class Student

{

int num;

String name;

Student(int num,String name)

{

this.num=num;

this.name=name;

}

public int hashCode()//重写hashCode()函数

{

return num*name.hashCode();

}

public boolean equals(Object o)//重写equals()函数

{

Student s=(Student)o;

return num==s.num && name.equals(s.name);

}

public String toString()

{

return num+":"+name;

}

}

6.TreeSet

Set接口的实现类。

TreeSet是一个有序集合TreeSet中元素将按照升序排列,缺省是按照自然顺序进行排列,意味着TreeSet中元素要实现Comparable接口。

我们可以在构造TreeSet对象时,传递实现了Comparator接口的比较器对象。

例子:

TreeSet ts=new TreeSet(new Student.StudentComparator());

HashSetTreeSet比较

HashSet是基于Hash算法实现的,其性能通常都优于TreeSet。我们通常都应该使用HashSet,在我们需要排序的功能时,我们才使用TreeSet

7.HashMap

HashMapkey进行散列。

keySet()values()entrySet()

例子:

public static void printElements(Collection c)

{

Iterator it=c.iterator();

while(it.hasNext())

{

System.out.println(it.next());

}

}

HashMap hm=new HashMap();

hm.put("one","zhangsan");//HashMap中添加数据

hm.put("two","lisi");

hm.put("three","wangwu");

System.out.println(hm.get("one"));//获取HashMap中的数据

System.out.println(hm.get("two"));

System.out.println(hm.get("three"));

Set keys=hm.keySet();//获取HashMap中的Key的视图

System.out.println("Key:");

printElements(keys);

Collection values=hm.values();//获取HashMapValue的视图

System.out.println("Value:");

printElements(values);

Set entry=hm.entrySet();//获取HashMap中的键值对的视图

//printElements(entry);

Iterator it=entry.iterator();

while(it.hasNext())

{

Map.Entry me=(Map.Entry)it.next();

System.out.println(me.getKey()+":"+me.getValue());

}

8.TreeMap按照key进行排序。

TreeMapHashMap区别

Set类似,HashMap的速度通常都比TreeMap快,只有在需要排序的功能的时候,才使用TreeMap

集合类总结:

接口:Collection接口没有直接的实现类,从Collection接口派生有Set接口与List接口,从Set接口派生有SortedSet接口。Map接口派生有SortedMap接口。

实现类:

Set接口实现了HashSet类与LinkedHashSet类,从SortedSet实现了TreeSet类。HashSetTreeSet都不能存储重复的元素,通常情况下我们都使用HashSet,只有需要排序功能的情况下我们使用TreeSet

List接口实现有两个类:ArrayListLinkedListArrayList底层是采用数组实现的,所以它的随即访问效率很高,LinkedList是采用双向循环链表实现的,对于频繁的插入删除操作使用LinkedList比较方便。

Map接口实现了HashMap类,它HashSet都是通过hash算法实现的。从SortedMap实现了TreeMap类,所以TreeMap中的元素是排序的。通常情况下我们应该使用HashMap,只有当需要排序时,我们使用TreeMap

原创粉丝点击