Java集合总结(一)
2015-07-03 10:38
387 查看
1.集合:用于存储对象的容器,长度可变,不可以存储基本数据类型。
2.Collection,顶层集合,实现了Iterable接口。代码示例;
3.Iterator变量放在for循环之中比放在while循环里面更加高效,因为for循环结束就会释放Iterator变量占有的内存。
4.
|–Collection
|–List:元素有序,可重复,List集合具有索引角标
|–ArrayList:底层使用数组结构,查询速度快,增删慢。线程不同步
|–LinkedList:底层使用链表,增删速度快,查询很慢。
|–Vector:底层使用数组,线程同步。已被ArrayList取代。
|–Set:元素无序,不可重复(存入的顺序与取出的顺序不同)。
|–HashSet:底层使用hash表,哈希表。
|–HashSet如何在添加对象时保证对象的唯一性?
通过元素的两个方法,hashCode()和equals()实现。
如果hashCode值相同,调用equals判断是否为true。
如果hashCode值不同,则为不同对象。
对于判断元素是否存在,以及删除操作,依次调用hashCode与equals方法判断。
|–TreeSet:底层使用二叉树,存储按照自然顺序,即ASCLL码存储。
|–当添加的对象本身没有实现Comparable接口的ComparaTo()方法,或者实现的方法不具有比较性,或者不是需要的比较性时这是需要让容器本身具有比较性,定义了比较器,调用构造方法,TreeSet(Comparator(Object o1,Object o2))来实现此处的Comparator(Object o1,Object o2)为实现Comparator接口的比较器对象,需要自己写实现,并用这个对象构造TreeSet对象。比较器优先级高于类本身的比较方法。
5.
|–ListIterator与Iterator的区别:并发性的考虑
|–ListIterator有add()方法,可以向当前选中的迭代数据添加新数据,添加到该数据之后。
|–ListIterator可以正向,逆向遍历。
|–ListIterator可以定位当前的索引位置,nextIndex(),previousIndex()。
|–ListIterator可以修改对象,set()。ListIterator和Iterator皆能remove()。
|–由于在集合中只是存储了对象的地址,而并没有存储对象的具体值,所以在调用set()方法时,并不会直接改变该对应对象的值,而是替换对应的地址。
6.|–Comparable:所有实现了此接口的类都对加入的新对新进行排序,需要复写CompareTo()
|–String类的CompareTo()方法默认实现升序排序
|–CompareTo()返回值为0 保证元素唯一性
7.List方法实现:
8.在迭代器Iterator中不要使用集合操作元素,容易出现java.util.ConcurrentModificationException。可以使用ListIterator来完成对元素的更多操作。
9.ListIterator操作集合:
10.LinkedList方法:
11.LinkedList模仿队列:
12.Set集合:
13.定义功能去除ArrayList中的重复元素:
14.无序变有序,使用LinkedHashSet。
2.Collection,顶层集合,实现了Iterable接口。代码示例;
Collection collectionOne = new ArrayList(); Collection collectionTwo = new ArrayList(); // 添加 System.out.println("add....."); collectionOne.add("s"); collectionOne.add(2); collectionOne.add(3.5); System.out.println(collectionOne); collectionTwo.addAll(collectionOne); collectionTwo.add(88); System.out.println(collectionTwo); // 删除 System.out.println("remove....."); collectionOne.remove(3.5); System.out.println(collectionOne); collectionOne.add("rt"); collectionTwo.removeAll(collectionOne); System.out.println(collectionOne + "\n" + collectionTwo); // 判断 System.out.println("contains....."); System.out.println(collectionOne.contains("jio")); collectionTwo.add("s"); collectionTwo.add("rt"); collectionTwo.add(2); System.out.println(collectionOne + "\t" + collectionTwo); System.out.println(collectionTwo.containsAll( collectionOne)); // 获取 System.out.println("get....."); System.out.println(collectionOne.size() + "\t" + collectionTwo.size()); Iterator it = collectionOne.iterator(); while (it.hasNext()) { System.out.print(it.next() + "\t"); } System.out.println(); // 其它 // 转变为数组 Object[] objArr = collectionTwo.toArray(); for (int j = 0; j < objArr.length; j++) { System.out.print(objArr[j] + "\t"); } System.out.println(); // 交集判断 去除collectionTwo中包含的非collectionOne中的元素 // 不更改collectionTwo的元素顺序 System.out.println( collectionTwo.retainAll(collectionOne)); System.out.println(collectionOne + "\t" + collectionTwo); Collection newCollection = new ArrayList(); newCollection.add("a"); newCollection.add("s"); for(Iterator newIt = newCollection.iterator(); newIt.hasNext();){ System.out.print(newIt.next()+"\t"); } System.out.println("\n"+newCollection); newCollection.clear(); System.out.println(newCollection);
3.Iterator变量放在for循环之中比放在while循环里面更加高效,因为for循环结束就会释放Iterator变量占有的内存。
4.
|–Collection
|–List:元素有序,可重复,List集合具有索引角标
|–ArrayList:底层使用数组结构,查询速度快,增删慢。线程不同步
|–LinkedList:底层使用链表,增删速度快,查询很慢。
|–Vector:底层使用数组,线程同步。已被ArrayList取代。
|–Set:元素无序,不可重复(存入的顺序与取出的顺序不同)。
|–HashSet:底层使用hash表,哈希表。
|–HashSet如何在添加对象时保证对象的唯一性?
通过元素的两个方法,hashCode()和equals()实现。
如果hashCode值相同,调用equals判断是否为true。
如果hashCode值不同,则为不同对象。
对于判断元素是否存在,以及删除操作,依次调用hashCode与equals方法判断。
|–TreeSet:底层使用二叉树,存储按照自然顺序,即ASCLL码存储。
|–当添加的对象本身没有实现Comparable接口的ComparaTo()方法,或者实现的方法不具有比较性,或者不是需要的比较性时这是需要让容器本身具有比较性,定义了比较器,调用构造方法,TreeSet(Comparator(Object o1,Object o2))来实现此处的Comparator(Object o1,Object o2)为实现Comparator接口的比较器对象,需要自己写实现,并用这个对象构造TreeSet对象。比较器优先级高于类本身的比较方法。
5.
|–ListIterator与Iterator的区别:并发性的考虑
|–ListIterator有add()方法,可以向当前选中的迭代数据添加新数据,添加到该数据之后。
|–ListIterator可以正向,逆向遍历。
|–ListIterator可以定位当前的索引位置,nextIndex(),previousIndex()。
|–ListIterator可以修改对象,set()。ListIterator和Iterator皆能remove()。
|–由于在集合中只是存储了对象的地址,而并没有存储对象的具体值,所以在调用set()方法时,并不会直接改变该对应对象的值,而是替换对应的地址。
6.|–Comparable:所有实现了此接口的类都对加入的新对新进行排序,需要复写CompareTo()
|–String类的CompareTo()方法默认实现升序排序
|–CompareTo()返回值为0 保证元素唯一性
7.List方法实现:
List<Integer> list = new LinkedList<Integer>(); //添加 list.add(1); list.add(2); list.add(3); list.add(4); list.add(2); //从指定位置插入 list.add(1, 9); System.out.println(list); //删除 list.remove(1); list.remove(new Integer(1)); //list.clear(); //list.removeAll(list); System.out.println(list); //修改 list.set(1, 88); System.out.println(list); //查找 System.out.println(list.get(1)); System.out.println( list.indexOf(1)+"\t"+list.indexOf(new Integer(2))); System.out.println(list.lastIndexOf(2)); System.out.println(list); //截取一部分List作为新的List List<Integer> newList = list.subList(0, 3); System.out.println(newList);
8.在迭代器Iterator中不要使用集合操作元素,容易出现java.util.ConcurrentModificationException。可以使用ListIterator来完成对元素的更多操作。
9.ListIterator操作集合:
List<String> list = new LinkedList<String>(); for (int i = 0; i < 10; i++) { list.add(new String(""+i)); } System.out.println(list); ListIterator<String> it; int num = 0; for(it = list.listIterator();it.hasNext();){ String s =it.next(); //删除当前指向的那个值 if(s.equals("1")){ it.remove(); num--; System.out.println(list); } if(num == 8) System.out.println("888888888888\t"+it.nextIndex()); //添加到当前指向值的下一位 if(s.equals("9")){ System.out.println(list.size()+"\t"+list.get(8)); it.add("10"); System.out.println(list); System.out.println( "num="+num+"\t" +"99999999999999999999"+"\t" +list.size()+"\t"+it.nextIndex()); } //中间跳过了9这个索引 是由于添加的缘故 if(it.nextIndex() == 9) System.out.println("------"+list.size()+"\t"+num); num++; } System.out.println(list);
10.LinkedList方法:
LinkedList<Integer> ld = new LinkedList<Integer>(); for (int i = 0; i < 10; i++) { ld.add(i); } ld.add(8888); //添加 ld.addFirst(-1); ld.addLast(10); System.out.println(ld); ld.offer(11); ld.offerFirst(-2); ld.offerLast(12); System.out.println(ld); //获取 System.out.println(ld.getFirst()+"----"+ld.peekFirst()); System.out.println(ld.getLast()+"----"+ld.peekLast()); System.out.println(ld.get(1)+"\t"+ld.peek()); //删除 //ld.remove(); //ld.remove(1); //ld.removeLast(); ld.poll(); ld.pollFirst(); ld.pollLast(); System.out.println(ld);
11.LinkedList模仿队列:
public static LinkedList<Integer> ld = new LinkedList<Integer>(); public static void addNum(Integer num){ ld.addLast(num); } public static Integer getNum(){ return ld.pollFirst(); } public static boolean isNull(){ return ld.isEmpty(); } public static void main(String[] args) { //LinkedList模仿队列 addNum(1); addNum(2); addNum(3); addNum(4); System.out.println(ld); while(!isNull()) System.out.println(getNum()); System.out.println(ld); }
12.Set集合:
class HashDemo { private String name; private int ID; public HashDemo(String name, int ID) { this.name = name; this.ID = ID; } @Override public int hashCode() { return this.name.hashCode() + this.ID; } @Override public boolean equals(Object obj) { if(obj == this) return true; if (!(obj instanceof HashDemo)) throw new ClassCastException("类型错误"); HashDemo objDemo = (HashDemo) obj; return this.name.equals(objDemo.name) && this.ID == objDemo.ID; } @Override public String toString() { return this.name + "\t" + ID + "\t" + + this.getClass().getName(); } } public class Demo { public static void main(String[] args) { HashDemo h1 = new HashDemo("caizhanqi", 2012241001); HashDemo h2 = new HashDemo("chenweirong", 2012241002); HashDemo h3 = new HashDemo("chenzheng", 2012241003); HashDemo h4 = new HashDemo("cuijunbiao", 2012241005); Set<HashDemo> set = new HashSet<HashDemo>(); set.add(h1); set.add(h2); set.add(h3); set.add(h4); Iterator<HashDemo> it; for (it = set.iterator(); it.hasNext();) System.out.println(it.next()); } }
13.定义功能去除ArrayList中的重复元素:
HashDemo h1 = new HashDemo("caizhanqi", 2012241001); HashDemo h2 = new HashDemo("chenweirong", 2012241002); HashDemo h3 = new HashDemo("chenzheng", 2012241003); HashDemo h4 = new HashDemo("cuijunbiao", 2012241005); List<HashDemo> list = new ArrayList<HashDemo>(); list.add(h1); list.add(h1); list.add(h1); list.add(h2); list.add(h2); list.add(h2); list.add(h3); list.add(h3); list.add(h3); list.add(h4); List<HashDemo> newList = new ArrayList<HashDemo>(); ListIterator<HashDemo> it; for (it = list.listIterator(); it.hasNext();){ HashDemo obj = it.next(); if(!newList.contains(obj)) newList.add(obj); } for(it = newList.listIterator();it.hasNext();) System.out.println(it.next());
14.无序变有序,使用LinkedHashSet。
相关文章推荐
- Struts开发所遇问题之Unknown tag (s:form).
- java堆栈区别(个人理解)
- 配置SpringMVC返回JSON遇到的坑
- 错误解决:There is no getter for property named 'id' in class 'java.lang.String'
- 《Java程序实习》日记(周一)
- Java中final关键字,抽象类与接口
- spring事务管理几种方式
- eclipse转android studio必看。
- Java学习笔记十(注解)
- Spring之AOP实现面向切面编程
- xcode和eclipse常用快捷键
- java这些东西发展(4)-------无穷time of error
- JAVA card 应用程序开发(七) JAVA 卡数据(永久数据/)时间数据
- Eclipse调试Java的10个技巧
- Spring基础
- 正则表达式的汉字匹配
- myEclipse新建工作空间设置
- 《Java程序实习》日记(周二)
- MyEclipse Servers视窗出现“Could not create the view: An unexpected exception was thrown”
- java从数据库读取信息执行文件拷贝