标签:treeset 循环 int 根据 效率比较 链表 treemap 性能 style
为什么需要集合框架?
如果并不知道程序运行时会需要多少对象,或者需要更复杂方式存储对象——可以使用Java集合框架;使用数组:扩充,插入,删除 不方便。
Java集合框架提供了一套性能优良、使用方便的接口和类,它们位于java.util包中
接口:
? LinkedList
? Set<--------HashSet
? TreeSet
? TreeMap
2.List 接口存储一组不唯一,有序(插入顺序)的对象
3.Set 接口存储一组唯一,无序的对象
Map:Map接口存储一组键值对象,提供key到value的映射
狗狗类 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()); }
最常用的实现类是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("");
遍历集合的方法
2.迭代器Iterator遍历
标签:treeset 循环 int 根据 效率比较 链表 treemap 性能 style
原文地址:https://www.cnblogs.com/wufanfan/p/12953060.html