java中集合总结(二)
2015-05-28 17:10
239 查看
java中使用集合是的一些技巧:
看到Array就是数组结构,有角标,查询速度很快。
看到link就是链表结构:增删速度快,而且有特有方法。addFirst; addLast;removeFirst(); removeLast();getFirst();getLast();
看到hash就是哈希表,就要想要哈希值,就要想到唯一性,就要想到存入到该结构的中的元素必须覆盖hashCode,equals方法。
看到tree就是二叉树,就要想到排序,就想要用到比较。
比较的两种方式:
一个是继承Comparable接口:覆盖compareTo方法;
一个是使用比较器Comparator:覆盖compare方法。
TreeSet:可以对Set集合中的元素进行排序。底层数据结构是二叉树。保证元素唯一性的依据:compareTo方法return 0.
TreeSet排序的第一种方式:让元素自身具备比较性。元素需要实现Comparable接口,覆盖compareTo方法。也种方式也成为元素的自然顺序,或者叫做默认顺序。
TreeSet的第二种排序方式。当元素自身不具备比较性时,或者具备的比较性不是所需要的。这时就需要让集合自身具备比较性。在集合初始化时,就有了比较方式。
LinkedHashSet,LinkedHashMap:这两个集合可以保证哈希表有存入顺序和取出顺序一致,保证哈希表有序。
看到Array就是数组结构,有角标,查询速度很快。
看到link就是链表结构:增删速度快,而且有特有方法。addFirst; addLast;removeFirst(); removeLast();getFirst();getLast();
看到hash就是哈希表,就要想要哈希值,就要想到唯一性,就要想到存入到该结构的中的元素必须覆盖hashCode,equals方法。
看到tree就是二叉树,就要想到排序,就想要用到比较。
比较的两种方式:
一个是继承Comparable接口:覆盖compareTo方法;
一个是使用比较器Comparator:覆盖compare方法。
TreeSet:可以对Set集合中的元素进行排序。底层数据结构是二叉树。保证元素唯一性的依据:compareTo方法return 0.
TreeSet排序的第一种方式:让元素自身具备比较性。元素需要实现Comparable接口,覆盖compareTo方法。也种方式也成为元素的自然顺序,或者叫做默认顺序。
class Student implements Comparable//该接口强制让学生具备比较性。 { private String name; private int age; Student(String name,int age) { this.name = name; this.age = age; } public int compareTo(Object obj) { if(!(obj instanceof Student)) throw new RuntimeException("不是学生对象"); Student s = (Student)obj; if(this.age>s.age) return 1; if(this.age==s.age) { return this.name.compareTo(s.name); } return -1; } public String getName() { return name; } public int getAge() { return age; } } class TreeSet { public static void main(String[] args) { TreeSet ts = new TreeSet(); ts.add(new Student("lisi02",22)); ts.add(new Student("lisi02",21)); ts.add(new Student("lisi007",20)); ts.add(new Student("lisi09",19)); ts.add(new Student("lisi06",18)); ts.add(new Student("lisi06",18)); Iterator it = ts.iterator(); while(it.hasNext()) { Student stu = (Student)it.next(); System.out.println(stu.getName()+"..."+stu.getAge()); } } }
TreeSet的第二种排序方式。当元素自身不具备比较性时,或者具备的比较性不是所需要的。这时就需要让集合自身具备比较性。在集合初始化时,就有了比较方式。
class Student { private String name; private int age; Student(String name,int age) { this.name = name; this.age = age; } public String getName() <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>return name; <span style="white-space:pre"> </span>} <span style="white-space:pre"> </span>public int getAge() <span style="white-space:pre"> </span>{ <span style="white-space:pre"> </span>return age; <span style="white-space:pre"> </span>} } class TreeSet { public static void main(String[] args) { TreeSet ts = new TreeSet(new <span style="font-family: Arial, Helvetica, sans-serif;">MyCompare()</span>); ts.add(new Student("lisi02",22)); ts.add(new Student("lisi02",21)); ts.add(new Student("lisi007",20)); ts.add(new Student("lisi09",19)); ts.add(new Student("lisi06",18)); ts.add(new Student("lisi06",18)); Iterator it = ts.iterator(); while(it.hasNext()) { Student stu = (Student)it.next(); System.out.println(stu.getName()+"..."+stu.getAge()); } } } class MyCompare implements Comparator //定义比较器 { public int compare(Object o1,Object o2) { Student s1 = (Student)o1; Student s2 = (Student)o2; int num = s1.getName().compareTo(s2.getName()); if(num==0) { return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge())); } return num; } }
LinkedHashSet,LinkedHashMap:这两个集合可以保证哈希表有存入顺序和取出顺序一致,保证哈希表有序。
相关文章推荐
- Java集合与框架总结与学习
- java集合总结
- java常用类与集合框架相关知识总结
- Java 中集合框架知识点的总结
- Java 集合系列08之 List总结(LinkedList, ArrayList等使用场景和性能分析)
- java set集合总结
- Java 集合系列08之 List总结(LinkedList, ArrayList等使用场景和性能分析)
- 3.Java集合总结系列:Set接口及其实现
- Java 集合系列08之 List总结(LinkedList, ArrayList等使用场景和性能分析)
- java_集合体系之List体系总结、应用场景——07
- 关于Java集合框架的总结
- Java集合定义与用法实例总结【Set、List与Map】
- paip.数组以及集合的操作uapi java php python总结..
- java基础之集合总结
- java基础总结六(自定义数据类型、ArrayList集合)
- java集合08--List总结
- 黑马程序员---Java基础---集合总结
- java集合系列——List集合总结(六)
- Java知识总结-集合
- 黑马程序员_Java_集合的基本用法总结