Collection 和 泛型

markdown ### 枚举 Enum ```text 有限的确定的对象 enum Temp{} ``` ### 集合 ```text #单列集合,存储一个一个的对象 public interface Collection extends Iterable #有序,可重复的对象 public interface List extends Collection ArrayList,LinkedList,Vector #无序,不可重复的对象 public interface Set extends Collection HashSet,LinkedHashSet,TreeSet #双列集合,存储一对一对的对象 public interface Map HashMap,LinkedHashMap,TreeMap,Hashtable,Properties ``` #### 1.Collection ##### ArrayList ```text Collection temp = new ArrayList(); temp.add(new String("hello")); Person person = new Person(12,"name",12.2); temp.add(person); System.out.println(temp.contains(new Person(12, "name", 12.2))); System.out.println(temp.contains(new String("hello"))); for (Object o : temp) { System.out.println(o.toString()); } temp.remove("hello"); for (Object o : temp) { System.out.println(o.toString()); } temp.add(23); temp.add(3423); Iterator iterator = temp.iterator(); while (iterator.hasNext()) { if (iterator.next() instanceof Person) System.out.println("是人"); } temp.clear(); ``` ##### LinkedList ```text 底层使用双向链表,高效插入删除应当使用此类 ``` ##### Vector ```text 过于古老不建议使用 ``` ##### HashSet -- LinkedHashSet ```text 频繁的遍历 应当使用LinkedHashSet ``` ##### TreeSet ```text 添加的数据要求是同一个类 ``` #### 2.Map #### HashMap -- LinkedHashMap TreeMap Properties ```text Map hashMap = new HashMap(); hashMap.put(12,"harlod"); hashMap.put("name","dashwood"); System.out.println(hashMap.containsKey("name")); Set set = hashMap.keySet(); Iterator iterator = set.iterator(); while(iterator.hasNext()) System.out.println(iterator.next()); for (Object value : hashMap.values()) { System.out.println(value); } ``` ```text 工具类: java.util.Collections ``` ### 泛型 ```text 规范数据类型,保证数据的安全 ``` #### 范型类 ```text class Information{ private Integer id; private String name; private T desc; } Information information = new Information(); ``` #### 范型方法 ```text #在范型方法中定义的范型与类的范型无关 public List getInformation(E[] array) { } ``` ```text List 与 List 共同父类 List ``` *

*[返回教程主页](https://www.monody.net/p/blog-page_3.html)*

*

评论