java数据结构之各数据结构之间的转化和集合工具类Collections

一、Collection与数组之间的转化

1.Collection转数组

集合接口都有一个toArray的方法,因此所有Collection都可以转化为数组

  • toArray() :返回一个Object数组,需要强转
  • toArray(T[] a):转化为一个指定的数组对象
  1. List<Integer> numList = new ArrayList<Integer>(5);
  2. numList.add(12);
  3. numList.add(15);
  4. numList.add(18);
  5. //1.第一种:转化为一个对象数组
  6. numArr = (Integer[]) numList.toArray();
  7. System.out.println(numArr);
  8. //2.第二种:转化为一个指定的数组对象
  9. Integer[] numArr = new Integer[numList.size()];
  10. numList.toArray(numArr);
  11. print(numArr); //12,15,18
2.数组转List

Arrays.asList方法可以将数组转化为List,如果要转成其他格式,只能通过List再转

  1. String[] cityArr = new String[]{"北京", "上海", "广州", "深圳", "杭州"};
  2. List<String> cityList = Arrays.asList(cityArr);
  3. System.out.println(cityList);

二、Collection内部结构的相互转化

Collection各子类之间的转化(如List转Set等)

1.在实例化时转化

在Collection的子类中可以通过另外一个Collection对象来进行初始化

  • List转Set
  1. List<Integer> scoreList = new ArrayList<Integer>(2);
  2. scoreList.add(87);
  3. scoreList.add(78);
  4. Set<Integer> scoreSet = new HashSet<Integer>(scoreList);
  • Set转List
  1. Set<Integer> scoreSet = new HashSet<Integer>(2);
  2. scoreSet.add(87);
  3. scoreSet.add(78);
  4. List<Integer> scoreList = new ArrayList<Integer>(scoreSet);
2.通过addAll转化
  • List转Set
  1. List<Integer> numList = new ArrayList<Integer>(5);
  2. numList.add(12);
  3. numList.add(15);
  4. numList.add(18);
  5. Set<Integer> numSet = new HashSet<Integer>();
  6. numSet.addAll(numList);
  7. System.out.println(numSet); //[18, 12, 15]
  • Set转List
  1. Set<Integer> numSet = new HashSet<Integer>(5);
  2. numSet.add(12);
  3. numSet.add(15);
  4. numSet.add(18);
  5. List<Integer> numList = new ArrayList<Integer>();
  6. numList.addAll(numSet);
  7. System.out.println(numList); //[18, 12, 15]

三、Map内部子类之间的转化

1.通过实例化方法来转化
  • TreeMap转HashMap
  1. TreeMap<String, String> tree = new TreeMap<String, String>();
  2. tree.put("name", "名称");
  3. tree.put("age", "年龄");
  4. HashMap<String, String> hashMap = new HashMap<String, String>(tree);
  5. System.out.println(hashMap); //{name=名称, age=年龄}
  • HashMap转TreeMap
  1. HashMap<String, String> hashMap = new HashMap<String, String>();
  2. hashMap.put("name", "名称");
  3. hashMap.put("age", "年龄");
  4. TreeMap<String, String> treeMap = new TreeMap<String, String>(tree);
  5. System.out.println(treeMap); //{name=名称, age=年龄}
2.通过putAll方法来转化
  • TreeMap转HashMap
  1. TreeMap<String, String> tree = new TreeMap<String, String>();
  2. tree.put("name", "名称");
  3. tree.put("age", "年龄");
  4. HashMap<String, String> hashMap = new HashMap<String, String>();
  5. hashMap.putAll(tree);
  6. System.out.println(hashMap); //{name=名称, age=年龄}
  • HashMap转TreeMap
  1. HashMap<String, String> hashMap = new HashMap<String, String>();
  2. hashMap.put("name", "名称");
  3. hashMap.put("age", "年龄");
  4. TreeMap<String, String> treeMap = new TreeMap<String, String>();
  5. treeMap.putAll(hashMap);
  6. System.out.println(treeMap); //{name=名称, age=年龄}

四、集合工具类Collections

1.排序操作
(1)元素反转reverse
  1. List<Integer> numList = new ArrayList<Integer>(5);
  2. numList.add(7);
  3. numList.add(8);
  4. numList.add(6);
  5. numList.add(9);
  6. System.out.println(numList); //[7, 8, 6, 9]
  7. Collections.reverse(numList);
  8. System.out.println(numList); //[9, 6, 8, 7]
(2)自然排序sort
  • Collections.sort(List list):使用默认的比较器
  • Collections.sort(List list, Comparator comparator):使用指定的比较器来比较
  1. List<Integer> numList = new ArrayList<Integer>(5);
  2. numList.add(7);
  3. numList.add(8);
  4. numList.add(6);
  5. numList.add(9);
  6. Collections.sort(numList);
  7. System.out.println(numList); //[6, 7, 8, 9]
2.查找替换操作
(1)二分查找binarySearch

Collections.binarySearch(List list, T key):在list通过二分法查找指定的值key

  • list必须是已经排好序的
  • 返回值是查找的值在列表中的索引位置
  1. List<Integer> scoreList = new ArrayList<Integer>(5);
  2. scoreList.add(68);
  3. scoreList.add(71);
  4. scoreList.add(85);
  5. scoreList.add(93);
  6. System.out.println(Collections.binarySearch(scoreList, 85)); //2
(2)找出最大元素max
  1. List<Integer> scoreList = new ArrayList<Integer>(5);
  2. scoreList.add(68);
  3. scoreList.add(85);
  4. scoreList.add(71);
  5. scoreList.add(93);
  6. System.out.println(Collections.max(scoreList)); //93
(3)找出最小元素min
  1. List<Integer> scoreList = new ArrayList<Integer>(5);
  2. scoreList.add(68);
  3. scoreList.add(85);
  4. scoreList.add(71);
  5. scoreList.add(93);
  6. System.out.println(Collections.min(scoreList)); //68
(4)元素填充fill

Collections.fill(List list, T obj):使用obj来填充list

  • 被填充的列表必须有元素,不是空列表,否则不填充,原来有多少个元素,就填充多少个元素
  1. List<Integer> demoList = new ArrayList<Integer>(5);
  2. demoList.add(1);
  3. demoList.add(2);
  4. System.out.println(demoList); //[1, 2]
  5. Collections.fill(demoList, 10);
  6. System.out.println(demoList); //[10, 10]
(5)元素替换replaceAll

replaceAll(List list, T oldVal, T newVal):将列表中所有oldVal的元素替换为newVal

  1. List<Integer> demoList = new ArrayList<Integer>(5);
  2. demoList.add(15);
  3. demoList.add(0);
  4. demoList.add(18);
  5. demoList.add(0);
  6. System.out.println(demoList); //[15, 0, 18, 0]
  7. Collections.replaceAll(demoList, 0, 1);
  8. System.out.println(demoList); //[15, 1, 18, 1]
3.同步操作

作用:将原来线程非安全的转化为线程安全的集合(将集合变成同步的),都是以synchronized开头的

  • synchronizedCollection:将非线程安全的集合变成线程安全的(线程同步)集合
  • synchronizedMap:把非线程安全的Map变成线程安全的Map
  • synchronizedList:把List变成线程安全的List
  • synchronizedSet:把Set变成线程安全的Set
  • synchronizedSortedMap:把SortedMap变成线程安全的SortedMap
  • synchronizedSortedSet:把SortedSet变成线程安全的SortedSet