Java 学习之容器

Array.asList 不支持 add() remove ()因为他的底层是数组,大小是固定的。

List<Snow> snow1 = Arrays.asList(
      new Crusty(), new Slush(), new Powder());

    // Won't compile:
     List<Snow> snow2 = Arrays.asList(
       new Light(), new Heavy());
    // Compiler says:
    // found   : java.util.List<Powder>
    // required: java.util.List<Snow>
     System.out.println(snow2);
    // Collections.addAll() doesn't get confused:
    List<Snow> snow3 = new ArrayList<Snow>();
    Collections.addAll(snow3, new Light(), new Heavy());

    // Give a hint using an
    // explicit type argument specification:
    List<Snow> snow4 = Arrays.<Snow>asList(
       new Light(), new Heavy());

可能是从jdk1.8开始, 编译可以通过,因为是继承关系。

Set

HashSet 最快获取元素的方式

TreeSet 升序保存

LinkedHashSet 按照添加顺序保存对象

Map 

同上

List

ArrayList 随机访问元素快,插入删除慢

LinkedList 随机访问元素慢,插入删除快

迭代器


 Iterator 可以移除由next()产生的最后一个元素,调用remove()之前必须调用next().

ListIterator

 List<Pet> pets = Pets.arrayList(8);
    ListIterator<Pet> it = pets.listIterator();
    System.out.println(pets);
    while(it.hasNext())
      System.out.println(it.next() + ", " + it.nextIndex() +
        ", " + it.previousIndex() + "; ");//这里如果不调用it.next()程序会一直运行
    System.out.println();
    // Backwards:
    while(it.hasPrevious())
    System.out.print(it.previous().id() + " ");
    System.out.println();
    System.out.println(pets);	
    it = pets.listIterator(3);
    
    while(it.hasNext()) {
      it.next();
      it.set(Pets.randomPet());//替换next的值
    }
    System.out.println(pets);
  }

因为用的是while,他一直可以看到下一个内容,所以不会跳出。如果用if的话,只会打印出一个值。

Queue 

PriorityQueue

确保调用peek() remove() poll()时获取的元素是队列中优先级最高的元素。

 PriorityQueue<Integer> priorityQueue =
      new PriorityQueue<Integer>();
    Random rand = new Random(47);
    for(int i = 0; i < 10; i++)
      priorityQueue.offer(rand.nextInt(i + 10));
    QueueDemo.printQ(priorityQueue);
   
    List<Integer> ints = Arrays.asList(25, 22, 20,
      18, 14, 9, 3, 1, 1, 2, 3, 9, 14, 18, 21, 23, 25);
    priorityQueue = new PriorityQueue<Integer>(ints);
    QueueDemo.printQ(priorityQueue);
    priorityQueue = new PriorityQueue<Integer>(
        ints.size(), Collections.reverseOrder());//反序
    priorityQueue.addAll(ints);
    QueueDemo.printQ(priorityQueue);

    String fact = "EDUCATION SHOULD ESCHEW OBFUSCATION";
    List<String> strings = Arrays.asList(fact.split(""));
    
    PriorityQueue<String> stringPQ =
      new PriorityQueue<String>(strings);
    System.out.println();
    QueueDemo.printQ(stringPQ);
    stringPQ = new PriorityQueue<String>(
      strings.size(), Collections.reverseOrder());
    stringPQ.addAll(strings);
    QueueDemo.printQ(stringPQ);
    System.out.println();
    Set<Character> charSet = new HashSet<Character>();
    for(char c : fact.toCharArray())
      charSet.add(c); // Autoboxing
    PriorityQueue<Character> characterPQ =
      new PriorityQueue<Character>(charSet);
    QueueDemo.printQ(characterPQ);
  }
//0 1 1 1 1 1 3 5 8 14 
//1 1 2 3 3 9 9 14 14 18 18 20 21 22 23 25 25 
//25 25 23 22 21 20 18 18 14 14 9 9 3 3 2 1 1 

//     A A B C C C D D E E E F H H I I L N N O O O O S S S T T U U U W 
//W U U U T T S S S O O O O N N L I I H H F E E E D D C C C B A A       

//  A B C D E F H I L N O S T U W 

允许重复 最小值最高优先级 

String  空格也算作值,比字母的优先级高

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值