java中的集合

Collection

List类

  Java API中所用的集合类,都是实现了Collection接口,他的一个类继承结构如下:

              Collection<--List<--Vector
              Collection<--List<--ArrayList
              Collection<--List<--LinkedList
              Collection<--Set<--HashSet
              Collection<--Set<--HashSet<--LinkedHashSet
              Collection<--Set<--SortedSet<--TreeSet

Vector :
   基于Array的List,其实就是封装了Array所不具备的一些功能方便我们使用,它不可能不受Array的限制。性能也就不可能超越Array。所以,在可能的情况下,我们要多运用Array。
ArrayList:
  同Vector一样是一个基于Array上的链表,他就像我们在数据结构中说到的线性表。他在内存中的存储是顺序存储的,每个元素都是相连的,当删除一个元素时,其后所有的元素要前移, 当添加一个元素时,其后所有的元素要后移。他的缺点就是删除、添加效率慢。优点是遍历快。
LinkedList:
  LinkedList不同于前面两种List,它不是基于Array的,所以不受Array性能的限制。它每一个节点(Node)都包含两方面的内容:1.节点本身的数据(data);2.下一个节点的信息(nextNode)。所以当对LinkedList做添加,删除动作的时候就不用像基于Array的List一样,必须进行大量的数据移动。只要更改nextNode的相关信息就可以实现了。所以优点是插入删除快,但是遍历慢。
  
这里以ArrayList为例说明用法:

public class TestArrayList {
    public static void main(String[] args) {    
        //创建集合list1,和list2.集合用来存储字符串类型。
        ArrayList<String> list1 = new ArrayList<String>();
        ArrayList<String> list2 = new ArrayList<String>();
        //将输入的字符串添加进集合list1中。
        Scanner scanner = new Scanner(System.in);
        for(int i = 0; i<10; i++){
            String in = scanner.next();
            list1.add(in);
        }
        //将“添加”字符串天加到集合位置为1 的位置。
        list1.add(1, "添加");
        //将list1 的内容全部添加进list2 中。
        list2.addAll(list1);
        //遍历list1 集合。
        for (int i = 0; i < list1.size(); i++) {
            System.out.print(list1.get(i));         
        }
        System.out.println();
        //for each 循环遍历list2
        for(String s:list2){
            System.out.println(s);
        }
    }
}

Set类

HashSet:
  HashSet是无序的。虽然Set同List都实现了Collection接口,但是他们的实现方式却大不一样。List基本上都是以Array为基础。但是Set则是在HashMap的基础上来实现的,这个就是Set和List的根本区别。HashSet的存储方式是把HashMap中的Key作为Set的对应存储项。看看HashSet的add(Object obj)方法的实现就可以一目了然了。

 public boolean add(Object obj)
    {
        return map.put(obj, PRESENT) == null;
    }

  这个也是为什么在Set中不能像在List中一样有重复的项的根本原因,因为HashMap的key是不能有重复的。
TreeSet:
  SortedSet的子类,它不同于HashSet的根本就是TreeSet是有序的。它是通过SortedMap来实现的。

public class TestHashSet {
    public static void main(String[] args) {
        //新建一个哈希表
        HashSet<Integer> set = new HashSet<>();
        //建立一个随机对象。
        Random random = new Random();
        int count = 0; 
        while (set.size()<10) {
            //随机产生的都是两位数。
            int i = random.nextInt(90)+10;
            System.out.println(count++ +"随机得到的数据为:"+i);
            //将产生的随机数添加进集合中。
            set.add(i);
        }
        System.out.println("已经放进去了"+set.size());
        //实现Set的遍历。
        Iterator<Integer> it = set.iterator();
        while(it.hasNext()){
            System.out.println(it.next());      
        }
    }
}

Map

HashMap类

  HashMap类是基于key和value键值的。key和value可以为null。

HashTable类

  HashTable类也是基于key和value键值的。key和value不同的是它们不可以为null。
  

public class TestHashMap {
    public static void main(String[] args) {    
        //创建hashmap对象。
        HashMap<String,Student> hashmap = new HashMap();
        //创建学生对象。
        Student zhangsan = new Student("张三");
        Student lisi = new Student("李四");
        Student wangwu = new Student("王五");
        //将对象放入hashmap中。
        hashmap.put("1", zhangsan);
        hashmap.put("2", lisi);
        hashmap.put("3", wangwu);
        //输出键值key为1的学生对象的名字。
        System.out.println(hashmap.get("1").getName());
        //实现hashmap的遍历。
        //获得所有键值,将其存储在String类型的集合中。
        Set<String> keys = hashmap.keySet();
        //利用集合Set的迭代器实现遍历。
        Iterator<String> it =   keys.iterator();
        while(it.hasNext()){
            String  key = it.next();
            System.out.println(hashmap.get(key).getName());         
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小_爽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值