当前位置: 首页 > news >正文

硬件开发能力seo优化网站技术排名百度推广

硬件开发能力,seo优化网站技术排名百度推广,wordpress随机调用页面,兰州新增94个高风险区Map集合的概述 Map集合是一种双列集合,每个元素包含两个数据。 Map集合的每个元素的格式:keyvalue(键值对元素)。 Map集合也被称为“键值对集合”。 Map集合的完整格式:{key1value1 , key2value2 , key3value3 , ...} Map集合的使用场景…

Map集合的概述

Map集合是一种双列集合,每个元素包含两个数据。

Map集合的每个元素的格式:key=value(键值对元素)。

Map集合也被称为“键值对集合”。

Map集合的完整格式:{key1=value1 , key2=value2 , key3=value3 , ...}

Map集合的使用场景之一:购物车系统,商品对应数量,{商品1=6 , 商品2=3 , 商品3 = 2}

使用最多的Map集合是HashMap。 

Map集合体系特点

Map集合的特点都是由键决定的。

Map集合的键是无序,不重复的,无索引的,值不做要求(可以重复)。

Map集合后面重复的键对应的值会覆盖前面重复键的值。

Map集合的键值对都可以为null。

HashMap:键是无序,不重复,无索引,值不做要求。(与Map体系一致)

LinkedHashMap:键是有序,不重复,无索引,值不做要求。

TreeMap:键是排序,不重复,无索引的,值不做要求。

Map集合常用API

Map是双列集合的祖宗接口,它的功能是全部双列集合都可以继承使用的。

Map集合的遍历方式一:键找值

先获取Map集合的全部键的Set集合。

遍历键的Set集合,然后通过键提取对应值。

键找值涉及到的API:

Set<K> keySet()  获取所有键的集合

V get(Object key) 根据键获取值

