Java ——集合框架 list lambda set map 遍历方法 数据结构
本节重点思维导图
有序无序:元素放入的顺序与取出的顺序是否一致,一致即为有序,不一致即无序。
- List:允许重复、有序
- ArrayList:长度可变的数组,遍历速度快
- LinkedList:链表:添加删除节点速度快
import java.util.ArrayList; import java.util.Date; import java.util.List; public class Demo { public static void main(String[] args) { List list = new ArrayList(); System.out.println("--------"+list.toString());//add list.add("aa"); list.add(1234); list.add(true); list.add(new Date()); list.add(1234); list.add(3.14); System.out.println(list.get(0) + " -- " + list.get(3)); System.out.println(list.size()); System.out.println(list); System.out.println(list.toString()); for (Object obj : list) { System.out.println(obj); } list.remove(0);// 根据下标移除 list.clear(); System.out.println(list.size()); } }
结果:
--------[] aa -- Fri May 31 15:07:45 CST 2019 6 [aa, 1234, true, Fri May 31 15:07:45 CST 2019, 1234, 3.14] [aa, 1234, true, Fri May 31 15:07:45 CST 2019, 1234, 3.14] aa 1234 true Fri May 31 15:07:45 CST 2019 1234 3.14 0
泛型
import java.util.ArrayList; import java.util.Date; import java.util.Iterator; import java.util.List; public class Demo { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("1234"); list.add("true"); list.add("aaaa"); list.add("aaaa"); list.add("bbbb"); list.add("dddd"); String e3 = list.get(3); System.out.println(e3); System.out.println(list.get(3)); int size = list.size(); System.out.println(size); for (String item : list) { System.out.println(item); } System.out.println("--------------------------"); // Iterator迭代器 Iterator<String> iterator = list.iterator(); while (iterator.hasNext()) {// 判断是否还有下一个元素,有的话返回true String next = iterator.next();// 取出元素,同时将指针指向当前位置 System.out.println(next); } } }
结果:
aaaa aaaa 6 1234 true aaaa aaaa bbbb dddd -------------------------- 1234 true aaaa aaaa bbbb dddd
list\Lambda\set\map
*lambda
public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("zhangsan"); list.add("lisi"); list.add("zhangsan"); list.add("wanger"); list.add("mazi"); list.add("wanger"); list.stream().forEach(element->System.out.println(element)); System.out.println(); list.forEach(item -> System.out.println(item)); System.out.println(); list.forEach(System.out::println); //将List中所有的元素变成大写字母后再逆序输出 System.out.println(); for(int i =list.size()-1;i>=0;i--) { String item = list.get(i); item = item.toUpperCase(); System.out.println(item); } System.out.println(); list.stream().map(item->item.toUpperCase())//映射:依次拿出List中的每一个元素进行映射 .forEach(System.out::println); //输出List中每一个元素的前两个字符 System.out.println(); list.stream().map(item->item.substring(0, 2)) .forEach(System.out::println); //只输出List中长度大于4的元素 System.out.println(); list.stream().filter(item->item.length()>4) .forEach(System.out::println); //去除重复 System.out.println(); list.stream().distinct().forEach(System.out::println); //将Stream转换成List System.out.println(); List<String> collect = list.stream().collect(Collectors.toList()); System.out.println(collect); }
*set
public static void main(String[] args) { Set<String> set = new HashSet<>(); set.add("zhangsan"); set.add("lis"); set.add("zhangsan"); set.add("wanger"); set.add("mazi"); set.add("wanger"); for(String item : set) { System.out.println(item); } System.out.println(); Iterator<String> iterator = set.iterator(); while(iterator.hasNext()) { System.out.println(iterator.next()); } System.out.println(); set.forEach(System.out::println); }
示例:学习set判断元素是否相等
public class Dept { private int deptno; private String dname; private String loc; public Dept() { super(); } public Dept(int deptno, String dname, String loc) { super(); this.deptno = deptno; this.dname = dname; this.loc = loc; } public int getDeptno() { return deptno; } public void setDeptno(int deptno) { this.deptno = deptno; } public String getDname() { return dname; } public void setDname(String dname) { this.dname = dname; } public String getLoc() { return loc; } public void setLoc(String loc) { this.loc = loc; } @Override public int hashCode() { return 234; } @Override public boolean equals(Object obj) { return true; } @Override public String toString() { return "Dept [deptno=" + deptno + ", dname=" + dname + ", loc=" + loc + "]"; } } public static void main(String[] args) { Set<Dept> set = new HashSet<>(); set.add(new Dept()); set.add(new Dept()); set.add(new Dept(10,"sales","new york")); set.add(new Dept(20,"research","chicago")); System.out.println(set.size()); set.forEach(System.out::println); }
*map
键值对
public static void main(String[] args) { Map<String, Integer> data = new HashMap<>(); data.put("a", 1); data.put("b", 2); data.put("c", 3); data.put("d", 4); System.out.println(data.get("a")); Set<String> keySet = data.keySet(); keySet.forEach(System.out::println); Collection<Integer> values = data.values(); values.forEach(item->System.out.print(item+" ")); System.out.println(); Set<Entry<String, Integer>> entrySet = data.entrySet(); for(Entry<String, Integer> item: entrySet) { System.out.println(item.getKey()+" "+item.getValue()); } data.forEach((k,v)->System.out.println(k+" : "+v));; }
遍历方法
快捷键:
代码格式化:ctrl+sfhit+f
自动提示:alt+?
抽取变量:alt+shift+L
复习总结
Arrays
public static void main(String[] args) { String[] data = { "zhangsan", "lisi", "wanger", "mazi","qianwu","zhaoliu" }; List<String> asList = Arrays.asList(data); System.out.println(asList); System.out.println(data); System.out.println(Arrays.toString(data)); int binarySearch = Arrays.binarySearch(data, "lisi"); System.out.println(binarySearch); //对数组进行扩容 String[] copyOf = Arrays.copyOf(data, data.length + 4); for (String item : copyOf) System.out.println(item); String[] copyOfRange = Arrays.copyOfRange(data, 2, 4); System.out.println(Arrays.toString(copyOfRange)); String[] copyOf2 = Arrays.copyOf(data, data.length); boolean equals = Arrays.equals(data,copyOf2);//比较两个数组的内容是否相等 System.out.println(equals); boolean[] bol = new boolean[8]; System.out.println(Arrays.toString(bol)); Arrays.fill(bol,true); System.out.println(Arrays.toString(bol)); Arrays.sort(data); System.out.println(Arrays.toString(data)); System.out.println("--------------------"); //匿名内部类 Comparator<String> c = new Comparator<String>() { @Override public int compare(String o1, String o2) { return o2.length()-o1.length(); } }; Arrays.sort(data,c); System.out.println(Arrays.toString(data)); data = new String[]{ "zhangsan", "lisi", "wanger", "mazi","qianwu","zhaoliu" }; Arrays.sort(data,(o1,o2)->o2.length()-o1.length()); System.out.println(Arrays.toString(data)); }
collections
public static void main(String[] args) { List<String> list = new ArrayList<>(); Collections.addAll(list,"zhangsan", "lisi","zhangsan","wanger","zhangsan","mazi"); System.out.println(list); int binarySearch = Collections.binarySearch(list, "lisi3"); System.out.println(binarySearch); int binarySearch2 = Collections.binarySearch(list, "wanger", (o1,o2)->o1.length()-o2.length()); System.out.println(binarySearch2); Collections.sort(list, new Comparator<String>() { @Override public int compare(String o1, String o2) { return o1.length()-o2.length(); } }); System.out.println(list); Collections.shuffle(list); System.out.println(list); Collections.sort(list, (o1,o2)->o1.length()-o2.length());; System.out.println(list); Collections.replaceAll(list, "zhangsan", "***"); System.out.println(list); Collections.swap(list, 1, 3); System.out.println(list); Collections.reverse(list); System.out.println(list); }
list
public static void main(String[] args) { List<String> list = new ArrayList<>(); Collections.addAll(list,"zhangsan", "lisi","wanger","mazi"); for(int i =0;i<list.size();i++){ System.out.println(list.get(i)); } for(String item : list){ System.out.println(item); } Iterator<String> iterator = list.iterator(); while(iterator.hasNext()){ System.out.println(iterator.next()); } ListIterator<String> listIterator = list.listIterator(); while(listIterator.hasNext()){ System.out.println(listIterator.next()); } list.forEach(item->System.out.println(item)); list.forEach(System.out::println); }
set
public static void main(String[] args) { Set<String> set = new HashSet<>(); set.add("aa"); set.add("bb"); set.add("cc"); set.add("dd"); for(String item : set){ System.out.println(item); } Iterator<String> iterator = set.iterator(); while(iterator.hasNext()){ System.out.println(iterator.next()); } set.forEach(item->System.out.println(item)); set.forEach(System.out::println); }
map
原则:将map变为set
public static void main(String[] args) { Map<Integer,Goods> map = new HashMap<>(); map.put(111, new Goods(111,"aaa",1,"aaaaaaaaaaa")); map.put(222, new Goods(222,"bbb",2,"bbbbbbbbbbbbb")); map.put(333, new Goods(333,"ccc",3,"ccccccccccccc")); map.put(444, new Goods(444,"ddd",4,"dddddddddddd")); Set<Integer> keySet = map.keySet(); for(Integer key : keySet){ System.out.println(key+" : "+map.get(key)); } Set<Entry<Integer, Goods>> entrySet = map.entrySet(); for(Entry<Integer, Goods> item : entrySet){ System.out.println(item.getKey()+" "+item.getValue()); } map.forEach((k,v)->System.out.println(k+" :: "+v));; }
扩展:
public static void main(String[] args) { List<String> list = Arrays.asList("zhangsan","lisi","wanger","mazi"); Collections.sort(list, new Comparator<String>() { @Override public int compare(String o1, String o2) { return o1.length() - o2.length(); } }); System.out.println(list); System.out.println("-------------"); list.forEach(item->System.out.println(item+" "+item.hashCode())); list.sort((o1,o2)->{ return o1.hashCode()- o2.hashCode(); }); list.forEach(System.out::println); }