Java基础知识:集合

黑马程序员---------Java基础知识:集合

------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------

                

一:为什么要出现集合类?

       因为在JAVA中,对事物的体现都是以对象的形式来体现,对象多了,为了方便对类的操作就出现了集合对类的存储。其实集合就是一个存储对象的一种方式。作用和数组差不多,不过又与数组有所差别。集合内存的都是对象,集合长度都是可变的,这与数组不一样。

二:常用的集合类

       在编程过程中常用的集合类有以下几个

1.      list,特点是list中元素是有序的,元素是可以重复,可以用索引查找对象。

List下又分有三种ArrayList ,LinkedList和Vector.这三种的特点如下。

~ArrayList底层的数据结构是数组结构,查询快,增删慢些,线程不同步。

~LinkedList底层使用链表结构,所以增删快,查询慢些。线程不同步。

~Vector 底层是数组结构。线程同步。但是因为效率低所以被ArrayList取代,在合并流中有使用到。

2.      Set,元素是无序的,因而元素不能重复,而且存入取出的顺序不一样。

Set下有份为两种HashSet和 TreeSet..特点如下:

~HashSet底层是哈希表,线程不安全,通过hashCode 和equals来完成元素的唯一性。

~ TreeSet可以对元素进行排序。

3.      Map<k,v>集合,该集合存的是键值的对,一对一对的存取而且要保证唯一。

Mapf分为三类Hashtable , HashMap , TreeMap,其特点如下:

~Hashtable底层是哈希表,不能存NULL,线程同步。

~ HashMap底层哈希表,可以存NULL,线程不同步。

~ TreeMap 底层二叉树,线程不同步,可以进行键排序。

三:集合的操作

       这几种集合类操作都有所不同。以下通过例程来说明。

  List集合可以通过使用角标对元素进行增删改查,ArrayList ,LinkedList使用迭代器进行元素的取出,而Vector则使用枚举取出元素。HashSet和 TreeSet通过hashCode 和equals保证元素的唯一性。TreeSet可以对元素进行排序,可以通过实现Comparable中compareTo(Object o)对指定顺序进行排序。Map<k,v>集合有两种取出方式,一种是Set keyset ,另一种是Set<Map.Entry<String, String>>,用法可以参照例程。

 

 import java.util.*;

importjava.util.Map.Entry;

 

public class JiHeDemo {

 

   public static void main(String[] args) {

      arrayListDemo();

      linkedListDemo();

      vectorListDemo();

      hashSetDemo();

      treeSetDemo();

      hashMapDemo();

   }

 public static void arrayListDemo()

 {

   ArrayList<String> a =new ArrayList<String>();//创建对象

 

   a.add("香蕉");//添加元素

   a.add("香蕉");

   a.add("香橙");

   a.add("桃子");

 

   sop(a.size());//获取集合长度 7

   a.remove(2);//删除位号为2的元素

   //a.retainAll(c);//取交集,a保留与c相同的元素

   //a.removeAll(c);//取交集,A保留与c不同的元素

   Iterator<String> it =a.iterator(); //迭代器取出元素

   while(it.hasNext())//判断是否还有元素

   {

      sop(it.next());//取出元素并打印

   }

  

  

   Object[] ch =a.toArray();//将集合转数组

   for(int x=0;x<ch.length;x++)

   {

      sop(ch[x]);//打印出来

   }

 }

 public static void linkedListDemo()

 {

    LinkedList<String> a =new LinkedList<String>();

    a.addFirst("西红柿");//特有方法

    a.addFirst("黄花菜");

    a.addLast("金针菇");//特有方法

    a.add("马铃薯");

    a.add("娃娃菜");

    //使用特有方法可以实现堆先进先出,栈先进后出

    Iterator<String> it =a.iterator(); //迭代器取出元素

      while(it.hasNext())//判断是否还有元素

      {

         sop("linkedList"+it.next());//取出元素并打印

      }

    

 }

 public static void vectorListDemo()

 {

    Vector<String> v = newVector<String>();

    v.add("园的");

    v.add("方的");

    v.add("长的");

    v.add("短的");

    Enumeration<String>en=v.elements();//枚举,特有的方法

    while(en.hasMoreElements())//判断是否还有元素

    {

       sop(en.nextElement());//打印元素

    }

 }

