JAVA中几种常见集合的使用实例

Java.util.ArrayList(类):

None.gif import  java.awt. * ;
None.gif
import  java.util. * ;
None.gif
public   class  CollectionTest
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {//List是一个能包含重复元素的已排序的Collection,有时list也称为序列,List第一个元素的下标为0 
ExpandedSubBlockStart.gifContractedSubBlock.gif
     public String colors[]=dot.gif{"red","white","blue"};//定义一个字符数组
InBlock.gif
InBlock.gif     
//构造函数
InBlock.gif
     public CollectionTest()
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif          ArrayList list
=new ArrayList();//实例化一个ArrayList
InBlock.gif
          list.add(Color.magenta);//向里面添加一个元素,这里是颜色
InBlock.gif
          
InBlock.gif          
for(int count=0;count<colors.length;count++)
InBlock.gif              list.add(colors[count]);
//加入开始声明的数组中的元素
InBlock.gif
          
InBlock.gif          list.add(Color.cyan);     
//颜色  导入awt包
InBlock.gif
          System.out.println("\nArrayList");
InBlock.gif          
for(int count=0;count<list.size();count++)
InBlock.gif             System.out.println(list.get(count)
+" ");//从arrayList中读取 元素
InBlock.gif
          
InBlock.gif          removeString(list);
InBlock.gif          System.out.println(
"\n\nArrayList after calling"+"removeString:");
InBlock.gif          
for(int count=0;count<list.size();count++)
InBlock.gif                 System.out.println(list.get(count)
+" ");
ExpandedSubBlockEnd.gif     }

InBlock.gif     
InBlock.gif     
InBlock.gif        
public void removeString(Collection collection)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif             Iterator itrator
=collection.iterator();    //声明一个迭代
InBlock.gif             
//调用itrator的hasNext方法判断Collection是否还包含元素
InBlock.gif
             while(itrator.hasNext())
ExpandedSubBlockStart.gifContractedSubBlock.gif             
dot.gif{
InBlock.gif                  
//调用itrator的next方法获得下一个元素的引用
InBlock.gif
                  if( itrator.next() instanceof String ) // instanceof 判断是否是String 的实例
InBlock.gif
                        itrator.remove();    //如果是的 则删除
ExpandedSubBlockEnd.gif
             }

ExpandedSubBlockEnd.gif        }

InBlock.gif     
InBlock.gif     
public  static void main(String[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif          
new CollectionTest();
ExpandedSubBlockEnd.gif     }

InBlock.gif     
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif

该例示范了ArrayList的使用 先声明了一String类型的数组,里面存储了“颜色”,是用字符串写出的颜色,将这个字符串数组存入ArrayList实例,同时还存入了awt包内的颜色实例,全部存入后利用迭代,删除不符要求的假数据,也就是我们用字符串写的颜色,也用到了 instanceof 它是一个二元操作符,类似于equals用于判断instanceof左边 的对象 是否是 右边对象的实例,若是 返回真,这里就可以判断 ArrayList里面的真假颜色,假颜色是 字符串的 实例,所以我们通过迭代 一个个对比。只要是String的实例就将其从数组中删除,所以最后 ArrayList里面仅仅剩下二个元素,运行效果如下:

 java.util.HashSet(类);

None.gif // Set是包含独一无二元素的Collection,HashSet把它的元素存储在哈希表中,而TreeSet把它的元素存储在树中
None.gif
import  java.util. * ;
None.gif
None.gif
public   class  SetTest
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif     
private String colors[]=dot.gif{"orange","tan","orange","white",  "gray"};
InBlock.gif     
public SetTest()
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif              ArrayList list;
InBlock.gif              list
=new ArrayList(Arrays.asList(colors));
InBlock.gif              System.out.println(
"ArrayList:"+list);
InBlock.gif              printNonDuplicates(list);  
ExpandedSubBlockEnd.gif     }

InBlock.gif 
InBlock.gif 
InBlock.gif     
public void printNonDuplicates(Collection collection)
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif          
//构造HashSet删除Collection中多余的元素
InBlock.gif
          HashSet set=new HashSet(collection);
InBlock.gif          
// 将coolection放入HashSet后即会消除重复元素          
InBlock.gif
          System.out.println("set:"+set);
InBlock.gif
InBlock.gif          Iterator itrator
=set.iterator();
InBlock.gif          System.out.println(
"\nNonDuplicates are:");
InBlock.gif          
while(itrator.hasNext())
InBlock.gif          System.out.println(itrator.next()
+" ");
InBlock.gif          System.out.println();
ExpandedSubBlockEnd.gif     }

