集合
来源:互联网 发布:如何联系淘宝客服 编辑:程序博客网 时间:2024/05/29 19:42
一、List 元素有序(储存数据顺序与迭代顺序一致或相反)
1.Vector : 底层数组实现,线程安全,效率低
2.ArrayList : 底层数组实现,线程不安全,效率高,较LinkedList查询快,增删慢
3.LinkedList : 底层链表实现,线程不安全,效率高,较ArrayList增删快,查询慢
二、Set 元素具有唯一性
1.HashSet : 底层哈希表(元素是链表的数组)实现,哈希表依赖哈希值存储。不保证元素的迭代顺序。此类允许使用null值元素。添加功能底层依赖两个方法:hashCode()、equals()
2.TreeSet : 底层二叉树结构,元素有序(比较方法排序):
自然排序:元素对象实现Comparable接口,重写compareTo()方法
import java.util.Comparator;import java.util.TreeSet;public class TreeSetTest1 { public static void main(String[] args) { //自然排序 TreeSet<Student> set = new TreeSet<Student>(); set.add(new Student("年三",20)); set.add(new Student("王一",15)); set.add(new Student("牛二",20)); set.add(new Student("三年",15)); set.add(new Student("一王",17)); set.add(new Student("二牛",20)); for(Student s : set) { System.out.println(s); } }}class Student implements Comparable<Student>{ private String name; private int age; /*年龄的数值 从小到大*/ public int compareTo(Student o) { if(this.age > o.age) return 1; else if(this.age < o.age) return -1; else //年龄相等情况:return 0——>不添加,return 1——>往前排,return -1——>往后排 return -1; } public Student(){} public Student(String name,int age){ this.name = name; this.age = age; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setAge(int age) { this.age = age; } public int getAge() { return age; } public String toString() { return "Student [name="+name+",age="+age+"]"; } public int hashCode() { final int prime = 31; int result = 1; result = prime * result + age; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } public boolean equals(Object obj) { if (this == obj)return true;if (obj == null)return false;if (getClass() != obj.getClass())return false;Student other = (Student) obj;if (age != other.age)return false;if (name == null) {if (other.name != null)return false;} else if (!name.equals(other.name))return false;return true; }}
比较器排序:比较器实现Comparator接口,重写compare()方法
import java.util.Comparator;import java.util.TreeSet;public class TreeSetTest2 { public static void main(String[] args) { //比较器排序 TreeSet<Student> set = new TreeSet<Student>( //创建一个Comparator的子类实例对象(匿名) new Comparator<Student>() { public int compare(Student o1,Student o2) { if(o1.getAge() > o2.getAge()) return -1; else if(o1.getAge() < o2.getAge()) return 1; else return -1; } } ); set.add(new Student("年三",20)); set.add(new Student("王一",15)); set.add(new Student("牛二",20)); set.add(new Student("三年",15)); set.add(new Student("一王",17)); set.add(new Student("二牛",20)); for(Student s : set) { System.out.println(s); } }}class Student{ private String name; private int age; public Student(){} public Student(String name,int age){ this.name = name; this.age = age; } public void setName(String name) { this.name = name; } public String getName() { return name; } public void setAge(int age) { this.age = age; } public int getAge() { return age; } public String toString() { return "Student [name="+name+",age="+age+"]"; } public int hashCode() { final int prime = 31; int result = 1; result = prime * result + age; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } public boolean equals(Object obj) { if (this == obj)return true;if (obj == null)return false;if (getClass() != obj.getClass())return false;Student other = (Student) obj;if (age != other.age)return false;if (name == null) {if (other.name != null)return false;} else if (!name.equals(other.name))return false;return true; }}
3.LinkedHashSet
底层由哈希表和链表实现。哈希表保证元素的唯一性,链表保证元素具有可预知的迭代顺序
三、Map 存储键值对
1.HashMap
基于哈希表的Map实现,哈希表的作用是用来保证键的唯一性,键可以为null,值也可以为null,此实现是不同步的,效率高
2.TreeMap
键是二叉树结构,可以保证键的有序和唯一性,键不能为null(添加元素时,需要调用键的比较方法),值也不能为null
3.Hashtable
此类实现一个哈希表,哈希表将键映射到相应的值,键和值都不能为null(JDK1.0出现)
import java.util.Collection;import java.util.Map;import java.util.Set;import java.util.TreeMap; public class TreeMapTest { public static void main(String[] args) { TreeMap map = new TreeMap(); map.put("one","1" ); map.put("two", "2"); map.put("three", "3"); //方式一 Collection collection = map.values(); for(Object v : collection) { System.out.println("——>"+v); } //方式二 Set keySet = map.keySet(); for(Object k : keySet) { System.out.println(k+"——>"+map.get(k)); } //方式三 Set<Map.Entry> entrySet = map.entrySet(); for(Map.Entry e : entrySet) { System.out.println(e.getKey()+"——>"+e.getValue()); } } }
四、Enumeration 和 Iterator 接口
Enumeration的效率比Iterator高,也使用更少的内存。Enumeration是非常基础的,也满足了基本需求。与Enumeration相比,Iterator更加安全,因为当一个集合中正在遍历的时候,它会阻止其它线程去修改集合。Iterator取代了Enumeration,允许调用者在迭代过程中移除元素,而Enumeration不能做到,为了使它的功能更加清晰,迭代器方法名已经经过改善。
五、Arrays类(数组工具类)
1.void sort(int[] a):对数组进行排序
2.int binarySearch(int[] a,int value):二分查找排好序的int类型数组中某个元素出现的下标
3.boolean deepEquals(Object[] o1,Object[] o2):比较两个数组的深度是否相等
4.void fill(int[] a,int value):将int类型数组中所有元素替换成指定int类型元素
5.int[] copyOf(int [] original,int newLength):复制数组中的元素,以使副本数组具有指定的长度
六、Collections类(集合工具类)
1.void sort(List list,Comparator c):使用比较器对集合进行排序
2.void shuffle(List list):打乱集合中的元素
3.T max(Collection c):获取集合中最大的元素
4.T min(Collection c):获取集合中最小的元素
5.void copy(List source,List target):将一个集合中的元素全部替换成指定的元素
七、泛型(Generic)
JDk 1.5开始,Java允许定义和使用泛型类、泛型接口、泛型方法
一种把类型明确的工作空间推迟到创建对象或者调用方法时明确,参数化类型的能力的特殊类型。
使用泛型的主要优点是能够在编译时而不是在运行时检测出错误
使用泛型来提高软件的可靠性和可读性
1.泛型类
public class GenericTest1 {public static void main(String[] args) {GenericClass<Student> gc = new GenericClass<Student>();gc.setObj(new Student("刘亦菲",20));System.out.println(gc.getObj());}}//泛型类class GenericClass<T> {private T obj;public T getObj() {return obj;}public void setObj(T obj) {this.obj = obj;}}class Student {private String name;private int age;public Student() {}public Student(String name,int age) {this.name = name;this.age = age;}public void setName(String name) {this.name = name;}public String getName() {return name;}public void setAge() {this.age = age;}public int getAge() {return age;}public String toString() {return "String [name="+name+",age="+age+"]";}}
public class GenericTest2 {public static void main(String[] args) {GenericInterface<String> gt = new Tool<String>();gt.show("123");}}//泛型接口interface GenericInterface<T> {public void show(T t);}class Tool<T> implements GenericInterface<T> {public void show(T t) {System.out.println(t);}}
import java.util.Collection;import java.util.ArrayList;public class GenericTest3 {public static void main(String[] args) {Collection<Object> c1 = new ArrayList<Object>();//?任意类型Collection<?> c2 = new ArrayList<Object>();//? extends E : 向下限定,E及其子类Collection<? extends Animal> c3 = new ArrayList<Animal>();Collection<? extends Animal> c4 = new ArrayList<Dog>();//?super Animal : 向上限定,E及其父类Collection<? super Animal> c5 = new ArrayList<Animal>();Collection<? super Animal> c6 = new ArrayList<Object>();}}class Animal {}class Dog extends Animal {}class Cat extends Animal {}
- 集合
- 集合
- 集合
- 集合
- 集合
- 集合
- 集合
- 集合
- 集合
- 集合
- 集合
- 集合
- 集合
- 集合
- 集合
- 集合
- 集合
- 集合
- js 设备检测(判断是移动端还是PC端)
- regmap机制
- maven中pom.xml文件的继承关系
- React.JS
- 织梦pc端+手机端 搜索功能实现
- 集合
- 关于JQuery动态添加的html会失去动态效果——(消灭bug①)
- 简单发货收货表格
- java实现读取文件夹下的文件并进行排序
- ORACLE使用“alter system register;”解决动态监听注册缓慢问题
- redis持久化
- 音乐播放器
- axure 8.1 lisence
- 一些特殊符号&%$等等,如何写在xml,TextView中