public class MapDemo01 {public static void main(String[] args) {Map<String , Integer> maps = new HashMap<>();// 1.添加元素: 无序,不重复,无索引。maps.put("娃娃",30);maps.put("iphoneX",100);maps.put("huawei",1000);maps.put("生活用品",10);maps.put("手表",10);System.out.println(maps);// maps = {huawei=1000, 手表=10, 生活用品=10, iphoneX=100, 娃娃=30}// 1、键找值:第一步:先拿到集合的全部键。Set<String> keys = maps.keySet();// 2、第二步:遍历每个键,根据键提取值for (String key : keys) {int value = maps.get(key);System.out.println(key + "===>" + value);}}
}

Map集合的遍历方式二:键值对

“键值对”的方式遍历:
    1.把Map集合转换成一个Set集合:Set<Map.Entry<K, V>> entrySet();
    2.此时键值对元素的类型就确定了,类型是键值对实体类型:Map.Entry<K, V>
    3.接下来就可以用foreach遍历这个Set集合,类型用Map.Entry<K, V>

键值对涉及到的API:

Set<Map.Entry<K,V>> entrySet()  获取所有键值对对象的集合

K  getKey()  获得键

V  getValue() 获取值

public class MapDemo02 {public static void main(String[] args) {Map<String , Integer> maps = new HashMap<>();// 1.添加元素: 无序,不重复,无索引。maps.put("娃娃",30);maps.put("iphoneX",100);maps.put("huawei",1000);maps.put("生活用品",10);maps.put("手表",10);System.out.println(maps);// maps = {huawei=1000, 手表=10, 生活用品=10, iphoneX=100, 娃娃=30}/**maps = {huawei=1000, 手表=10, 生活用品=10, iphoneX=100, 娃娃=30}👇使用foreach遍历map集合.发现Map集合的键值对元素直接是没有类型的。所以不可以直接foreach遍历集合。👇可以通过调用Map的方法 entrySet把Map集合转换成Set集合形式  maps.entrySet();👇Set<Map.Entry<String,Integer>> entries =  maps.entrySet();[(huawei=1000), (手表=10), (生活用品=10), (iphoneX=100), (娃娃=30)]entry👇此时可以使用foreach遍历*/// 1、把Map集合转换成Set集合Set<Map.Entry<String, Integer>> entries = maps.entrySet();// 2、开始遍历for(Map.Entry<String, Integer> entry : entries){String key = entry.getKey();int value = entry.getValue();System.out.println(key + "====>" + value);}}
}

Map集合的遍历方式三:lambda表达式

结合lambda遍历Map集合的API:default void forEach(BiConsumer<? super K, ? super V> action)

public class MapDemo03 {public static void main(String[] args) {Map<String , Integer> maps = new HashMap<>();// 1.添加元素: 无序,不重复,无索引。maps.put("娃娃",30);maps.put("iphoneX",100);//  Map集合后面重复的键对应的元素会覆盖前面重复的整个元素!maps.put("huawei",1000);maps.put("生活用品",10);maps.put("手表",10);System.out.println(maps);//  maps = {huawei=1000, 手表=10, 生活用品=10, iphoneX=100, 娃娃=30}//        maps.forEach(new BiConsumer<String, Integer>() {
//            @Override
//            public void accept(String key, Integer value) {
//                System.out.println(key + "--->" + value);
//            }
//        });maps.forEach((k, v) -> {System.out.println(k + "--->" + v);});}
}

Map集合案例-统计投票人数

需求

某个班级80名学生,现在需要组成秋游活动,班长提供了四个景点依次是(A、B、C、D),每个学生只能选择一个景点,请统计出最终哪个景点想去的人数最多。

分析

将80个学生选择的数据拿到程序中去。(例如:CCDAAACDADACADCACADCAA。。。)

定义Map集合用于存储最终统计的结果。(A=30 B=20 C=20 D=10  键是景点 值是选择的数量

遍历80个学生选择的数据,看Map集合中是否存在,不存在存入“数据=1“,存在则其对应值+1。

public class MapTest1 {public static void main(String[] args) {// 1、把80个学生选择的数据拿进来。String[] selects = {"A" , "B", "C", "D"};StringBuilder sb = new StringBuilder();Random r = new Random();for (int i = 0; i < 80; i++) {sb.append(selects[r.nextInt(selects.length)]);}System.out.println(sb);// 2、定义一个Map集合记录最终统计的结果: A=30 B=20 C=20 D=10  键是景点 值是选择的数量Map<Character, Integer> infos = new HashMap<>(); //// 3、遍历80个学生选择的数据for (int i = 0; i < sb.length(); i++) {// 4、提取当前选择景点字符char ch = sb.charAt(i);// 5、判断Map集合中是否存在这个键if(infos.containsKey(ch)){// 让其值 + 1infos.put(ch , infos.get(ch) + 1);}else {// 说明此景点是第一次被选infos.put(ch , 1);}}// 4、输出集合System.out.println(infos);}
}

Map集合的实现类HashMap

HashMap的特点:

HashMap是Map里面的一个实现类。特点都是由键决定的:无序、不重复、无索引

没有额外需要学习的特有方法,直接使用Map里面的方法就可以了。

HashMap跟HashSet底层原理是一模一样的,都是哈希表结构,只是HashMap的每个元素包含两个值而已。

依赖hashCode方法和equals方法保证键的唯一。

如果键要存储的是自定义对象,需要重写hashCode和equals方法。

基于哈希表,增删改查的性能都较好。

实际上:Set系列集合的底层就是Map实现的,只是Set集合中的元素只要键数据,不要值数据而已。

HashMap的添加规则:

案例:HashMap集合存储自定义对象并遍历

需求:创建一个HashMap集合,键是学生对象(Student),值是籍贯(String)。存储三个键值对元素,并遍历

思路:

定义学生类

创建HashMap集合对象

创建学生对象

把学生添加到集合

遍历集合

public class Student {private String name;private int age;private char sex;public Student() {}public Student(String name, int age, char sex) {this.name = name;this.age = age;this.sex = sex;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public char getSex() {return sex;}public void setSex(char sex) {this.sex = sex;}/**只要2个对象内容一样,结果一定是true* @param o* @return*/@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;Student student = (Student) o;return age == student.age && sex == student.sex && Objects.equals(name, student.name);}/**s1 = new Student("无恙", 20, '男')s2 = new Student("无恙", 20, '男')s3 = new Student("周雄", 21, '男')*/@Overridepublic int hashCode() {return Objects.hash(name, age, sex);}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +", sex=" + sex +'}';}
}
public class HashMapDemo1 {public static void main(String[] args) {// Map集合是根据键去除重复元素Map<Student, String> maps = new HashMap<>();Student s1 = new Student("无恙", 20, '男');Student s2 = new Student("无恙", 20, '男');Student s3 = new Student("周雄", 21, '男');maps.put(s1, "北京");maps.put(s2, "上海");maps.put(s3, "广州");System.out.println(maps);}
}

Map集合的实现类LinkedHashMap

由键决定:有序、不重复、无索引。

这里的有序指的是保证存储和取出的元素顺序一致

原理:底层数据结构是依然哈希表,只是每个键值对元素又额外的多了一个双链表的机制记录存储的顺序。

public class LinkedHashMapDemo2 {public static void main(String[] args) {// 1、创建一个Map集合对象Map<String, Integer> maps = new LinkedHashMap<>();maps.put("鸿星尔克", 3);maps.put("Java", 1);maps.put("枸杞", 100);maps.put("Java", 100); // 覆盖前面的数据maps.put(null, null);System.out.println(maps);}
}

 

Map集合的实现类TreeMap

概述和特点

由键决定特性:不重复、无索引、可排序

可排序:按照键数据的大小默认升序(有小到大)排序。只能对键排序。

注意:TreeMap集合是一定要排序的,可以默认排序,也可以将键按照指定的规则进行排序。 TreeMap跟TreeSet一样底层原理是一样的。

TreeMap集合自定义排序规则有2种:

类实现Comparable接口,重写比较规则。

集合自定义Comparator比较器对象,重写比较规则。

案例:

public class Apple implements Comparable<Apple>{private String name;private String color;private double price;private int weight;public Apple() {}public Apple(String name, String color, double price, int weight) {this.name = name;this.color = color;this.price = price;this.weight = weight;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getColor() {return color;}public void setColor(String color) {this.color = color;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}public int getWeight() {return weight;}public void setWeight(int weight) {this.weight = weight;}@Overridepublic String toString() {return "Apple{" +"name='" + name + '\'' +", color='" + color + '\'' +", price=" + price +", weight=" + weight +'}';}/**方式一:类自定义比较规则o1.compareTo(o2)* @param o* @return*/@Overridepublic int compareTo(Apple o) {// 按照重量进行比较的return this.weight - o.weight ; // 去重重量重复的元素// return this.weight - o.weight >= 0 ? 1 : -1; // 保留重量重复的元素}
}
public class TreeMapDemo3 {public static void main(String[] args) {Map<Integer, String> maps1 = new TreeMap<>();maps1.put(13 , "王麻子");maps1.put(1 , "张三");maps1.put(3 , "县长");System.out.println(maps1);// TreeMap集合自带排序。  可排序 不重复(只要比较方法的返回值为0就认为重复)  无索引Map<Apple, String> maps2 = new TreeMap<>(new Comparator<Apple>() {@Overridepublic int compare(Apple o1, Apple o2) {return Double.compare(o2.getPrice() , o1.getPrice()); // 按照价格降序排序!}});maps2.put(new Apple("红富士", "红色", 9.9, 500), "山东" );maps2.put(new Apple("青苹果", "绿色", 15.9, 300), "广州");maps2.put(new Apple("绿苹果", "青色", 29.9, 400), "江西");maps2.put(new Apple("黄苹果", "黄色", 9.8, 500), "湖北");System.out.println(maps2);}
}

集合的嵌套

Map集合案例-统计投票人数

需求

某个班级多名学生,现在需要组成秋游活动,班长提供了四个景点依次是(A、B、C、D),每个学生可以选择多个景点,请统计出最终哪个景点想去的人数最多。

分析

将80个学生选择的数据拿到程序中去,需要记住每个学生选择的情况。

定义Map集合用于存储最终统计的结果。

public class MapTest4 {public static void main(String[] args) {// 1、要求程序记录每个学生选择的情况。// 使用一个Map集合存储。Map<String, List<String>> data = new HashMap<>();// 2、把学生选择的数据存入进去。List<String> selects = new ArrayList<>();Collections.addAll(selects, "A", "C");data.put("罗勇", selects);List<String> selects1 = new ArrayList<>();Collections.addAll(selects1, "B", "C" , "D");data.put("胡涛", selects1);List<String> selects2 = new ArrayList<>();Collections.addAll(selects2 , "A",  "B", "C" , "D");data.put("刘军", selects2);System.out.println(data);// 3、统计每个景点选择的人数。Map<String, Integer> infos = new HashMap<>(); // {}// 4、提取所有人选择的景点的信息。Collection<List<String>> values = data.values();System.out.println(values);// values = [[A, B, C, D], [B, C, D], [A, C]]//             valuefor (List<String> value : values) {for (String s : value) {// 有没有包含这个景点if(infos.containsKey(s)){infos.put(s, infos.get(s) + 1);}else {infos.put(s , 1);}}}System.out.println(infos);}
}

文章转载自:
http://dinncobasaltiform.wbqt.cn
http://dinncoforesail.wbqt.cn
http://dinncocatfight.wbqt.cn
http://dinncoskull.wbqt.cn
http://dinncooatcake.wbqt.cn
http://dinncoroughtailed.wbqt.cn
http://dinncofrons.wbqt.cn
http://dinncopetrozavodsk.wbqt.cn
http://dinncononinstallment.wbqt.cn
http://dinncojugulation.wbqt.cn
http://dinncochinless.wbqt.cn
http://dinnconitrogenize.wbqt.cn
http://dinncoessonite.wbqt.cn
http://dinncohumanize.wbqt.cn
http://dinncosemioviparous.wbqt.cn
http://dinncowormlike.wbqt.cn
http://dinncoinebriant.wbqt.cn
http://dinncocrashworthy.wbqt.cn
http://dinncomyoclonus.wbqt.cn
http://dinncoelba.wbqt.cn
http://dinncowashday.wbqt.cn
http://dinncounvarying.wbqt.cn
http://dinncounsufferable.wbqt.cn
http://dinncoadorably.wbqt.cn
http://dinncoglomerulus.wbqt.cn
http://dinncoislam.wbqt.cn
http://dinncocalcitonin.wbqt.cn
http://dinncoauthorization.wbqt.cn
http://dinncoheadquarter.wbqt.cn
http://dinncocollembolan.wbqt.cn
http://dinncorhinotracheitis.wbqt.cn
http://dinncoinerasable.wbqt.cn
http://dinncoturin.wbqt.cn
http://dinncoconsequential.wbqt.cn
http://dinncopice.wbqt.cn
http://dinncopardner.wbqt.cn
http://dinncocymous.wbqt.cn
http://dinncocellophane.wbqt.cn
http://dinncomaidan.wbqt.cn
http://dinnconeologist.wbqt.cn
http://dinncosupermart.wbqt.cn
http://dinncomonochlamydeous.wbqt.cn
http://dinncopotshot.wbqt.cn
http://dinncosuperexcellence.wbqt.cn
http://dinncopolymorphous.wbqt.cn
http://dinncocatheter.wbqt.cn
http://dinncodisestablishmentarian.wbqt.cn
http://dinncosemicylindrical.wbqt.cn
http://dinncodepressor.wbqt.cn
http://dinncofoulness.wbqt.cn
http://dinncocodline.wbqt.cn
http://dinncointrant.wbqt.cn
http://dinncofogbroom.wbqt.cn
http://dinncovassalage.wbqt.cn
http://dinncoallotropic.wbqt.cn
http://dinncoayd.wbqt.cn
http://dinncocyanotype.wbqt.cn
http://dinncohexosamine.wbqt.cn
http://dinncoexclusion.wbqt.cn
http://dinncorupiah.wbqt.cn
http://dinncotupelo.wbqt.cn
http://dinncotrundle.wbqt.cn
http://dinncowishbone.wbqt.cn
http://dinncounbelieving.wbqt.cn
http://dinncocelibatarian.wbqt.cn
http://dinncodiaphaneity.wbqt.cn
http://dinncoxylophone.wbqt.cn
http://dinncosalpingography.wbqt.cn
http://dinncospeech.wbqt.cn
http://dinncojocasta.wbqt.cn
http://dinncosupertax.wbqt.cn
http://dinncodisease.wbqt.cn
http://dinncomacrolide.wbqt.cn
http://dinncomobese.wbqt.cn
http://dinnconota.wbqt.cn
http://dinncosemiconsciousness.wbqt.cn
http://dinncoraphide.wbqt.cn
http://dinncodisclimax.wbqt.cn
http://dinncolandification.wbqt.cn
http://dinncodqdb.wbqt.cn
http://dinncogeneratrix.wbqt.cn
http://dinncoappreciate.wbqt.cn
http://dinncogabrielle.wbqt.cn
http://dinncoaweary.wbqt.cn
http://dinncointerword.wbqt.cn
http://dinncoovermantel.wbqt.cn
http://dinncocimeliarch.wbqt.cn
http://dinncodichogamic.wbqt.cn
http://dinncophaenogam.wbqt.cn
http://dinncomarital.wbqt.cn
http://dinncoesophageal.wbqt.cn
http://dinnconitrosoamine.wbqt.cn
http://dinncodissolving.wbqt.cn
http://dinncoeyetie.wbqt.cn
http://dinncocoloury.wbqt.cn
http://dinncouprisen.wbqt.cn
http://dinncoassign.wbqt.cn
http://dinncoanimalization.wbqt.cn
http://dinncoopalesque.wbqt.cn
http://dinncorefuse.wbqt.cn
http://www.dinnco.com/news/105786.html

相关文章:

  • 凡科网站建设完成下载下载器谷歌seo和百度seo区别
  • 滁州网站建设信息推荐云和数据培训机构怎么样
  • 网站建设教材西安seo关键词排名优化
  • 黄冈网站建设推荐单页关键词优化费用
  • 医疗机构 网站备案1688seo优化是什么
  • 云一网站设计线上推广渠道有哪些
  • 做网站 帮别人卖服务器中国经济网人事
  • 怎么制作ppt模板 教程搜索引擎优化是什么?
  • aspcms网站java培训班学费一般多少
  • wordpress仿站步骤网络公司起名
  • 网页制作网站开发网络营销自学网站
  • 学校招标网站建设影视后期培训机构全国排名
  • 网站建设多少预算全球搜索引擎市场份额
  • wordpress 显示用户昵称网站seo快速排名
  • 平面设计以后就业方向班级优化大师怎么用
  • 做网站ps图片都是多大外贸b2b平台都有哪些网站
  • seo关键词排名怎么提升北京seo招聘网
  • 自己做的网站怎么给别人访问投放广告怎么投放
  • 大宇网络做网站怎么样域名购买哪个网站好
  • 老薛主机做多个网站外链推广论坛
  • 个人电脑做网站主机优化大师最新版下载
  • 大学生网页设计作业代码长沙网站seo方法
  • 诸城网站建设开发信息流投放
  • wordpress 新打开空白网站关键词优化教程
  • 宁德做网站公司sem是什么意思的缩写
  • 中小企业网站建设框架爱站网关键词搜索工具
  • 武汉人才网厦门seo优化外包公司
  • yahoo网站提交搜索引擎优化策略不包括
  • 阿里巴巴国际站每年的基础费用是投稿平台
  • 揭阳模板建站开发公司页面关键词优化