 public static void hashSetDemo()

 {

    //因为存的是类,所以需要有一个类,内部复写hashCoe equals,去除相同元素

    HashSet<Flower> ha =newHashSet<Flower>();

    ha.add(new Flower("红色",50));

    ha.add(new Flower("红色",40));

    ha.add(new Flower("粉色",50));

    ha.add(new Flower("绿色",15));

    ha.add(new Flower("桃色",20));

    ha.add(new Flower("红色",50));

    Iterator<Flower> it = ha.iterator();

    while(it.hasNext())

    {

       Flowerf=it.next();

       sop(f.getColor()+" "+f.getNum());

    }

    

    

 }

 public  static void treeSetDemo()//使用TreeSet时为了让对象有比较性需要实现Comparable,复写comparaTo(Object obj)

 {//按照数量先排序,如果数量相同按照名字排序,去掉重复的对象

    TreeSet<Flower> tr = newTreeSet<Flower>();

    tr.add(new Flower("粉梅花1",50));

    tr.add(new Flower("粉梅花2",50));

    tr.add(new Flower("粉梅花1",45));

    tr.add(new Flower("红梅花2",45));

    tr.add(new Flower("蓝梅花3",12));

    tr.add(new Flower("绿梅花0",18));

    tr.add(new Flower("绿梅花0",18));

    Iterator<Flower> it =tr.iterator();

    while(it.hasNext())

    {

       Flower fl =(Flower)it.next();

       sop(fl.getColor()+" "+fl.getNum());

    }

 }

 

 public  static void hashMapDemo()

 {//使用集合存入数据并取出打印

    Map<String, String> map =new HashMap<String, String>();

    map.put("01""张山");

    map.put("02""赵一");

    map.put("03""李四");

    map.put("05""钱二");

    map.put("04""吴六");

    //map取出元素有两种方法,一种是使用Set<String> keyset,一种是

    Set<String> keyset =map.keySet();//将获取KEY放入set集合当中

    Iterator<String> it =keyset.iterator();//使用迭代器

    while(it.hasNext())//判断有没有元素

    {

       String key=it.next();//KEY取出,然后就可以使用get(key)获取值

       String vaule = map.get(key);//获取值

       sop(key+" ... "+vaule);//打印见和值

    }

    //第二种取出方式Set<Map.Entry<String,String>> ,

    Set<Map.Entry<String,String>>   entrySet =map.entrySet();//将键值关系存入集合

    Iterator<Entry<String, String>> it1 =  entrySet.iterator();//迭代器

    while(it1.hasNext())

    {

      Map.Entry<String, String> me =it1.next();//把键值关系取出

      String key1 =me.getKey();//获取键

      String vaule1 =me.getValue();//获取值

      sop(key1+" : "+vaule1);

       

    }

       

 }

 

 public static void sop(Object obj)

 {

    System.out.println(obj);

 }

}

class Flower implements Comparable<Object>

{

   private String color;//颜色

   private int num;//数量

   Flower(String color,int num)

   {

      this.color =color;

      this.num = num;

   }

   public int hashCode()//复写

   {

      return color.hashCode()+num*41;

   }

   public boolean equals(Object obj)

   {

      if(!(obj instanceof Flower))//当输入的类不是Flower

      {

         return false;

      }

      Flower f=(Flower)obj;

      return this.color.equals(f.color)&&this.num==f.num;

   }

   public String getColor() {

      return color;

   }

  

   public int getNum() {

      return num;

   }

  

   public int compareTo(Object o) {

      if(!(o instanceof Flower))

         throw new RuntimeException("这个不是花对象");

      Flower f=(Flower)o;

      if(this.num>f.num)

         return 1;

      if(this.num==f.num)

      {

         return this.color.compareTo(f.color);

      }

     

      return -1;

   }

  

  

}

 

 

总结:集合其实用法差不多,用处却不大一样,假如需要排序,则使用TreeSet。假如只需要保证元素的唯一性则使用hashset。假如输入输出需要取第一个元素,最后一个元素则使用Linkedlist。假如需要按位操作,最好使用Arraylist。假如是键值对,则使用Map集合。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值