package collection;
import java.util.ArrayList;
import java.util.List;
import property.Item;
import charactor.APHero;
import charactor.Hero;
public class TestCollection {
public static void main(String[] args) {
//对于不使用泛型的容器,可以往里面放英雄,也可以往里面放物品
List heros = new ArrayList();
heros.add(new Hero("盖伦"));
//本来用于存放英雄的容器,现在也可以存放物品了
heros.add(new Item("冰杖"));
//对象转型会出现问题
Hero h1= (Hero) heros.get(0);
//尤其是在容器里放的对象太多的时候,就记不清楚哪个位置放的是哪种类型的对象了
Hero h2= (Hero) heros.get(1);
//引入泛型Generic
//声明容器的时候,就指定了这种容器,只能放Hero,放其他的就会出错
List<Hero> genericheros = new ArrayList<Hero>();
genericheros.add(new Hero("盖伦"));
//如果不是Hero类型,根本就放不进去
//genericheros.add(new Item("冰杖"));
//除此之外,还能存放Hero的子类
genericheros.add(new APHero());
//并且在取出数据的时候,不需要再进行转型了,因为里面肯定是放的Hero或者其子类
Hero h = genericheros.get(0);
}
}
不指定类型的ArrayList,其get方法返回的是Object类的对象,所以必须强制类型转换。但是可以允许放所有其他类的对象进入ArrayList中。
但对于制定类型的ArrayList,在get的时候就不用强制类型转换。但不允许放除制定类型的对象进入ArrayList中
遍历
package Set;
import java.util.ArrayList;
import java.util.Iterator;
public class ArrayListFor {
public static void main(String[] args) {
ArrayList<Hero> heroes = new ArrayList<>();
heroes.add(new Hero("hakimi0"));
heroes.add(new Hero("hakimi1"));
heroes.add(new Hero("hakimi2"));
heroes.add(new Hero("hakimi2"));
System.out.println("普通下标for");
for(int i = 0; i < heroes.size(); i++){
System.out.println(heroes.get(i).toString());
}
System.out.println("\n迭代器for");
for(Iterator<Hero> it = heroes.iterator(); it.hasNext();){
Hero h = it.next();
System.out.println(h.toString());
}
System.out.println("\n增强for");
for(Hero h : heroes){
System.out.println(h.toString());
}
}
}
ArrayList和LinkedList性能比较
实际测试中,发现定位的开销还是比数组移动的开销大的,因此在中间插入数据,实际上还是ArrayList更快
Set
HashSet: 无序
LinkedHashSet: 按照插入顺序
TreeSet: 从小到大排序
package collection;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.TreeSet;
public class TestCollection {
public static void main(String[] args) {
HashSet<Integer> numberSet1 =new HashSet<Integer>();
//HashSet中的数据不是按照插入顺序存放
numberSet1.add(88);
numberSet1.add(8);
numberSet1.add(888);
System.out.println(numberSet1);
LinkedHashSet<Integer> numberSet2 =new LinkedHashSet<Integer>();
//LinkedHashSet中的数据是按照插入顺序存放
numberSet2.add(88);
numberSet2.add(8);
numberSet2.add(888);
System.out.println(numberSet2);
TreeSet<Integer> numberSet3 =new TreeSet<Integer>();
//TreeSet 中的数据是进行了排序的
numberSet3.add(88);
numberSet3.add(8);
numberSet3.add(888);
System.out.println(numberSet3);
}
}
比较器
Comparator
TestCollection.java
package STL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Random;
public class TestCollection {
public static void main(String[] args) {
Random r = new Random();
List<Hero> hs = new ArrayList<>();
for(int i = 0; i < 10; i++)
hs.add(new Hero("hero" + i,r.nextInt(100),r.nextInt(100)));
System.out.println("初始化后的集合:");
System.out.println(hs);
Comparator<Hero> c = new Comparator<Hero>(){
/* Lamada
Comparator<Hero> c = (h1, h2) -> {
if(h1.hp >= h2.hp)
return 1;
return -1;
};
*/
@Override
public int compare(Hero h1, Hero h2){
if(h1.hp >= h2.hp)
return 1;
return -1;
}
};
Collections.sort(hs, c);
System.out.println("按照hp排序后的集合:");
System.out.println(hs);
}
}
Hero.java
package STL;
public class Hero {
String name;
int hp;
int damage;
public Hero(){}
public Hero(String name){
this.name = name;
}
public Hero(String name, int hp,int damage){
this.name = name;
this.hp = hp;
this.damage = damage;
}
public Hero(String name, int hp){
this.name = name;
this.hp = hp;
}
public String toString(){
return this.name;
}
}
Comparable
Hero.java
package STL;
public class Hero implements Comparable<Hero>{
String name;
int hp;
int damage;
public Hero(){}
public Hero(String name){
this.name = name;
}
public Hero(String name, int hp,int damage){
this.name = name;
this.hp = hp;
this.damage = damage;
}
public Hero(String name, int hp){
this.name = name;
this.hp = hp;
}
@Override
public int compareTo(Hero another){
if(damage < another.damage)
return 1;
return -1;
}
public String toString(){
return this.name;
}
}