首页 > 代码库 > Java Day 19

Java Day 19

Map集合--LinkedHashMap
 有序 存取一致

技术分享
 1 package com.company.Day019; 2  3 import java.util.HashMap; 4 import java.util.Iterator; 5 import java.util.LinkedHashMap; 6 import java.util.Map; 7  8 /** 9  * Created by junius on 2016/10/15.10  */11 public class HashMap001 {12     public static void main(String[] args){13         HashMap<Integer,String> hm = new LinkedHashMap<Integer,String>();14         hm.put(7,"zhouqi");15         hm.put(5,"zhouqi3");16         hm.put(2,"zhouqi2");17 18         Iterator<Map.Entry<Integer,String>> it = hm.entrySet().iterator();19 20         while(it.hasNext()){21             Map.Entry<Integer,String> me = it.next();22             Integer key = me.getKey();23             String value =http://www.mamicode.com/ me.getValue();24 25             System.out.println(key+" : "+value);26         }27     }28 }
View Code


 
记录字母次数
 --002 TreeMap 有序
 1、将字符串中的字母变成字符数组
 2、遍历字符数组,用每一个字母作为键去查Map集合这个表
    如果该字母键不存在,就将该字母作为键1存储
    如果该字母键存在,就将该字母键对应值取出并+1,然后存储
 3、遍历Map集合

技术分享
 1 package com.company.Day019; 2  3 import java.util.Iterator; 4 import java.util.Map; 5 import java.util.TreeMap; 6  7 /** 8  * Created by junius on 2016/10/10. 9  * "fagavcbascdfs" 字母次数  映射关系10  * 1、字符串变成字符数组11  * 2、遍历字符数组,用每一个字母做键 查map表12  * 如果字母键不存在,就值为113  * 存在,则值+114  * 3、遍历15  */16 public class MainDemo002 {17     public static void main(String[] args){18         String str = "abcefgabcabcooi";19         String s = getCharCount(str);20         System.out.println(s);21     }22 23     public static String getCharCount(String str) {24         //字符串转换成字符数组25         char[] chs = str.toCharArray();26 27         //定义map集合28         Map<Character,Integer> map = new TreeMap<Character,Integer>();29 30         for (int i = 0; i <chs.length ; i++) {31             /*if(value=http://www.mamicode.com/=null)>32                 map.put(chs[i],1);33             else{34                 map.put(chs[i],value+1);35             }*/36             if(!(chs[i] >= ‘a‘ && chs[i] <= ‘z‘ || chs[i] >= ‘A‘ &&chs[i] <= ‘Z‘))37                 continue;38             int count = 0;39             Integer value =http://www.mamicode.com/ map.get(chs[i]);40 41             if(value != null){42                 count = value;43             }44             count++;45             map.put(chs[i],count);46         }47 48         return mapToString(map);49     }50 51     private static String mapToString(Map<Character, Integer> map) {52         StringBuilder sb = new StringBuilder();53         Iterator<Character> it = map.keySet().iterator();54         while(it.hasNext()){55             Character key = it.next();56             Integer value =http://www.mamicode.com/ map.get(key);57             sb.append(key+"("+value+")");58         }59         return sb.toString();60     }61 62 }
View Code

 

Map查表法
 如果有映射关系,优先考虑map集合

集合框架工具类
 Collections 静态方法
 

技术分享
 1 package com.company.Day019; 2  3 import java.util.*; 4  5 public class MapTest003 { 6     public static void main(String[] args){ 7  8         demo_2(); 9     }10 11     private static void demo_2() {12         List<String> list = new ArrayList<String>();13         list.add("zzzA");14         list.add("yyyYY");15         list.add("dd");16         list.add("aaa");17         System.out.println(list);18 //        Collections.sort(list);19 //        mySort_2(list,new comparatorByLength());20         Collections.sort(list,new comparatorByLength());21         System.out.println(list);22 23     }24 25     private static void demo_1() {26 27         List<String> list = new ArrayList<String>();28         list.add("zzz");29         list.add("yyy");30         list.add("ddd");31         list.add("aaa");32         System.out.println(list);33 //        Collections.sort(list);34         mySort(list);35         System.out.println(list);36 37     }38 39     public static <T extends Comparable<? super T>> void mySort(List<T> list){40         for (int i = 0; i <list.size()-1 ; i++) {41             for (int j = i+1; j <list.size(); j++) {42                 if(list.get(i).compareTo(list.get(j))>0){43 //                    String temp = (String)list.get(i);44 //                    list.set(i,list.get(j));45 //                    list.set(j,(T)temp);46                     Collections.swap(list,i,j);47                 }48             }49         }50     }51 52     public static <T> void mySort_2(List<T> list,Comparator<? super T> comp){53         for (int i = 0; i <list.size()-1 ; i++) {54             for (int j = i+1; j <list.size(); j++) {55                     if(comp.compare(list.get(i),list.get(j))>0) {56                         Collections.swap(list, i, j);57                     }58                 }59             }60     }61 }
View Code

 


 sort

 折半
  
 最值
 
 逆序 替换
 Collections.reverseorder 返回比较器
 Collections.reverseorder(比较器)
 reverse(List list)

 replaceAll(list oldValue,newValue) -> set(index,value)

 其他方法 非同步转换成同步
 fill()
 

技术分享
 1 package com.company.Day019; 2  3 import java.util.ArrayList; 4 import java.util.List; 5  6 /** 7  * Created by junius on 2016/10/15. 8  */ 9 public class Lock004 {10     public static void main(String[] args){11         List list = new ArrayList();//非同步12         list = MyCollections.synList(list);//同步list13     }14 }15 class MyCollections{16     public static List synList(List list){17         return new Mylist(list);18     }19 20     private class Mylist implements List{21         @Override22         public List subList(int fromIndex, int toIndex) {23             return null;24         }25 26         private List list;27         private final Object lock = new Object();28 29         public MyList(List list){30             this.list = list;31         }32 33         public boolean add(Object obj){34             synchronized(lock){35                 return list.add(obj);36             }37 38         }39 40         public boolean remove(Object obj){41             synchronized(lock) {42                 return list.remove(obj);43             }44         }45 46     }47 }
View Code


shuffle(list)
 使用随机源对列表进行置换,类似洗牌

 Arrays--操作容器的方法
   全是静态方法
   toString()
   asList() 将数组转成list集合
   转换前,对象和基本类型数据的区别

技术分享
 1 package com.company.Day019; 2  3 import java.util.Arrays; 4 import java.util.List; 5  6 /** 7  * Created by junius on 2016/10/15. 8  */ 9 public class Arrays005 {10     public static void main(String[] args){11 12         demo_2();13 //        System.out.println(myToString(arr));14         String[] str = {"abc","abb","abd"};15 16         //将数组转换成集合17 //      好处:可以使用集合的方法18 //      弊端:对于集合的增删方法不可用19         List<String> list = Arrays.asList(str);20         boolean b = list.contains("abc");21         System.out.println("list b = "+b);22     }23 24     private static void demo_2() {25         int[] arr = {2,3,4,5,7};26         List<int[]> list = Arrays.asList(arr);27         System.out.println(list);28     }29 30     private static String myToString(int[] a){31         if (a == null)32             return "null";33         int iMax = a.length - 1;34         if (iMax == -1)35             return "[]";36 37         StringBuilder b = new StringBuilder();38         b.append(‘[‘);39         for (int i = 0; ; i++) {40             b.append(a[i]);41             if (i == iMax)42                 return b.append(‘]‘).toString();43             b.append(", ");44         }45     }46 47 48 }
View Code

 

 Collection--toArray

 ForEach循环
 
 函数可变参数
 
 静态导入
  import static **

技术分享
 1 package com.company.Day019; 2  3 import java.util.*; 4  5 /** 6  * Created by junius on 2016/10/15. 7  * Collection 的 toArray方法 8  * 9  */10 public class toArray006 {11     public static void main(String[] args){12         List<String> list = new ArrayList<String>();13         list.add("abc1");14         list.add("abc4");15         list.add("abc3");16         String[] str = list.toArray(new String[list.size()]);17         //toArray方法需要传入 一个指定类型的数组18         //如果长度小于集合的size,该方法会创建一个同类型并和集合相同size的数组19         //如果长度大于集合的size,则数组中其他多余的位置存储null20         //所以建议长度指定为集合的size.21         //System.out.println(Arrays.toString(str));22 23         /*foreach语句24          *for(类型 变量 : Collection集合|数组)25          * 只用于遍历,不会对元素进行过多的操作26          *27          * 与普通for的区别28          * 1、普通for可以定义控制循环的增量和条件29          *    增强for是简化形式,必须要有遍历的目标30          */31         for (String str2 : str) {32             System.out.println(str2);33 34         }35 36         Map<Integer,String> map = new HashMap<Integer,String>();37         map.put(3,"lisi1");38         map.put(4,"lisi2");39         map.put(2,"lisi3");40         map.put(5,"lisi4");41 42         for(Integer key:map.keySet()){43             String value =http://www.mamicode.com/ map.get(key);44             System.out.println(key+" : "+value);45         }46 47         for(Map.Entry<Integer,String> me:map.entrySet()){48             Integer key = me.getKey();49             String value =http://www.mamicode.com/ me.getValue();50             System.out.println(key+" : "+value);51         }52 53         int sum = add(4,6);54 55          System.out.print(newAdd(2,1,2,3,4,5,6));56 57     }58 59     public static int add(int a,int b){60         return a+b;61     }62 63     /*64     * 注意事项 可变参数类型,必须定义在参数列表的结尾65     */66     public  static int newAdd(int... arr){67         int sum = 0;68         for (int i = 0; i <arr.length ; i++) {69             sum += arr[i];70         }71         return sum;72     }73 74 75 }
View Code

 


  

 

Java Day 19