Java语言步调计划——篇十一(5)

[复制链接]
发表于 2026-2-13 04:12:15 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×


          🌿🌿🌿跟随博主脚步,从这里开始→博主主页🌿🌿🌿
  

  • 接待各人:这里是我的学习条记、总结知识的地方,喜好的话请三连,有题目可以私信🌳🌳🌳
    您的点赞👍、关注➕、收藏⭐️、品评📝、私信📧是我最大的支持与鼓舞!!!🌻🌻🌻
  

Map接口及实现类



  • Map是用来存储 “键/值”对 的对象。在Map中存储的关键字和值都必须是对象,并要求关键字是唯一的,而值可以重复。

Map接口操纵



  • Map接口根本操纵:
  1.   public V put(K key, V value)  public V get(Object key)  public V remove(Object key)  public int size()  public void clear()  public Set<K> keySet()  public Collection<V> values()
复制代码
Map接口的实现类



  • Map接口的常用实现类有HashMap、TreeMap。
  • HashMap类以散列方法存放“键/值”对。其构造方法&#xff1a;
  1. HashMap() HashMap(int initialCapacity) HashMap(Map m)
复制代码


  • TreeMap类包管Map中的“键/值”对按关键字升序排序。其构造方法&#xff1a;
  1. TreeMap()TreeMap(Comparator c)TreeMap(Map m)
复制代码
实战演练