InBlock.gif         
InBlock.gif     
public static void main(String[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{  
InBlock.gif            
new SetTest(); 
ExpandedSubBlockEnd.gif     }

InBlock.gif
ExpandedBlockEnd.gif}

None.gif
None.gif


可以看到重复元素 orange除去了

java.util.Set(接口)
None.gif import  java.util.HashSet;
None.gif
import  java.util.Iterator;
None.gif
import  java.util.Set;
None.gif
None.gif
class  TestSet 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {    
InBlock.gif     
public static void main(String args[])
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{
InBlock.gif         Set set 
= new HashSet();
InBlock.gif         set.add(
"aaa");
InBlock.gif         set.add(
"bbb");
InBlock.gif         set.add(
"aaa");//后面加入的重复性元素均无效
InBlock.gif
         set.add("bbb");
InBlock.gif         set.add(
"aaa");
InBlock.gif         set.add(
"bbb");
InBlock.gif         set.add(
"aaa");
InBlock.gif         set.add(
"bbb");
InBlock.gif         set.add(
"aaa");
InBlock.gif         set.add(
"bbb");
InBlock.gif         Iterator ite
=set.iterator();
InBlock.gif         System.out.println(set.size());
//the result is 2
InBlock.gif
         while(ite.hasNext())
ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{
InBlock.gif             System.out.println(
"----"+ite.next());
ExpandedSubBlockEnd.gif          }

ExpandedSubBlockEnd.gif     }

ExpandedBlockEnd.gif}


我们看到效果

java.util.List(接口)
None.gifpackage tt;
None.gif
None.gif
import java.util.Arrays;
None.gif
import java.util.Collections;
None.gif
import java.util.Iterator;
None.gif
import java.util.LinkedList;
None.gif
import java.util.List;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public class ListTest dot.gif{
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static void baseUse()dot.gif{
InBlock.gif            
//链表实现
InBlock.gif
            List list = new LinkedList();
InBlock.gif            
//数组实现
InBlock.gif            
//List list = new ArrayList();
InBlock.gif
            list.add("a");//向列表的尾部追加"a"
InBlock.gif
            System.out.println("使用list接口的add()一个参数的方法:"+list);
InBlock.gif            list.add(
0,"b");//在指定位置插入"b"
InBlock.gif
            System.out.println("使用list接口的add二个参数的方法:"+list);
InBlock.gif            list.remove(
"a");//移除列表中"a"
InBlock.gif
            System.out.println("使用list接口的remove()方法删除a:"+list);
ExpandedSubBlockEnd.gif     }
    
ExpandedSubBlockStart.gifContractedSubBlock.gif      
public static void useSort()dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            String[] strArray 
= new String[] dot.gif{"z""a""c","C"};
InBlock.gif            List list 
= Arrays.asList(strArray);
InBlock.gif            System.out.println(list);
InBlock.gif            Collections.sort(list);
//根据元素自然顺序排序
InBlock.gif
            System.out.println("自然顺序:"+list);
InBlock.gif            Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
//根据指定的字母方式排序    
InBlock.gif
            System.out.println("指定字母方式:"+list);
InBlock.gif            Collections.sort(list, Collections.reverseOrder());
//根据反转自然顺序方式排序
InBlock.gif
            System.out.println("反转自然顺序:"+list);
InBlock.gif            Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
InBlock.gif            System.out.println(list);
InBlock.gif            Collections.reverse(list);
//反转列表排序
InBlock.gif
            System.out.println(list);
ExpandedSubBlockEnd.gif      }

InBlock.gif           
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static void main(String[] args) dot.gif{
InBlock.gif    
InBlock.gif        baseUse();
InBlock.gif    
//    useSort();
ExpandedSubBlockEnd.gif
    }

InBlock.gif
ExpandedBlockEnd.gif}

None.gif

运行
 

java.util.TreeSet(类)
None.gif package  tt;
None.gif
None.gif
import  java.util.Iterator;
None.gif
import  java.util.TreeSet;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   class  TreeSetTest  dot.gif {
InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif     
public static void main(String args[])dot.gif{
InBlock.gif          TreeSet a 
= new TreeSet();
InBlock.gif          a.add(
"1167014513046,hondanna_mousepress");
InBlock.gif          a.add(
"1167014512046,hondanna_mousepress_usefull");
InBlock.gif          a.add(
"1167014511046,hondanna_mousepress_num");
InBlock.gif          a.add(
"1167014515437,hondanna_mousepress");
InBlock.gif          a.add(
"1167014514438,hondanna_mousepress_usefull");
InBlock.gif         Iterator iterator 
= a.iterator();
InBlock.gif         
while(iterator.hasNext())
InBlock.gif             System.out.println(iterator.next());
ExpandedSubBlockEnd.gif         }

ExpandedBlockEnd.gif}

None.gif
运行结果:

TreeSet为使用树来进行存储的Set接口提供了一个工具,对象按升序存储,访问和存储是很快的,在存储了大量的需要进行快速检索的排序信息的情况下,TreeSet是一个很好的选择
构造函数定义为:
TreeSet()-构造一个空的树集合,该树集合将根据其元素的自然顺序按升序排序。
TreeSet(Collection c)-构造了一个包含了c的元素的树的集合。
TreeSet(Comparator comp)-构造了一个空的树的集合,它按照由comp指定的比较函数进行排序。
TreeSet(SortedSet ss)-构造了一个包含ss的元素的树集合。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值