Holding Your Objects 1-4

1.接收未知数量的对象:

  • container(容器):List,Set,Queue,Map.
  • ArrayList:add()添加元素;get()取元素;size()大小;
ArrayList<type parameters>names=new ArrayList<type parameters>();

可以在ArrayList中添加他的子类型

import java.util.*;

class Apple1 extends Apple {}
class Apple2 extends Apple {}
class Apple3 extends Apple {}
class Apple4 extends Apple {}

public class GenericsAndUpcasting {

    public static void main(String[] args) {
        ArrayList<Apple> apples = new ArrayList<Apple>();
        apples.add(new Apple1());
        apples.add(new Apple2());
        apples.add(new Apple3());
        apples.add(new Apple4());

        for (Apple c : apples) {
            System.out.println(c);
        }
    }
}

输出

Apple1@659e0bfd
Apple2@2a139a55
Apple3@15db9742
Apple4@6d06d69c

2.基本概念

container:

  • Collection(List,Set,Queue).
  • Map (ArrayList,map),键值对(key-value)使用key找到value.

Upcast

List <Apple> apples=new LinkedList <Apple> ();
Collection <Integer> i=new ArrayList <Integer> ();

3.添加一组元素

Collection.addAll方法

import java.util.*;

public class AddingGroups {

    public static void main(String[] args) {
        Collection<Integer> collection = new ArrayList<Integer>(Arrays.asList(1, 2, 3, 4, 5));
        Integer[] moreInts = { 6, 7, 8, 9, 10 };
        Collections.addAll(collection, 11, 12, 13, 14, 15);
        Collections.addAll(collection, moreInts);

        List<Integer> list = Arrays.asList(16, 17, 18, 19, 20);
        list.set(1, 99);
    }
}

asList()方法将一组方法或一个数组转换成List对象。
Collection.addAll()只接受另一个Collection对象作为参数。
不如Arrays.asList()和Collections.addAll()灵活。

4.打印容器

import java.util.*;

public class PrintingGontainers {

    static Collection fill(Collection<String>collection){
        collection.add("rat");
        collection.add("cat");
        collection.add("dog");
        collection.add("dog");
        return collection;
    }
    static Map fill(Map<String,String>map){
        map.put("rat", "Fuzzy");
        map.put("cat", "Rags");
        map.put("dog", "Bosco");
        map.put("dog", "Sopt");
        return map;
    }

    public static void main(String[] args) {
        System.out.println(fill(new ArrayList<String>()));
        System.out.println(fill(new LinkedList<String>()));
        System.out.println(fill(new HashSet<String>()));
        System.out.println(fill(new TreeSet<String>()));
        System.out.println(fill(new LinkedHashSet<String>()));
        System.out.println(fill(new HashMap<String,String>()));
        System.out.println(fill(new TreeMap<String,String>()));
        System.out.println(fill(new LinkedHashMap<String,String>()));
    }
}

输出

[rat, cat, dog, dog]
[rat, cat, dog, dog]
[rat, cat, dog]
[cat, dog, rat]
[rat, cat, dog]
{rat=Fuzzy, cat=Rags, dog=Sopt}
{cat=Rags, dog=Sopt, rat=Fuzzy}
{rat=Fuzzy, cat=Rags, dog=Sopt}

Collection每个位置只有一项,
Map每个位置有两项,一个key和一个与之关联的value。
由输出可以看出它们的默认打印方式(即由容器的toString()方法决定的):

  • ArrayList和LinkedList都是List类型,都以插入的顺序输出;
  • HashSet,TreeSet和LinkedHashSet都是Set类型,只保留不相同的元素,且不同的Set保存元素的方式不同(HashSet保存方式比较复杂,现在只需要知道他是最快的检索元素的方式);
  • TreeSet以升序比较的方式保存元素;
  • LinkedHashSet与插入的顺序相同。

Map允许使用一个key来寻找一个相关联的对象

  • Map.put(key,value)添加元素(添加一个值并将其与一个key关联)
  • Map.get(key)取得与key关联的值

Map自动调整大小。
Map元素的存储顺序不是插入顺序。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值