例1&#xff1a;HashMapDemo.java
  1. import java.util.*;public class HashMapDemo {public static void main(String[] args) {   String[] country&#61;{&#34;中国&#34;,&#34;印度&#34;,&#34;澳大利亚&#34;,&#34;德国&#34;,&#34;古巴&#34;,                        &#34;希腊&#34;,&#34;日本&#34;};   String[] capital&#61;{&#34;Beijing&#34;,&#34;New Delhi&#34;,&#34;Canberra&#34;,&#34;Berlin&#34;,                                &#34;Havana&#34;,&#34;Athens&#34;,&#34;Tokyo&#34;};   Map<String, String> m &#61; new HashMap<>();   for(int i &#61; 0;i<country.length;i&#43;&#43;)        m.put(country[i], capital[i]);   System.out.println(&#34;共有 &#34; &#43; m.size() &#43; &#34; 个国家:&#34;);   System.out.println(m);   System.out.println(m.get(&#34;中国&#34;));   // Beijing   m.remove(&#34;日本&#34;);      Set<String> coun &#61; m.keySet();   for(Object c : coun)       System.out.print(c &#43; &#34; &#34;);   }}
复制代码

例2&#xff1a;TreeMapDemo.java
  1. import java.util.*;public class TreeMapDemo {public static void main(String[] args) {   String[] country&#61;{&#34;中国&#34;,&#34;印度&#34;,&#34;澳大利亚&#34;,&#34;德国&#34;,&#34;古巴&#34;,                        &#34;希腊&#34;,&#34;日本&#34;};   String[] capital&#61;{&#34;Beijing&#34;,&#34;New Delhi&#34;,&#34;Canberra&#34;,&#34;Berlin&#34;,                                &#34;Havana&#34;,&#34;Athens&#34;,&#34;Tokyo&#34;};   Map<String, String> m &#61; new TreeMap<>();   for(int i &#61; 0;i<country.length;i&#43;&#43;)        m.put(country[i], capital[i]);   System.out.println(&#34;共有 &#34; &#43; m.size() &#43; &#34; 个国家:&#34;);   System.out.println(m);   System.out.println(m.get(&#34;中国&#34;));   // Beijing   m.remove(&#34;日本&#34;);      Set<String> coun &#61; m.keySet();   for(Object c : coun)       System.out.print(c &#43; &#34; &#34;);   }}
复制代码



  • 在Map对象的键上迭代
  1.   for(String key: map.keyset())  system.out.println(key);
复制代码


  • 在Map对象的值上迭代
  1. for(Integer value: map.values()) system.out.println(value);
复制代码
Collections类



  • java.util.Collections类是工具类&#xff0c;这个类提供了多少static方法实现聚集对象的操纵。这些操纵大多针对List操纵。告急包罗下面几个方面&#xff1a;
   排序 sort()
查找 binarySearch()
重排 shuffle()
求极值以及其他操纵。 max(), min(), reverse()等。
  Collections类 — 排序

  1. public static<T> void sort(List<T> list)
复制代码
功能&#xff1a;按照元素的天然序次举行排序
  1. public static<T> void sort(List<T> list,         Comparator<? Super T> c)
复制代码
功能&#xff1a;根据指定的比力器举行排序
实战演练

例&#xff1a;ListSortDemo.java
  1. import java.util.Arrays;import java.util.Collections;import java.util.List;public class ListSortDemo {public static void main(String[] args) {List<String> names &#61; Arrays.asList(&#34;peter&#34;,&#34;anna&#34;,&#34;mike&#34;,&#34;Lily&#34;);Collections.sort(names);System.out.println(names);Collections.sort(names, (a,b)->b.compareTo(a));System.out.println(names);}}
复制代码

Collections类 — 查找

  1. public static<T> int binarySearch(List<T> list, T key)
复制代码
功能&#xff1a;在按照天然序次排序后的List中查找key的位置
  1. public static<T> int binarySearch(List<T> list, T key,         Comparator<? super T>  c)
复制代码
功能&#xff1a;根据指定的比力器对List排序后&#xff0c;查找key的位置

实战演练

例&#xff1a;ListSearchDemo.java
  1. import java.util.*;public class ListSearchDemo{public static void main(String[] args) {List<Integer> list &#61; Arrays.asList(5,3,1,7);Collections.sort(list);   //先排序再查找Integer key &#61; 4;int pos &#61; Collections.binarySearch(list, key);  //-3if(pos < 0) {List<Integer> nList &#61; new ArrayList<>(list);nList.add(-pos-1,key);System.out.println(nList);      }   }}
复制代码

Collections类 — 打乱元素序次

  1. public static void shuffle(List<T> list)
复制代码
功能&#xff1a;使用默认的随机数打乱List中元素的序次
  1. public static void shuffle (List<T> list, Random rnd)
复制代码
功能&#xff1a;使用指定的Random对象打乱List中元素的序次
实战演练

例&#xff1a;ListShuffleDemo.java
  1. import java.util.*;public class ListShuffleDemo {public static void main(String[] args) {Integer[] num &#61; {1,3,5,6,4,2,7,9,8,10};List<Integer> list &#61; Arrays.asList(num);System.out.println(list);Collections.shuffle(list);System.out.println(list); //每次运行输出的效果差别   }}
复制代码

Collections类 — 求极值

  1. public static<T> T max(Collection<? extends T> coll)
复制代码
功能&#xff1a;返回给定聚集根据其元素天然排序后的最大元素
  1. public static<T> T max(Collection<? extends T> coll, Comparator<? super T> comp)
复制代码
功能&#xff1a;返回给定聚集根据指定的比力器排序后的最大元素
  1. public static<T> T min(Collection<? extends T> coll)
复制代码
功能&#xff1a;返回给定聚集根据其元素天然排序后的最小元素
  1. public static<T> T min(Collection<? extends T> coll, Comparator<? super T> comp)
复制代码
功能&#xff1a;返回给定聚集根据指定的比力器排序后的最大元素
实战演练

例&#xff1a;ListExtremeDemo.java
  1. import java.util.*;public class ListExtremeDemo {public static void main(String[] args) {List<String> list &#61; Arrays.asList(&#34;dog&#34;,&#34;cat&#34;,&#34;tiger&#34;);System.out.println(Collections.max(list));System.out.println(Collections.max(list, (String a,String b)->b.compareTo(a)));System.out.println(Collections.min(list));System.out.println(Collections.min(list, (String a,String b)->b.compareTo(a)));    }}
复制代码

Collections类 — 其他常用方法

  1. public static void reverse(List<?> list)
复制代码
功能&#xff1a;反转指定列表中元素的序次。
  1. public static <T> void fill(List<? super T> list, T obj)
复制代码
功能&#xff1a;用指定的值obj更换指定列表的全部元素。
  1. public static <T> void copy(List<? super T> dest, List<? extends T> src)
复制代码
功能&#xff1a;将src中的元素复制到dest列表中。要求&#xff1a;dest列表长度≥src列表长度。
  1. public static void swap(List<?> list, int i, int j)
复制代码
功能&#xff1a;在指定的列表中的指定位置上交换元素。
博主用心写&#xff0c;读者点关注&#xff0c;互动传真情&#xff0c;知识不迷路。
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表