Collections
package Collection; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Hashtable; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Queue; import java.util.Random; import java.util.Set; import java.util.Stack; import java.util.TreeMap; import java.util.TreeSet; /* * |Collection | ├List | │-├LinkedList | │-├ArrayList | │-└Vector | │ └Stack | ├Set | │├HashSet | │├TreeSet | │└LinkedSet | |Map ├Hashtable ├HashMap └WeakHashMap */ public class App { public static void main(String[] args) throws Exception { // TODO Auto-generated method stub // test1(); // test2(); test13(); } /** * 遍历 * @author SiyyaWu * @param collection */ public static void traversal(Collection<? extends Object> collection){ for(Object i:collection){ System.out.println(i); } } interface Callback{ void execute(); } public static void testTime(Callback callBack){ long begin = System.nanoTime();//测试起始时间 callBack.execute();//进行回调操作 long end = System.nanoTime();//测试结束时间 System.out.println("[use time]:" + (end - begin));//打印使用时间 } /** * ArrayList * @author SiyyaWu */ public static void test1() throws Exception{ //随机 // List<Integer> list=new ArrayList<>(); // for(int i=0;i<10;i++){ // list.add(new Random().nextInt(100)); // } //排序 // Collections.sort(list); // traversal(list); List<Integer> list2=new ArrayList<>(); for(int i=0;i<10;i++){ list2.add(i); } //查找 // System.out.println("查找第2个元素"+list2.get(2)); // System.out.println("查找第一个等于2的元素的位置"+list2.indexOf(2)); //初始化(三种初始化方式) // List<Integer> list3=new ArrayList<>(list2); // List<Integer> list4=new ArrayList<>(20); // List<Integer> list5=new ArrayList<>(); // traversal(list3); //截取 // List<Integer> list6=new ArrayList<>(list2); // List<Integer> listtemp=list6.subList(1, 3); // traversal(listtemp); //查找 // List<Integer> list7=list2.subList(3, 5); // System.out.println(list2.contains(6)); // System.out.println(list2.containsAll(list7)); //测试插入时间 // List<Integer> listTestTime=new ArrayList<>(); // for(int i=0;i<1000000;i++){ // listTestTime.add(new Random().nextInt(10000000)); // } // testTime(new Callback() { // // @Override // public void execute() { // // TODO Auto-generated method stub // for(int i=0;i<1000;i++){ // listTestTime.add(new Random().nextInt(100), new Random().nextInt(1000000)); // } // } // // }); List<Pet> listtemp=new ArrayList<Pet>(); listtemp.add(new Pet(1, "admin")); listtemp.add(new Pet(2, "b")); listtemp.add(new Pet(3, "c")); listtemp.add(new Pet(4, "d")); Collections.sort(listtemp); traversal(listtemp); } /** * LinkedList * @author SiyyaWu */ public static void test2(){ //测试插入时间 // List<Integer> list=new LinkedList<>(); // for(int i=0;i<1000000;i++){ // list.add(i); // } // testTime(new Callback() { // // @Override // public void execute() { // // TODO Auto-generated method stub // for(int i=0;i<1000;i++){ // list.add(new Random().nextInt(100), new Random().nextInt(1000000)); // } // } // // }); List<Integer> list2=new LinkedList<>(); for(int i=0;i<10;i++){ list2.add(i); } Iterator<Integer> it=list2.iterator(); while(it.hasNext()){ System.out.println(it.next()); } } /* * LinkedList模拟栈 */ public static void test3(){ List<Integer> list=new LinkedList<>(); for(int i=0;i<10;i++){ list.add(i); } ((LinkedList<Integer>)list).addFirst(90); ((LinkedList<Integer>)list).addLast(91); System.out.println(((LinkedList<Integer>)list).removeFirst()); System.out.println(((LinkedList<Integer>)list).removeLast()); System.out.println(((LinkedList<Integer>)list).getFirst()); System.out.println(((LinkedList<Integer>)list).getLast()); for(Integer i:list){ System.out.println(i); } } /** * Stack * @author SiyyaWu */ public static void test4(){ //peek pop区别 Stack<Integer> stack=new Stack<>(); stack.push(1); stack.push(2); stack.push(3); stack.push(4); stack.push(5); stack.push(6); stack.push(7); traversal(stack); System.out.println(stack.peek()); traversal(stack); } /** * Queue * @author SiyyaWu */ public static void test5() throws Exception{ //peek poll 的区别 Queue<Integer> queue=new LinkedList<>(); queue.add(1); queue.add(2); queue.add(3); queue.add(4); queue.add(5); queue.add(6); queue.add(7); traversal(queue); System.out.println("----"); System.out.println(queue.peek()); System.out.println("----"); traversal(queue); } /** * HashSet * @author SiyyaWu */ public static void test6(){ //测试HashSet // Set<Integer> set=new HashSet<>(); // set.add(1); // set.add(2); // set.add(3); // set.add(4); // set.add(4); // traversal(set); // set.remove(2); // traversal(set); //测试equals和hashCode Set<Pet> set2=new HashSet<>(); Pet pet1=new Pet(1, "A"); Pet pet2=new Pet(1, "A"); // System.out.println(pet1.hashCode()); // System.out.println(pet2.hashCode()); //==等于Object的equals方法,调用HashCode // System.out.println(pet1==pet2); //重写了equals方法,令pet1等于pet2 // System.out.println(pet1.equals(pet2)); //添加到HashSet中,仍然插入两条记录.当重写HashCode方法后,只插入一条数据 set2.add(pet1); set2.add(pet2); traversal(set2); } /** * treeSets * @author SiyyaWu */ public static void test7(){ Set<Integer> set=new TreeSet<>(); for(int i=0;i<10;i++){ set.add(new Random().nextInt(100)); } Iterator<Integer> it=set.iterator(); while(it.hasNext()){ System.out.println(it.next()); } } /** * HashMap遍历方式一 * @author SiyyaWu */ public static void test8(){ Map<String, Integer> map=new HashMap<String,Integer>(); map.put("admin", 1); map.put("b", 2); for(Entry<String, Integer> entry:map.entrySet()){ System.out.println(entry.getKey()+":"+entry.getValue()); } } /** * HashMap遍历方式二 * @author SiyyaWu */ public static void test9(){ Map<String, Integer> map=new HashMap<String,Integer>(); map.put("admin", 1); map.put("b", 2); Iterator<Map.Entry<String, Integer>> iterator=map.entrySet().iterator(); while(iterator.hasNext()){ Map.Entry<String, Integer> entry=iterator.next(); System.out.println(entry.getKey()+":"+entry.getValue()); } } /** * HashMap的遍历方式三 * @author SiyyaWu */ public static void test10(){ Map<String, Integer> map=new HashMap<String,Integer>(); map.put("admin", 1); map.put("b", 2); for(String key:map.keySet()){ System.out.println(key+":"+map.get(key)); } } /** * HashMap的遍历方式四 * @author SiyyaWu */ public static void test11(){ Map<String, Integer> map=new HashMap<String,Integer>(); map.put("admin", 1); map.put("b", 2); Set<Entry<String, Integer>> entrySet=map.entrySet(); for(Entry<String, Integer> entry:entrySet){ System.out.println(entry.getKey()+":"+entry.getValue()); } } /** * HashMap的key不能相同,则要重写Key的hashcode方法 * @author SiyyaWu */ public static void test12(){ Map<Pet,Integer> map=new HashMap<Pet,Integer>(); map.put(new Pet(1, "admin"), 100); map.put(new Pet(1, "admin"), 200); for(Entry<Pet, Integer> entry:map.entrySet()){ System.out.println(entry.getKey()+":"+entry.getValue()); } } /** * TreeMap * @author SiyyaWu */ public static void test13(){ Map<String, Integer> treeMap=new TreeMap<>(); treeMap.put("m", 1); treeMap.put("r", 2); treeMap.put("c", 20); for(Entry<String, Integer> entry:treeMap.entrySet()){ System.out.println(entry.getKey()+":"+entry.getValue()); } } /** * HashTable 不允许存储null的key和value * @author SiyyaWu */ public static void test14(){ Map<String,Integer> map=new Hashtable<>(); map.put("String", null); } } class Pet implements Comparable<Pet>{ private int id; private String name; public Pet(int id, String name) { this.id = id; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public boolean equals(Object o) { // TODO Auto-generated method stub if(o instanceof Pet){ Pet pet=(Pet)o; return this.id==pet.getId(); } return false; } @Override public int compareTo(Pet o) { // TODO Auto-generated method stub if(this.id>o.id){ return 1; }else if (this.id==o.id) { return 0; } else{ return -1; } } @Override public String toString() { return "Pet [id=" + id + ", name=" + name + "]"; } @Override public int hashCode() { // TODO Auto-generated method stub return name.toUpperCase().hashCode()^id; } }
作者:人类一思考上帝就发笑
来源链接:https://www.cnblogs.com/siyyawu/p/11001216.html
版权声明:
1、JavaClub(https://www.javaclub.cn)以学习交流为目的,由作者投稿、网友推荐和小编整理收藏优秀的IT技术及相关内容,包括但不限于文字、图片、音频、视频、软件、程序等,其均来自互联网,本站不享有版权,版权归原作者所有。
2、本站提供的内容仅用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人及本网站的合法权利。
3、本网站内容原作者如不愿意在本网站刊登内容,请及时通知本站(javaclubcn@163.com),我们将第一时间核实后及时予以删除。