码迷,mamicode.com
首页 > 编程语言 > 详细

java-集合框架

时间:2020-05-24 22:37:32      阅读:93      评论:0      收藏:0      [点我收藏+]

标签:treeset   循环   int   根据   效率比较   链表   treemap   性能   style   

集合框架

为什么需要集合框架?

如果并不知道程序运行时会需要多少对象,或者需要更复杂方式存储对象——可以使用Java集合框架;使用数组:扩充,插入,删除 不方便。

Java集合框架提供了一套性能优良、使用方便的接口和类,它们位于java.util包中

接口:

Collection<--------List<--------ArrayList

?                                                 LinkedList

?               Set<--------HashSet

?                                TreeSet

 

Map<---------HashMap

?                      TreeMap

1.Collection 接口存储一组不唯一,无序的对象

2.List 接口存储一组不唯一,有序(插入顺序)的对象

3.Set 接口存储一组唯一,无序的对象

Map:Map接口存储一组键值对象,提供key到value的映射

List接口的实现类

ArrayList:遍历及随机访问效率高

LinkedList采用链表存储方式。插入、删除元素时效率比较高

狗狗类
package cn.kgc.wu;

public class Dog extends Pet{
    
    private String strain;//品种

    public Dog() {
        
        
    }

    public Dog(String name,String strain) {
        super(name);
        this.strain = strain;
    }

    public String getStrain() {
        return strain;
    }

    public void setStrain(String strain) {
        this.strain = strain;
    }
父类
package cn.kgc.wu;

public class Pet {
    protected String name;
    protected int health;
    protected int love;
    
    public Pet() {
        
        
    }

    

    public Pet(String name) {
        
        this.name = name;
        
    }



    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getHealth() {
        return health;
    }

    public void setHealth(int health) {
        this.health = health;
    }

    public int getLove() {
        return love;
    }

    public void setLove(int love) {
        this.love = love;
    }
    public void show() {
        System.out.println("宠物的自白:\n我的名字叫" + this.name + 
                ",健康值是"    + this.health + ",和主人的亲密度是"
                + this.love + "。");
    }
}

ArryList测试类
package cn.kgc.wu;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.junit.Test;

public class TestAa {
    @Test
    public void test01(){
    Dog ououDog = new Dog("欧欧","雪娜瑞");
    Dog yayaDog = new Dog("亚亚", "拉布拉多");
    Dog meimeiDog = new Dog("美美", "雪娜瑞");
    Dog feifeiDog = new Dog("菲菲", "拉布拉多");
    
    List dogs = new ArrayList();
    
    dogs.add(ououDog);
    dogs.add(yayaDog);
    dogs.add(meimeiDog);
    dogs.add(2, feifeiDog );
     System.out.println("共计有" + dogs.size() + "条狗狗。");
     System.out.println("分别是:");
     for(int i =0;i<dogs.size();i++){
         Dog dog=(Dog)dogs.get(i);
         
         System.out.println(dog.getName()+"  "+dog.getStrain());
     }
     boolean contains=dogs.contains(feifeiDog);
     System.out.println(contains);
    
}

 

LingedList测试类
@Test
    public void test02(){
        Dog ououDog = new Dog("欧欧","雪娜瑞");
        Dog yayaDog = new Dog("亚亚", "拉布拉多");
        Dog meimeiDog = new Dog("美美", "雪娜瑞");
        Dog feifeiDog = new Dog("菲菲", "拉布拉多");
        LinkedList dogs = new LinkedList<>();
        dogs.add(ououDog);
        dogs.add(yayaDog);
        dogs.add(meimeiDog);
        dogs.addFirst(feifeiDog);
        for(int i =0;i<dogs.size();i++){
             Dog dog=(Dog)dogs.get(i);
             
             System.out.println(dog.getName()+"  "+dog.getStrain());
         }
        dogs.removeFirst();
        System.out.println("***********");
        Dog dog=(Dog)dogs.getFirst();
        Dog dog1=(Dog)dogs.getLast();
        System.out.println(dog.getName());
        System.out.println(dog1.getName());
    }

 

 

 

Map接口

专门处理键值映射数据的存储,可以根据键实现对值的操作

最常用的实现类是HashMap

Map测试类
@Test
    public void test6(){
        Dog ououDog = new Dog("欧欧","雪娜瑞");
        Dog yayaDog = new Dog("亚亚", "拉布拉多");
        Dog meimeiDog = new Dog("美美", "雪娜瑞");
        Dog feifeiDog = new Dog("菲菲", "拉布拉多");
        Map map=new HashMap<>();
        map.put(feifeiDog.getName(),feifeiDog);
        map.put(meimeiDog.getName(), meimeiDog);
        map.put(yayaDog.getName(), yayaDog);
        map.put(ououDog.getName(), ououDog);
        
        Set keySet=map.keySet();
        Iterator it=keySet.iterator();
        while(it.hasNext()){
            String key=(String)it.next();
            Dog dog=(Dog)map.get(key);
            System.out.println(dog.getName()+" "+dog.getStrain());
        }
        
        System.out.println("");

遍历集合的方法

1.增强for循环遍历

2.迭代器Iterator遍历

 

java-集合框架

标签:treeset   循环   int   根据   效率比较   链表   treemap   性能   style   

原文地址:https://www.cnblogs.com/wufanfan/p/12953060.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!