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

网站里面送礼物要钱怎么做代码dede添加网站背景

网站里面送礼物要钱怎么做代码,dede添加网站背景,营销型网站方案书,网站一年续费多少钱文章目录 一、Map集合1.1 Map集合概述和特点【理解】1.2 Map集合的基本功能【应用】1.3 Map集合的获取功能【应用】1.4 Map集合的两种遍历方式 二、HashMap集合2.1 HashMap集合概述和特点【理解】2.2 HashMap的组成、构造函数2.3 put、查找方法2.4 HashMap集合应用案例【应用】… 文章目录 一、Map集合1.1 Map集合概述和特点【理解】1.2 Map集合的基本功能【应用】1.3 Map集合的获取功能【应用】1.4 Map集合的两种遍历方式 二、HashMap集合2.1 HashMap集合概述和特点【理解】2.2 HashMap的组成、构造函数2.3 put、查找方法2.4 HashMap集合应用案例【应用】 三、TreeMap集合3.1 TreeMap集合概述和特点【理解】3.2 TreeMap集合应用案例【应用】 四、可变参数五、Collections类5.1 Collections常用功能5.2 Comparator比较器 六、综合练习练习1随机点名器练习2带概率的随机练习3随机不重复练习4集合的嵌套 还记得 Java集合框架体系、Collection、List、ArrayList、LinkedList、Set、TreeSet、HashSet 吗如果忘记可以到这里重新温习 Java集合【超详细】 本文我们重点介绍 Map、HahsMap、TreeMap相关集合。Collection、List、Set相关部分可查看 Java集合【超详细】。 一、Map集合 1.1 Map集合概述和特点【理解】 Map集合概述 interface MapK,V K键的类型V值的类型Map集合的特点 双列集合,一个键对应一个值键不可以重复,值可以重复 Map集合的基本使用 public class MapDemo01 {public static void main(String[] args) {MapString, String map new HashMap();//V put(K key, V value) 将指定的值与该映射中的指定键相关联map.put(student1, zhangsan);map.put(student2, lisi);map.put(student3, wangwu);map.put(student4, zhaoliu);//输出集合对象//{student2lisi, student1zhangsan, student4zhaoliu, student3wangwu}System.out.println(map);} }1.2 Map集合的基本功能【应用】 方法介绍 方法名说明V put(K key,V value)添加元素V remove(Object key)根据键删除键值对元素void clear()移除所有的键值对元素boolean containsKey(Object key)判断集合是否包含指定的键boolean containsValue(Object value)判断集合是否包含指定的值boolean isEmpty()判断集合是否为空int size()集合的长度也就是集合中键值对的个数 示例代码 public class MapDemo02 {public static void main(String[] args) {MapString, String map new HashMap();//V put(K key, V value) 将指定的值与该映射中的指定键相关联map.put(student1, zhangsan);map.put(student2, lisi);map.put(student3, wangwu);map.put(student4, zhaoliu);// System.out.println(map.remove(student1)); //zhangsan // System.out.println(map.remove(student5)); //null // //{student2lisi, student4zhaoliu, student3wangwu} // System.out.println(map);// map.clear(); // System.out.println(map.size() , map); //0, {}System.out.println(map.containsKey(student1)); //trueSystem.out.println(map.containsKey(student5)); //falseSystem.out.println(map.containsValue(zhangsan)); //trueSystem.out.println(map.containsValue(Jenny)); //falseSystem.out.println(map.isEmpty()); //falseSystem.out.println(map.size()); //4System.out.println(map);} }1.3 Map集合的获取功能【应用】 方法介绍 方法名说明V get(Object key)根据键获取值Set keySet()获取所有键的集合Collection values()获取所有值的集合SetMap.EntryK,V entrySet()获取所有键值对对象的集合 示例代码 public class MapDemo03 {public static void main(String[] args) {MapString, String map new HashMap();map.put(student1, zhangsan);map.put(student2, lisi);map.put(student3, wangwu);map.put(student4, zhaoliu);System.out.println(map.get(student1));System.out.println(map.get(student5));//SetK keySet():获取所有键的集合SetString keySet map.keySet();for (String key : keySet) {System.out.println(key);}System.out.println(~~~~~);//CollectionV values():获取所有值的集合CollectionString values map.values();for (String value : values) {System.out.println(value);}System.out.println(~~~~~);SetMap.EntryString, String entries map.entrySet();for (Map.EntryString, String entry : entries) {System.out.println(entry);}System.out.println(entries);} }1.4 Map集合的两种遍历方式 Map集合有两种遍历方式 方式一 获取所有键的集合。用keySet()方法实现遍历键的集合获取到每一个键。用增强for实现根据键去找值。用get(Object key)方法实现 方式二 获取所有键值对对象的集合 SetMap.EntryK,V entrySet()获取所有键值对对象的集合 遍历键值对对象的集合得到每一个键值对对象 用增强for实现得到每一个Map.Entry 根据键值对对象获取键和值 用getKey()得到键用getValue()得到值 public class MapDemo04 {public static void main(String[] args) {MapString, String map new HashMap();map.put(student1, zhangsan);map.put(student2, lisi);map.put(student3, wangwu);map.put(student4, zhaoliu);//方式一获取所有键的集合。用keySet()方法实现SetString keySet map.keySet();for (String key : keySet) {//根据键去找值。用get(Object key)方法实现String value map.get(key);System.out.println(key , value);}System.out.println(~~~~~);//方式二获取所有键值对对象的集合SetMap.EntryString, String entries map.entrySet();for (Map.EntryString, String entry : entries) {//根据键值对对象获取键和值String key entry.getKey();String value entry.getValue();System.out.println(key , value);}} }二、HashMap集合 2.1 HashMap集合概述和特点【理解】 HashMap底层是哈希表结构的依赖hashCode方法和equals方法保证键的唯一如果键要存储的是自定义对象需要重写hashCode和equals方法 HashMap存放数据的数据是什么呢代码中存放数据的容器如下 transient NodeK,V[] table;说明了该容器中是一个又一个node组成而node有三种实现所以hashMap中存放的node的形式既可以是Node也可以是TreeNode。 2.2 HashMap的组成、构造函数 HashMap的组成 public class HashMapK,V extends AbstractMapK,Vimplements MapK,V, Cloneable, Serializable {private static final long serialVersionUID 362498820763181265L;//是hashMap的最小容量16容量就是数组的大小也就是变量transient NodeK,V[] table。static final int DEFAULT_INITIAL_CAPACITY 1 4; // aka 16//最大数量该数组最大值为2^31一次方。static final int MAXIMUM_CAPACITY 1 30;//默认的加载因子如果构造的时候不传则为0.75static final float DEFAULT_LOAD_FACTOR 0.75f;//一个位置里存放的节点转化成树的阈值也就是8比如数组里有一个node这个node链表的长度达到该值才会转化为红黑树。static final int TREEIFY_THRESHOLD 8;//当一个反树化的阈值当这个node长度减少到该值就会从树转化成链表static final int UNTREEIFY_THRESHOLD 6;//满足节点变成树的另一个条件就是存放node的数组长度要达到64static final int MIN_TREEIFY_CAPACITY 64;//具体存放数据的数组transient NodeK,V[] table;//entrySet一个存放k-v缓冲区transient SetMap.EntryK,V entrySet;//size是指hashMap中存放了多少个键值对transient int size;//对map的修改次数transient int modCount;//The next size value at which to resize (capacity * load factor).int threshold;//加载因子final float loadFactor;//... }HashMap的构造函数 //只有容量initialCapacity public HashMap(int initialCapacity) {this(initialCapacity, DEFAULT_LOAD_FACTOR); }public HashMap() {this.loadFactor DEFAULT_LOAD_FACTOR; // all other fields defaulted }public HashMap(Map? extends K, ? extends V m) {this.loadFactor DEFAULT_LOAD_FACTOR;putMapEntries(m, false); }final void putMapEntries(Map? extends K, ? extends V m, boolean evict) {int s m.size();if (s 0) {if (table null) { // pre-sizefloat ft ((float)s / loadFactor) 1.0F;int t ((ft (float)MAXIMUM_CAPACITY) ?(int)ft : MAXIMUM_CAPACITY);if (t threshold)threshold tableSizeFor(t);}else if (s threshold)resize();for (Map.Entry? extends K, ? extends V e : m.entrySet()) {K key e.getKey();V value e.getValue();putVal(hash(key), key, value, false, evict);}} }public HashMap(int initialCapacity, float loadFactor) {if (initialCapacity 0) // 容量不能为负数throw new IllegalArgumentException(Illegal initial capacity: initialCapacity);//当容量大于2^31就取最大值131;if (initialCapacity MAXIMUM_CAPACITY)initialCapacity MAXIMUM_CAPACITY;if (loadFactor 0 || Float.isNaN(loadFactor))throw new IllegalArgumentException(Illegal load factor: loadFactor);this.loadFactor loadFactor;//当前数组table的大小一定是是2的幂次方// tableSizeFor保证了数组一定是是2的幂次方是大于initialCapacity最接近的值。this.threshold tableSizeFor(initialCapacity); }tableSizeFor()方法保证了数组大小一定是是2的幂次方,是如何实现的呢 static final int tableSizeFor(int cap) {int n cap - 1;n | n 1;n | n 2;n | n 4;n | n 8;n | n 16;return (n 0) ? 1 : (n MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n 1; }该方法将一个二进制数第一位1后边的数字全部变成1然后再加1这样这个二进制数就一定是100…这样的形式。此处实现在ArrayDeque的实现中也用到了类似的方法来保证数组长度一定是2的幂次方。 2.3 put、查找方法 put方法 开发人员使用的put方法 public V put(K key, V value) {return putVal(hash(key), key, value, false, true); }HashMap内部使用的put值的方法 final V putVal(int hash, K key, V value, boolean onlyIfAbsent,boolean evict) {NodeK,V[] tab; NodeK,V p; int n, i;if ((tab table) null || (n tab.length) 0)n (tab resize()).length;//当hash到的位置该位置为null的时候存放一个新node放入 // 这儿p赋值成了table该位置的node值if ((p tab[i (n - 1) hash]) null)tab[i] newNode(hash, key, value, null);else {NodeK,V e; K k;//该位置第一个就是查找到的值将p赋给eif (p.hash hash ((k p.key) key || (key ! null key.equals(k))))e p;//如果是红黑树调用红黑树的putTreeVal方法 else if (p instanceof TreeNode)e ((TreeNodeK,V)p).putTreeVal(this, tab, hash, key, value);else {//是链表遍历注意e p.next这个一直将下一节点赋值给e直到尾部注意开头是binCountfor (int binCount 0; ; binCount) {if ((e p.next) null) {p.next newNode(hash, key, value, null);//当链表长度大于等于7插入第8位树化if (binCount TREEIFY_THRESHOLD - 1) // -1 for 1sttreeifyBin(tab, hash);break;}if (e.hash hash ((k e.key) key || (key ! null key.equals(k))))break;p e;}}if (e ! null) { // existing mapping for keyV oldValue e.value;if (!onlyIfAbsent || oldValue null)e.value value;afterNodeAccess(e);return oldValue;}}modCount;if (size threshold)resize();afterNodeInsertion(evict);return null; }查找方法 final NodeK,V getNode(int hash, Object key) {NodeK,V[] tab; NodeK,V first, e; int n; K k;//先判断表不为空if ((tab table) ! null (n tab.length) 0 //这一行是找到要查询的Key在table中的位置table是存放HashMap中每一个Node的数组。(first tab[(n - 1) hash]) ! null) {//Node可能是一个链表或者树先判断根节点是否是要查询的key,就是根节点方便后续遍历Node写法并且//对于只有根节点的Node直接判断if (first.hash hash // always check first node((k first.key) key || (key ! null key.equals(k))))return first;//有子节点if ((e first.next) ! null) {//红黑树查找if (first instanceof TreeNode)return ((TreeNodeK,V)first).getTreeNode(hash, key);do {//链表查找if (e.hash hash ((k e.key) key || (key ! null key.equals(k))))return e;}//遍历链表当链表后续为null则推出循环while ((e e.next) ! null);}}return null; }2.4 HashMap集合应用案例【应用】 案例需求 创建一个HashMap集合键是学生对象(Student)值是居住地 (String)。存储多个元素并遍历。要求保证键的唯一性如果学生对象的成员变量值相同我们就认为是同一个对象 代码实现 学生类 public class Student {private String name;private int age;public Student() {}public Student(String name, int age) {this.name name;this.age age;}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;}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 Objects.equals(name, student.name);}Overridepublic int hashCode() {return Objects.hash(name, age);} }测试类 public class HashMapDemo {public static void main(String[] args) {HashMapStudent, String hm new HashMapStudent, String();Student s1 new Student(林青霞, 30);Student s2 new Student(张曼玉, 35);Student s3 new Student(王祖贤, 33);Student s4 new Student(王祖贤, 33);//把学生添加到集合hm.put(s1, 西安);hm.put(s2, 武汉);hm.put(s3, 郑州);hm.put(s4, 北京);SetStudent keySet hm.keySet();for (Student key : keySet) {String value hm.get(key);System.out.println(key.getName() , key.getAge() , value);}} }三、TreeMap集合 3.1 TreeMap集合概述和特点【理解】 TreeMap底层是红黑树结构依赖自然排序或者比较器排序对键进行排序如果键存储的是自定义对象需要实现Comparable接口或者在创建TreeMap对象时候给出比较器排序规则 3.2 TreeMap集合应用案例【应用】 案例需求 创建一个TreeMap集合键是学生对象(Student)值是籍贯(String)学生属性姓名和年龄按照年龄进行排序并遍历要求按照学生的年龄进行排序如果年龄相同则按照姓名进行排序 代码实现 学生类 public class Student implements ComparableStudent{private String name;private int age;public Student() {}public Student(String name, int age) {this.name name;this.age age;}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;}Overridepublic String toString() {return Student{ name name \ , age age };}Overridepublic int compareTo(Student o) {//按照年龄进行排序int result o.getAge() - this.getAge();//次要条件按照姓名排序。result result 0 ? o.getName().compareTo(this.getName()) : result;return result;} }测试类 public class TreeMapDemo {public static void main(String[] args) {TreeMapStudent,String tm new TreeMap();Student s1 new Student(zhangsan,23);Student s2 new Student(lisi,22);Student s3 new Student(wangwu,22);Student s4 new Student(zhangsan,21);tm.put(s1,江苏);tm.put(s2,北京);tm.put(s3,天津);tm.put(s4, 武汉);// 遍历TreeMap集合,打印每个学生的信息tm.forEach((Student key, String value) - {System.out.println(key --- value);});} }四、可变参数 在JDK1.5之后如果我们定义一个方法需要接受多个参数并且多个参数类型一致我们可以对其简化. 格式 修饰符 返回值类型 方法名(参数类型... 形参名){ }**底层**其实就是一个数组 **好处**在传递数据的时候省的我们自己创建数组并添加元素了JDK底层帮我们自动创建数组并添加元素了 代码演示: public class ChangeArgs {public static void main(String[] args) {int sum getSum(8, 10, 5, 13, 2300);System.out.println(sum);}public static int getSum(int... arr) {int sum 0;for (int a : arr) {sum a;}return sum;} }注意 ​ 1.一个方法只能有一个可变参数 ​ 2.如果方法中有多个参数可变参数要放到最后。 应用场景: Collections 在Collections中也提供了添加一些元素方法 public static T boolean addAll(CollectionT c, T... elements) :往集合中添加一些元素。 public class CollectionsDemo {public static void main(String[] args) {ListInteger list new ArrayList();//原来写法list.add(9);list.add(14);list.add(8);list.add(320);System.out.println(list); //[9, 14, 8, 320]//采用工具类 完成 往集合中添加元素Collections.addAll(list, 15, 14, 7, 310);System.out.println(list);} }五、Collections类 5.1 Collections常用功能 java.utils.Collections是集合工具类用来对集合进行操作。常用方法如下 public static void shuffle(List? list) :打乱集合顺序。public static T void sort(ListT list):将集合中元素按照默认规则排序。public static T void sort(ListT listComparator? super T ):将集合中元素按照指定规则排序。 代码演示 public class CollectionsDemo1 {public static void main(String[] args) {ListInteger list new ArrayList();//原来写法list.add(9);list.add(8);list.add(320);list.add(14);System.out.println(list); //[9, 8, 320, 14]//将集合中元素按照默认的自然规则排序Collections.sort(list);System.out.println(list); //[8, 9, 14, 320]//打乱集合顺序Collections.shuffle(list);System.out.println(list); //[320, 8, 14, 9]} }我们的集合按照默认的自然顺序进行了排列如果想要指定顺序那该怎么办呢 5.2 Comparator比较器 如果希望根据指定规则对集合进行排序可使用public static T void sort(ListT listComparator? super T )方法 Student类 public class Student {private String name;private int age;public Student() {}public Student(String name, int age) {this.name name;this.age age;}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;}Overridepublic String toString() {return Student{ name name \ , age age };} }测试类 public class StudentSort {public static void main(String[] args) {ListStudent list new ArrayList();list.add(new Student(Rose,18));list.add(new Student(Jack,16));list.add(new Student(LiHua,20));list.add(new Student(Jenny,18));Collections.sort(list, new ComparatorStudent() {Overridepublic int compare(Student o1, Student o2) {//以学生的年龄升序如果年龄一样 就按名字升序int result o1.getAge() - o2.getAge();result result0 ? o1.getName().compareTo(o2.getName()) : result;return result;}});for (Student stu : list) {System.out.println(stu);}} }结果输出 Student{name‘Jack’, age16} Student{name‘Jenny’, age18} Student{name‘Rose’, age18} Student{name‘LiHua’, age20} 六、综合练习 练习1随机点名器 需求班级里有N个学生实现随机点名器 public class Test1 {public static void main(String[] args) {ListString list new ArrayList();Collections.addAll(list,范闲,范建,范统,杜子腾,杜琦燕,宋合泛,侯笼藤,朱益群,朱穆朗玛峰,袁明媛);//法一Random r new Random();int index r.nextInt(list.size());String name list.get(index);System.out.println(name);//法二 打乱顺序Collections.shuffle(list);String name1 list.get(0);System.out.println(name1);} }练习2带概率的随机 需求 ​ 班级里有N个学生 ​ 要求在随机的时候70%的概率随机到男生30%的概率随机到女生 public class Test2 {public static void main(String[] args) {ListInteger list new ArrayList();Collections.addAll(list,1,1,1,1,1,1,1);Collections.addAll(list,0,0,0);//打乱集合中的数据Collections.shuffle(list);//从list集合中随机抽取0或者1Random r new Random();int index r.nextInt(list.size());int number list.get(index);System.out.println(number);//创建两个集合分别存储男生和女生的名字ArrayListString boyList new ArrayList();ArrayListString girlList new ArrayList();Collections.addAll(boyList,范闲,范建,范统,杜子腾,宋合泛,侯笼藤,朱益群,朱穆朗玛峰);Collections.addAll(girlList,杜琦燕,袁明媛,李猜,田蜜蜜);//判断此时是从boyList里面抽取还是从girlList里面抽取if(number 1){//boyListint boyIndex r.nextInt(boyList.size());String name boyList.get(boyIndex);System.out.println(name);}else{//girlListint girlIndex r.nextInt(girlList.size());String name girlList.get(girlIndex);System.out.println(name);}} }练习3随机不重复 需求 ​ 班级里有N个学生被点到的学生不会再被点到。但是如果班级中所有的学生都点完了 需要重新开启第二轮点名。 public class Test3 {public static void main(String[] args) {ListString list1 new ArrayList();Collections.addAll(list1, 范闲, 范建, 范统, 杜子腾, 杜琦燕, 宋合泛, 侯笼藤, 朱益群, 朱穆朗玛峰, 袁明媛);//创建一个临时的集合用来存已经被点到学生的名字ListString list2 new ArrayList();//外循环表示轮数for (int i 0; i 10; i) {System.out.println(第 i 轮点名开始了);int count list1.size();Random r new Random();//内循环每一轮中随机循环抽取的过程for (int j 0; j count; j) {int index r.nextInt(list1.size());String name list1.remove(index);list2.add(name);System.out.println(name);}//此时表示一轮点名结束//list1 空了 list2 10个学生的名字list1.addAll(list2);list2.clear();}} }练习4集合的嵌套 需求 ​ 定义一个Map集合键用表示省份名称province值表示市city但是市会有多个。 添加完毕后遍历结果格式如下 江苏省 南京市扬州市苏州市无锡市常州市 湖北省 武汉市孝感市十堰市宜昌市鄂州市 河北省 石家庄市唐山市邢台市保定市张家口市 public class Test4 {public static void main(String[] args) {MapString, ArrayListString hm new HashMap();//创建单列集合存储市ArrayListString city1 new ArrayList();city1.add(南京市);city1.add(扬州市);city1.add(苏州市);city1.add(无锡市);city1.add(常州市);ArrayListString city2 new ArrayList();city2.add(武汉市);city2.add(孝感市);city2.add(十堰市);city2.add(宜昌市);city2.add(鄂州市);ArrayListString city3 new ArrayList();city3.add(石家庄市);city3.add(唐山市);city3.add(邢台市);city3.add(保定市);city3.add(张家口市);//把省份和多个市添加到map集合hm.put(江苏省,city1);hm.put(湖北省,city2);hm.put(河北省,city3);SetMap.EntryString, ArrayListString entries hm.entrySet();for (Map.EntryString, ArrayListString entry : entries) {//entry依次表示每一个键值对对象String key entry.getKey();ArrayListString value entry.getValue();StringJoiner sj new StringJoiner(, ,,);for (String city : value) {sj.add(city);}System.out.println(key sj);}} }参考黑马程序员相关视频与笔记、【查漏补缺】Java 集合详解
http://www.dnsts.com.cn/news/50576.html

相关文章:

  • 滁州市建设局网站全网营销推广系统
  • mysql 收费 网站建设南京网站设计外包
  • 怎么增加网站流量搜索引擎优化的常用方法
  • 做公装的什么网站好html5怎么做简单的网站
  • 西安建设门户网站wordpress省市联动
  • ps做网站一般用多大字体一个小网站一般多少钱
  • 网站会员推广功能西安网站seo外包
  • 专业网站制作的公司免费查企业老板的软件
  • 韩国企业网站模板下载网站logo怎么做动态
  • 邢台网站改版怎么开发百度站长平台账号购买
  • 网站建设企业资质等级服务器镜像wordpress
  • 秋佐科技公司网站wordpress 侧滑
  • 凌晨三点看的片免费知道一个网站怎么知道是谁做的百度优化
  • 企业网站建设建议wordpress 微信授权
  • 微信公众号怎么做链接网站吗网站怎样制作吸引人
  • 如果提高网站邯郸网站设计哪家专业
  • 随州网站建设优化推广渠道外贸公司域名哪个网站申请比较好
  • 宁波自己建网站平度做网站公司
  • 做的网站在百度找不到了wordpress改微博系统
  • 网站建设分为几个时期链接交换公司
  • 邹平建设项目网站公示wordpress滑动菜单
  • 怎么做网站备案连接免费高清视频
  • 做网站的程序重庆网站推广团队
  • 铝合金做网站photoshop软件教学
  • 社交网站建设需求分析浙江网站建设制作流程
  • 上海建设门户网站自己怎么优化我网站关键词
  • 如何在自己的服务器上做网站建设网站最好的
  • 网站开发流程怎么写百度蜘蛛对视频网站的抓取
  • 公司网站的建设流程wordpress主题不显示小工具
  • 调用别人网站注册表单如何开发高端市场