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

福田做网站怎么样专业建站公司收费标准

福田做网站怎么样,专业建站公司收费标准,湖南网站建站系统平台,怎样在百度上发布信息文章目录 Collection接口继承树Collection接口及方法判断删除其它 Iterator(迭代器)接口迭代器的执行原理 foreach循环Collection子接口1#xff1a;ListList接口特点List接口方法List接口主要实现类#xff1a;ArrayListList的实现类之二#xff1a;LinkedListList的实现类… 文章目录 Collection接口继承树Collection接口及方法判断删除其它 Iterator(迭代器)接口迭代器的执行原理 foreach循环Collection子接口1ListList接口特点List接口方法List接口主要实现类ArrayListList的实现类之二LinkedListList的实现类之三Vector(老了) Collection子接口2SetSet接口概述Set主要实现类HashSet**HashSet概述**HashSet中添加元素的过程重写 hashCode() 方法的基本原则重写equals()方法的基本原则练习 Set实现类之二LinkedHashSetSet实现类之三TreeSet1 TreeSet概述 Collection接口继承树 Collection接口及方法 JDK不提供此接口的任何直接实现而是提供更具体的子接口如Set和List去实现。Collection 接口是 List和Set接口的父接口该接口里定义的方法既可用于操作 Set 集合也可用于操作 List 集合。方法如下添加: 1add(E obj)添加元素对象到当前集合中 2addAll(Collection other)添加other集合中的所有元素对象到当前集合中即this this ∪ other 注意add和addAll的区别 import org.junit.Test;import java.util.ArrayList; import java.util.Collection;public class TestCollectionAdd {Testpublic void testAdd(){//ArrayList是Collection的子接口List的实现类之一。Collection coll new ArrayList();// 添加coll.add(小李广);coll.add(扫地僧);coll.add(石破天);System.out.println(coll);}Testpublic void testAddAll(){Collection c1 new ArrayList();c1.add(1);c1.add(2);System.out.println(c1集合元素的个数 c1.size());//2 元素的个数System.out.println(c1 c1);Collection c2 new ArrayList();c2.add(1);c2.add(2);System.out.println(c2集合元素的个数 c2.size());//2System.out.println(c2 c2);Collection other new ArrayList();other.add(1);other.add(2);other.add(3);System.out.println(other集合元素的个数 other.size());//3System.out.println(other other);System.out.println();c1.addAll(other);System.out.println(c1集合元素的个数 c1.size());//5System.out.println(c1.addAll(other) c1);c2.add(other);System.out.println(c2集合元素的个数 c2.size());//3System.out.println(c2.add(other) c2);} }判断 3int size()获取当前集合中实际存储的元素个数 4boolean isEmpty()判断当前集合是否为空集合 5boolean contains(Object obj)判断当前集合中是否存在一个与obj对象equals返回true的元素 6boolean containsAll(Collection coll)判断coll集合中的元素是否在当前集合中都存在。即coll集合是否是当前集合的“子集” 7boolean equals(Object obj)判断当前集合与obj是否相等 import org.junit.Test;import java.util.ArrayList; import java.util.Collection;public class TestCollectionContains {Testpublic void test01() {Collection coll new ArrayList();System.out.println(coll在添加元素之前isEmpty coll.isEmpty());coll.add(小李广);coll.add(扫地僧);coll.add(石破天);coll.add(佛地魔);System.out.println(coll的元素个数 coll.size());System.out.println(coll在添加元素之后isEmpty coll.isEmpty());}Testpublic void test02() {Collection coll new ArrayList();coll.add(小李广);coll.add(扫地僧);coll.add(石破天);coll.add(佛地魔);System.out.println(coll coll);System.out.println(coll是否包含“小李广” coll.contains(小李广));System.out.println(coll是否包含“小爬虫” coll.contains(小爬虫));Collection other new ArrayList()other.add(小李广);other.add(扫地僧);other.add(小爬虫);System.out.println(other other);System.out.println(coll.containsAll(other) coll.containsAll(other));System.out.println(coll.contains(other));}Testpublic void test03(){Collection c1 new ArrayList();c1.add(1);c1.add(2);System.out.println(c1集合元素的个数 c1.size());//2System.out.println(c1 c1);Collection c2 new ArrayList();c2.add(1);c2.add(2);System.out.println(c2集合元素的个数 c2.size());//2System.out.println(c2 c2);Collection other new ArrayList();other.add(1);other.add(2);other.add(3);System.out.println(other集合元素的个数 other.size());//3System.out.println(other other);System.out.println();c1.addAll(other);System.out.println(c1集合元素的个数 c1.size());//5System.out.println(c1.addAll(other) c1);System.out.println(c1.contains(other) c1.contains(other));System.out.println(c1.containsAll(other) c1.containsAll(other));System.out.println();c2.add(other);System.out.println(c2集合元素的个数 c2.size());System.out.println(c2.add(other) c2);System.out.println(c2.contains(other) c2.contains(other));System.out.println(c2.containsAll(other) c2.containsAll(other));}}删除 8void clear()清空集合元素 9 boolean remove(Object obj) 从当前集合中删除第一个找到的与obj对象equals返回true的元素。 10boolean removeAll(Collection coll)从当前集合中删除所有与coll集合中相同的元素。即this this - this ∩ coll 11boolean retainAll(Collection coll)从当前集合中删除两个集合中不同的元素使得当前集合仅保留与coll集合中的元素相同的元素即当前集合中仅保留两个集合的交集即this this ∩ coll 注意几种删除方法的区别 import org.junit.Test;import java.util.ArrayList; import java.util.Collection;public class TestCollectionRemove {Testpublic void test01(){Collection coll new ArrayList();coll.add(小李广);coll.add(扫地僧);coll.add(石破天);coll.add(佛地魔);System.out.println(coll coll); //coll [小李广, 扫地僧, 石破天, 佛地魔]coll.remove(小李广);System.out.println(删除元素\小李广\之后coll coll); //删除元素小李广之后coll [扫地僧, 石破天, 佛地魔]coll.clear();System.out.println(coll清空之后coll coll);// coll清空之后coll []}Testpublic void test02() {Collection coll new ArrayList();coll.add(小李广);coll.add(扫地僧);coll.add(石破天);coll.add(佛地魔);System.out.println(coll coll); // coll [小李广, 扫地僧, 石破天, 佛地魔]Collection other new ArrayList();other.add(小李广);other.add(扫地僧);other.add(小爬虫);System.out.println(other other); // other [小李广, 扫地僧, 小爬虫]coll.removeAll(other);System.out.println(coll.removeAll(other)之后coll coll); //coll.removeAll(other)之后coll [石破天, 佛地魔]System.out.println(coll.removeAll(other)之后other other); //coll.removeAll(other)之后other [小李广, 扫地僧, 小爬虫]}Testpublic void test03() {Collection coll new ArrayList();coll.add(小李广);coll.add(扫地僧);coll.add(石破天);coll.add(佛地魔);System.out.println(coll coll); // coll [小李广, 扫地僧, 石破天, 佛地魔]Collection other new ArrayList(); other.add(小李广);other.add(扫地僧);other.add(小伙子);System.out.println(other other); // other [小李广, 扫地僧, 小伙子]coll.retainAll(other); // 取交集System.out.println(coll.retainAll(other)之后coll coll); // coll.retainAll(other)之后coll [小李广, 扫地僧]System.out.println(coll.retainAll(other)之后other other); //coll.retainAll(other)之后other [小李广, 扫地僧, 小伙子]}}其它 12Object[] toArray()返回包含当前集合中所有元素的数组 13hashCode()获取集合对象的哈希值 14iterator()返回迭代器对象用于集合遍历 import org.junit.Test; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection;public class TestCollectionContains {Testpublic void test01() {Collection coll new ArrayList();coll.add(小李广);coll.add(扫地僧);coll.add(石破天);coll.add(佛地魔);//集合转换为数组集合的toArray()方法Object[] objects coll.toArray();System.out.println(用数组返回coll中所有元素 Arrays.toString(objects)); // 用数组返回coll中所有元素[小李广, 扫地僧, 石破天, 佛地魔]//对应的数组转换为集合调用Arrays的asList(Object ...objs)Object[] arr1 new Object[]{123,AA,CC};Collection list Arrays.asList(arr1);System.out.println(list); // [123, AA, CC]} }Iterator(迭代器)接口 在程序开发中经常需要遍历集合中的所有元素。针对这种需求JDK专门提供了一个接口java.util.Iterator。Iterator接口也是Java集合中的一员但它与Collection、Map接口有所不同。 Collection接口与Map接口主要用于存储元素Iterator被称为迭代器接口本身并不提供存储对象的能力主要用于遍历Collection中的元素 Collection接口继承了java.lang.Iterable接口该接口有一个iterator()方法那么所有实现了Collection接口的集合类都有一个iterator()方法用以返回一个实现了Iterator接口的对象。 public Iterator iterator(): 获取集合对应的迭代器用来遍历集合中的元素的。集合对象每次调用iterator()方法都得到一个全新的迭代器对象默认游标都在集合的第一个元素之前。 Iterator接口的常用方法如下 public E next():返回迭代的下一个元素。public boolean hasNext():如果仍有元素可以迭代则返回 true。 注意在调用it.next()方法之前必须要调用it.hasNext()进行检测。若不调用且下一条记录无效直接调用it.next()会抛出NoSuchElementException异常。 举例 import org.junit.Test; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator;public class TestIterator {Testpublic void test01(){Collection coll new ArrayList();coll.add(小李广);coll.add(扫地僧);coll.add(石破天);Iterator iterator coll.iterator();System.out.println(iterator.next()); // 小李广System.out.println(iterator.next()); // 扫地僧System.out.println(iterator.next()); // 石破天System.out.println(iterator.next()); //报NoSuchElementException异常}Testpublic void test02(){Collection coll new ArrayList();coll.add(小李广);coll.add(扫地僧);coll.add(石破天);Iterator iterator coll.iterator();//获取迭代器对象while(iterator.hasNext()) {//判断是否还有元素可迭代System.out.println(iterator.next());//取出下一个元素}/* 输出结果:* 小李广扫地僧石破天* */} }迭代器的执行原理 Iterator迭代器对象在遍历集合时内部采用指针的方式来跟踪集合中的元素接下来通过在这里插入代码片一个图例来演示Iterator对象迭代元素的过程 使用Iterator迭代器删除元素java.util.Iterator迭代器中有一个方法void remove() ; Iterator iter coll.iterator();//回到起点 while(iter.hasNext()){Object obj iter.next();if(obj.equals(Tom)){iter.remove();} }注意- Iterator可以删除集合的元素但是遍历过程中通过迭代器对象的remove方法不是集合对象的remove方法。 - 如果还未调用next()或在上一次调用 next() 方法之后已经调用了 remove() 方法再调用remove()都会报IllegalStateException。- Collection已经有remove(xx)方法了为什么Iterator迭代器还要提供删除方法呢因为迭代器的remove()可以按指定的条件进行删除。import org.junit.Test;import java.util.ArrayList; import java.util.Collection; import java.util.Iterator;public class TestIteratorRemove {Testpublic void test01(){Collection coll new ArrayList();coll.add(1);coll.add(2);coll.add(3);coll.add(4);coll.add(5);coll.add(6);Iterator iterator coll.iterator();while(iterator.hasNext()){Integer element (Integer) iterator.next();if(element % 2 0){iterator.remove();}}System.out.println(coll); // [1, 3, 5]} } foreach循环 foreach循环也称增强for循环是 JDK5.0 中定义的一个高级for循环专门用来遍历数组和集合的。 foreach循环的语法格式 for(元素的数据类型 局部变量 : Collection集合或数组){ //操作局部变量的输出操作 } //这里局部变量就是一个临时变量自己命名就可以举例 import org.junit.Test;import java.util.ArrayList; import java.util.Collection;public class TestForeach {Testpublic void test01(){Collection coll new ArrayList();coll.add(小李广);coll.add(扫地僧);coll.add(石破天);//foreach循环其实就是使用Iterator迭代器来完成元素的遍历的。for (Object o : coll) {System.out.println(o); // 循环遍历之前的ArrayList}}Testpublic void test02(){int[] nums {1,2,3,4,5};for (int num : nums) {System.out.println(num);}System.out.println(-----------------);String[] names {张三,李四,王五};for (String name : names) {System.out.println(name);}} }对于集合的遍历增强for的内部原理其实是个Iterator迭代器。 它用于遍历Collection和数组。通常只进行遍历元素不要在遍历的过程中对集合元素进行增删操作。 Collection子接口1List List接口特点 鉴于Java中数组用来存储数据的局限性我们通常使用java.util.List替代数组 List集合类中元素有序、且可重复集合中的每个元素都有其对应的顺序索引。 举例List集合存储数据就像银行门口客服给每一个来办理业务的客户分配序号第一个来的是“张三”客服给他分配的是0第二个来的是“李四”客服给他分配的1以此类推最后一个序号应该是“总人数-1”。 List接口方法 List除了从Collection集合继承的方法外List 集合里添加了一些根据索引来操作集合元素的方法。 插入元素 void add(int index, Object ele):在index位置插入ele元素boolean addAll(int index, Collection eles):从index位置开始将eles中的所有元素添加进来 获取元素 Object get(int index):获取指定index位置的元素List subList(int fromIndex, int toIndex):返回从fromIndex到toIndex位置的子集合 获取元素索引 int indexOf(Object obj):返回obj在集合中首次出现的位置int lastIndexOf(Object obj):返回obj在当前集合中末次出现的位置 删除和替换元素 Object remove(int index):移除指定index位置的元素并返回此元素 Object set(int index, Object ele):设置指定index位置的元素为ele 举例 import java.util.ArrayList; import java.util.List;public class TestListMethod {public static void main(String[] args) {// 创建List集合对象ListString list new ArrayListString();// 往 尾部添加 指定元素list.add(图图);list.add(小美);list.add(不高兴);System.out.println(list);// add(int index,String s) 往指定位置添加list.add(1,没头脑);System.out.println(list);// String remove(int index) 删除指定位置元素 返回被删除元素// 删除索引位置为2的元素System.out.println(删除索引位置为2的元素);System.out.println(list.remove(2));System.out.println(list);// String set(int index,String s)// 在指定位置 进行 元素替代改// 修改指定位置元素list.set(0, 三毛);System.out.println(list);// String get(int index) 获取指定位置元素// 跟size() 方法一起用 来 遍历的for(int i 0;ilist.size();i){System.out.println(list.get(i));}//还可以使用增强forfor (String string : list) {System.out.println(string);}} }注意在JavaSE中List名称的类型有两个一个是java.util.List集合接口一个是java.awt.List图形界面的组件别导错包了。 List接口主要实现类ArrayList ArrayList 是 List 接口的主要实现类 本质上ArrayList是对象引用的一个”变长”数组 Arrays.asList(…) 方法返回的 List 集合既不是 ArrayList 实例也不是 Vector 实例。 Arrays.asList(…) 返回值是一个固定长度的 List 集合 List的实现类之二LinkedList 对于频繁的插入或删除元素的操作建议使用LinkedList类效率较高。这是由底层采用链表双向链表结构存储数据决定的。 特有方法 void addFirst(Object obj)void addLast(Object obj)Object getFirst()Object getLast()Object removeFirst()Object removeLast() List的实现类之三Vector(老了) Vector 是一个古老的集合JDK1.0就有了。大多数操作与ArrayList相同区别之处在于Vector是线程安全的。在各种List中最好把ArrayList作为默认选择。当插入、删除频繁时使用LinkedListVector总是比ArrayList慢所以尽量避免使用。 特有方法 void addElement(Object obj)void insertElementAt(Object obj,int index)void setElementAt(Object obj,int index)void removeElement(Object obj)void removeAllElements() Test public void testListRemove() {List list new ArrayList();list.add(1);list.add(2);list.add(3);updateList(list);System.out.println(list);//[1,2] }private static void updateList(List list) {list.remove(2); // 移除下标 }Collection子接口2Set Set接口概述 Set接口是Collection的子接口Set接口相较于Collection接口没有提供额外的方法Set 集合不允许包含相同的元素如果试把两个相同的元素加入同一个 Set 集合中则添加操作失败。Set集合支持的遍历方式和Collection集合一样foreach和Iterator。Set的常用实现类有HashSet、TreeSet、LinkedHashSet。 Set主要实现类HashSet HashSet概述 HashSet 是 Set 接口的主要实现类大多数时候使用 Set 集合时都使用这个实现类。 HashSet 按 Hash 算法来存储集合中的元素因此具有很好的存储、查找、删除性能。 HashSet 具有以下特点 不能保证元素的排列顺序HashSet 不是线程安全的集合元素可以是 null HashSet 集合判断两个元素相等的标准两个对象通过 hashCode() 方法得到的哈希值相等并且两个对象的 equals() 方法返回值为true。 对于存放在Set容器中的对象对应的类一定要重写hashCode()和equals(Object obj)方法以实现对象相等规则。即“相等的对象必须具有相等的散列码”。 HashSet集合中元素的无序性不等同于随机性。这里的无序性与元素的添加位置有关。具体来说我们在添加每一个元素到数组中时具体的存储位置是由元素的hashCode()调用后返回的hash值决定的。导致在数组中每个元素不是依次紧密存放的表现出一定的无序性。 HashSet中添加元素的过程 第1步当向 HashSet 集合中存入一个元素时HashSet 会调用该对象的 hashCode() 方法得到该对象的 hashCode值然后根据 hashCode值通过某个散列函数决定该对象在 HashSet 底层数组中的存储位置。 第2步如果要在数组中存储的位置上没有元素则直接添加成功。 第3步如果要在数组中存储的位置上有元素则继续比较 如果两个元素的hashCode值不相等则添加成功如果两个元素的hashCode()值相等则会继续调用equals()方法 如果equals()方法结果为false则添加成功。如果equals()方法结果为true则添加失败。 第2步添加成功元素会保存在底层数组中。 第3步两种添加成功的操作由于该底层数组的位置已经有元素了则会通过链表的方式继续链接存储。 举例 import java.util.Objects;public class MyDate {private int year;private int month;private int day;public MyDate(int year, int month, int day) {this.year year;this.month month;this.day day;}Overridepublic boolean equals(Object o) {if (this o) return true;if (o null || getClass() ! o.getClass()) return false;MyDate myDate (MyDate) o;return year myDate.year month myDate.month day myDate.day;}Overridepublic int hashCode() {return Objects.hash(year, month, day);}Overridepublic String toString() {return MyDate{ year year , month month , day day };} }测试类 import org.junit.Test;import java.util.HashSet;public class TestHashSet {Testpublic void test01(){HashSet set new HashSet();set.add(张三);set.add(张三);set.add(李四);set.add(王五);set.add(王五);set.add(赵六);System.out.println(set set);//不允许重复无序}Testpublic void test02(){HashSet set new HashSet();set.add(new MyDate(2021,1,1));set.add(new MyDate(2021,1,1));set.add(new MyDate(2022,2,4));set.add(new MyDate(2022,2,4));System.out.println(set set);//不允许重复无序// set [MyDate{year2022, month2, day4}, MyDate{year2021, month1, day1}]} }重写 hashCode() 方法的基本原则 在程序运行时同一个对象多次调用 hashCode() 方法应该返回相同的值。当两个对象的 equals() 方法比较返回 true 时这两个对象的 hashCode() 方法的返回值也应相等。对象中用作 equals() 方法比较的 Field都应该用来计算 hashCode 值。 注意如果两个元素的 equals() 方法返回 true但它们的 hashCode() 返回值不相等hashSet 将会把它们存储在不同的位置但依然可以添加成功。 重写equals()方法的基本原则 重写equals方法的时候一般都需要同时复写hashCode方法。通常参与计算hashCode的对象的属性也应该参与到equals()中进行计算。 推荐开发中直接调用Eclipse/IDEA里的快捷键自动重写equals()和hashCode()方法即可。 为什么用Eclipse/IDEA复写hashCode方法有31这个数字 首先选择系数的时候要选择尽量大的系数。因为如果计算出来的hash地址越大所谓的“冲突”就越少查找起来效率也会提高。减少冲突其次31只占用5bits,相乘造成数据溢出的概率较小。再次31可以 由i*31 (i5)-1来表示,现在很多虚拟机里面都有做相关优化。提高算法效率最后31是一个素数素数作用就是如果我用一个数字来乘以这个素数那么最终出来的结果只能被素数本身和被乘数还有1来整除(减少冲突)练习 **练习1**在List内去除重复数字值要求尽量简单 public static List duplicateList(List list) {HashSet set new HashSet();set.addAll(list);return new ArrayList(set); } public static void main(String[] args) {List list new ArrayList();list.add(new Integer(1));list.add(new Integer(2));list.add(new Integer(2));list.add(new Integer(4));list.add(new Integer(4));List list2 duplicateList(list);for (Object integer : list2) {System.out.println(integer);} }**练习2**获取随机数 编写一个程序获取10个1至20的随机数要求随机数不能重复。并把最终的随机数输出到控制台。 public class RandomValueTest {public static void main(String[] args) {HashSet hs new HashSet(); // 创建集合对象Random r new Random();while (hs.size() 10) {int num r.nextInt(20) 1; // 生成1到20的随机数hs.add(num);}for (Integer integer : hs) { // 遍历集合System.out.println(integer); // 打印每一个元素}} } **练习3**去重 public class DistinctTest {public static void main(String[] args) {Scanner sc new Scanner(System.in); // 创建键盘录入对象System.out.println(请输入一行字符串:);String line sc.nextLine(); // 将键盘录入的字符串存储在line中char[] arr line.toCharArray(); // 将字符串转换成字符数组HashSet hs new HashSet(); // 创建HashSet集合对象for (Object c : arr) { // 遍历字符数组hs.add(c); // 将字符数组中的字符添加到集合中}for (Object ch : hs) { // 遍历集合System.out.print(ch);}} }**练习4**面试题 import java.util.HashSet; import java.util.Objects;public class Person {private int num;private String name;Overridepublic String toString() {return Person{ num num , name name \ };}Overridepublic boolean equals(Object o) {System.out.println(进入了equals方法.....);if (this o) return true;if (o null || getClass() ! o.getClass()) return false;Person person (Person) o;return num person.num Objects.equals(name, person.name);}Overridepublic int hashCode() {return Objects.hash(num, name);}public Person(int num, String name) {this.num num;this.name name;}public void setNum(int num) {this.num num;}public void setName(String name) {this.name name;}public int getNum() {return num;}public String getName() {return name;}public static void main(String[] args) {HashSet set new HashSet();Person p1 new Person(1001,AA);Person p2 new Person(1002,BB);set.add(p1);set.add(p2);p1.name CC; System.out.println(现在集合里的元素:set); // 现在集合里的元素:[Person{num1002, nameBB}, Person{num1001, nameCC}] ---修改成功System.out.println(现在的p1p1); // 现在的 p1 也修改成功/** 在equals 方法里面 打印了 东西 但是这里也没有显示equals方法的进入,* */set.remove(p1); // 移除p1 但是移除不了 p1的hashCode() 已经改变了 进不了equal方法了 如果是进入了哈希算法相同的位置 但是之前的哈希值值 1001 AA 之后的是1001CC 所以哈希值不同。System.out.println(set); // [Person{num1002, nameBB}, Person{num1001, nameCC}]} } 类似 import java.util.HashSet; import java.util.Objects;public class Person {private int num;private String name;Overridepublic String toString() {return Person{ num num , name name \ };}Overridepublic boolean equals(Object o) {System.out.println(进入了equals方法.....);if (this o) return true;if (o null || getClass() ! o.getClass()) return false;Person person (Person) o;return num person.num Objects.equals(name, person.name);}Overridepublic int hashCode() {return Objects.hash(num, name);}public Person(int num, String name) {this.num num;this.name name;}public void setNum(int num) {this.num num;}public void setName(String name) {this.name name;}public int getNum() {return num;}public String getName() {return name;}public static void main(String[] args) {HashSet set new HashSet();Person p1 new Person(1001,AA);Person p2 new Person(1002,BB);set.add(p1);set.add(p2);p1.name CC;System.out.println(现在集合里的元素:set); // 现在集合里的元素:[Person{num1002, nameBB}, Person{num1001, nameCC}] ---修改成功System.out.println(现在的p1p1); // 现在的 p1 也修改成功/** 在equals 方法里面 打印了 东西 但是这里也没有显示equals方法的进入,* */set.remove(p1); // 移除p1 但是移除不了 p1的hashCode() 已经改变了 进不了equal方法了,就算是创建了一样的hashCode,进了equal方法也不相等了System.out.println(set); // [Person{num1002, nameBB}, Person{num1001, nameCC}]set.add(new Person(1001,CC));System.out.println(set); // [Person{num1002, nameBB}, Person{num1001, nameCC}, Person{num1001, nameCC}] // 现在居然可以加入相同的对象set.add(new Person(1001,AA));System.out.println(set); // [Person{num1002, nameBB}, Person{num1001, nameCC}, Person{num1001, nameCC}, Person{num1001, nameAA}]//其中Person类中重写了hashCode()和equal()方法} } Set实现类之二LinkedHashSet LinkedHashSet 是 HashSet 的子类不允许集合元素重复。 LinkedHashSet 根据元素的 hashCode 值来决定元素的存储位置但它同时使用双向链表维护元素的次序这使得元素看起来是以添加顺序保存的。 LinkedHashSet插入性能略低于 HashSet但在迭代访问 Set 里的全部元素时有很好的性能。 朴素一点就是按顺序存储 import org.junit.Test;import java.util.LinkedHashSet;public class TestLinkedHashSet {Testpublic void test01(){LinkedHashSet set new LinkedHashSet();set.add(张三);set.add(张三);set.add(李四);set.add(王五);set.add(王五);set.add(赵六);System.out.println(set set);//不允许重复体现添加顺序} }Set实现类之三TreeSet 1 TreeSet概述 TreeSet 是 SortedSet 接口的实现类TreeSet 可以按照添加的元素的指定的属性的大小顺序进行遍历。TreeSet底层使用红黑树结构存储数据新增的方法如下 Comparator comparator()Object first()Object last()Object lower(Object e)Object higher(Object e)SortedSet subSet(fromElement, toElement)SortedSet headSet(toElement)SortedSet tailSet(fromElement) TreeSet特点不允许重复、实现排序自然排序或定制排序TreeSet 两种排序方法自然排序和定制排序。默认情况下TreeSet 采用自然排序。 自然排序TreeSet 会调用集合元素的 compareTo(Object obj) 方法来比较元素之间的大小关系然后将集合元素按升序(默认情况)排列。 如果试图把一个对象添加到 TreeSet 时则该对象的类必须实现 Comparable 接口。实现 Comparable 的类必须实现 compareTo(Object obj) 方法两个对象即通过 compareTo(Object obj) 方法的返回值来比较大小。 定制排序如果元素所属的类没有实现Comparable接口或不希望按照升序(默认情况)的方式排列元素或希望按照其它属性大小进行排序则考虑使用定制排序。定制排序通过Comparator接口来实现。需要重写compare(T o1,T o2)方法。 利用int compare(T o1,T o2)方法比较o1和o2的大小如果方法返回正整数则表示o1大于o2如果返回0表示相等返回负整数表示o1小于o2。要实现定制排序需要将实现Comparator接口的实例作为形参传递给TreeSet的构造器。 因为只有相同类的两个实例才会比较大小所以向 TreeSet 中添加的应该是同一个类的对象。对于 TreeSet 集合而言它判断两个对象是否相等的唯一标准是两个对象通过 compareTo(Object obj) 或compare(Object o1,Object o2)方法比较返回值。返回值为0则认为两个对象相等。 注意它的结构不一样之前那种修改元素之后在添加可能会添加出一样元素的值这个结构不一样要特殊分析。 举例 import org.junit.Test; import java.util.Iterator; import java.util.TreeSet;public class TreeSetTest {/** 自然排序针对String类的对象* */Testpublic void test1(){TreeSet set new TreeSet();set.add(MM);set.add(CC);set.add(AA);set.add(DD);set.add(ZZ);//set.add(123); //报ClassCastException的异常Iterator iterator set.iterator();while(iterator.hasNext()){System.out.println(iterator.next());}}/** 自然排序针对User类的对象* */Testpublic void test2(){TreeSet set new TreeSet();set.add(new User(Tom,12));set.add(new User(Rose,23));set.add(new User(Jerry,2));set.add(new User(Eric,18));set.add(new User(Tommy,44));set.add(new User(Jim,23));set.add(new User(Maria,18));//set.add(Tom);Iterator iterator set.iterator();while(iterator.hasNext()){System.out.println(iterator.next());}System.out.println(set.contains(new User(Jack, 23))); //true} }其中User类定义如下 public class User implements Comparable{String name;int age;public User() {}public User(String name, int age) {this.name name;this.age age;}Overridepublic String toString() {return User{ name name \ , age age };}/*举例按照age从小到大的顺序排列如果age相同则按照name从大到小的顺序排列* */public int compareTo(Object o) {if(this o){return 0;}if(o instanceof User){User user (User)o;int value this.age - user.age;if(value ! 0){return value;}return -this.name.compareTo(user.name);}throw new RuntimeException(输入的类型不匹配);} }/** 定制排序* */ Test public void test3(){//按照User的姓名的从小到大的顺序排列Comparator comparator new Comparator() {Overridepublic int compare(Object o1, Object o2) {if(o1 instanceof User o2 instanceof User){User u1 (User)o1;User u2 (User)o2;return u1.name.compareTo(u2.name);}throw new RuntimeException(输入的类型不匹配);}};TreeSet set new TreeSet(comparator);set.add(new User(Tom,12));set.add(new User(Rose,23));set.add(new User(Jerry,2));set.add(new User(Eric,18));set.add(new User(Tommy,44));set.add(new User(Jim,23));set.add(new User(Maria,18));//set.add(new User(Maria,28));Iterator iterator set.iterator();while(iterator.hasNext()){System.out.println(iterator.next());} }**练习1**在一个List集合中存储了多个无大小顺序并且有重复的字符串定义一个方法让其有序(从小到大排序)并且不能去除重复元素。 public class SortTest {public static void main(String[] args) {ArrayList list new ArrayList();list.add(ccc);list.add(ccc);list.add(aaa);list.add(aaa);list.add(bbb);list.add(ddd);list.add(ddd);sort(list);System.out.println(list);}/** 对集合中的元素排序,并保留重复*/public static void sort(List list) {TreeSet ts new TreeSet(new Comparator() { Overridepublic int compare(Object o1, Object o2) { // 重写compare方法String s1 (String)o1;String s2 (String)o2;int num s1.compareTo(s2); // 比较内容return num 0 ? 1 : num; // 如果内容一样返回一个不为0的数字即可}});ts.addAll(list); // 将list集合中的所有元素添加到ts中list.clear(); // 清空listlist.addAll(ts); // 将ts中排序并保留重复的结果在添加到list中} } **练习2**TreeSet的自然排序和定制排序 定义一个Employee类。 该类包含private成员变量name,age,birthday其中 birthday 为 MyDate 类的对象 并为每一个属性定义 getter, setter 方法 并重写 toString 方法输出 name, age, birthday MyDate类包含: private成员变量year,month,day并为每一个属性定义 getter, setter 方法 创建该类的 5 个对象并把这些对象放入 TreeSet 集合中下一章TreeSet 需使用泛型来定义 分别按以下两种方式对集合中的元素进行排序并遍历输出 1). 使Employee 实现 Comparable 接口并按 name 排序 2). 创建 TreeSet 时传入 Comparator对象按生日日期的先后排序。 代码实现 public class MyDate implements Comparable{private int year;private int month;private int day;public MyDate() {}public MyDate(int year, int month, int day) {this.year year;this.month month;this.day day;}public int getYear() {return year;}public void setYear(int year) {this.year year;}public int getMonth() {return month;}public void setMonth(int month) {this.month month;}public int getDay() {return day;}public void setDay(int day) {this.day day;}Overridepublic String toString() { // return MyDate{ // year year // , month month // , day day // };return year 年 month 月 day 日;}Overridepublic int compareTo(Object o) {if(this o){return 0;}if(o instanceof MyDate){MyDate myDate (MyDate) o;int yearDistance this.getYear() - myDate.getYear();if(yearDistance ! 0){return yearDistance;}int monthDistance this.getMonth() - myDate.getMonth();if(monthDistance ! 0){return monthDistance;}return this.getDay() - myDate.getDay();}throw new RuntimeException(输入的类型不匹配);} }public class Employee implements Comparable{private String name;private int age;private MyDate birthday;public Employee() {}public Employee(String name, int age, MyDate birthday) {this.name name;this.age age;this.birthday birthday;}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 MyDate getBirthday() {return birthday;}public void setBirthday(MyDate birthday) {this.birthday birthday;}Overridepublic String toString() {return Employee{ name name \ , age age \ , birthday birthday };}Overridepublic int compareTo(Object o) {if(o this){return 0;}if(o instanceof Employee){Employee emp (Employee) o;return this.name.compareTo(emp.name);}throw new RuntimeException(传入的类型不匹配);} }public class EmployeeTest {/*自然排序创建该类的 5 个对象并把这些对象放入 TreeSet 集合中* 需求1使Employee 实现 Comparable 接口并按 name 排序* */Testpublic void test1(){TreeSet set new TreeSet();Employee e1 new Employee(Tom,23,new MyDate(1999,7,9));Employee e2 new Employee(Rose,43,new MyDate(1999,7,19));Employee e3 new Employee(Jack,54,new MyDate(1998,12,21));Employee e4 new Employee(Jerry,12,new MyDate(2002,4,21));Employee e5 new Employee(Tony,22,new MyDate(2001,9,12));set.add(e1);set.add(e2);set.add(e3);set.add(e4);set.add(e5);//遍历Iterator iterator set.iterator();while(iterator.hasNext()){System.out.println(iterator.next());}}/** 定制排序* 创建 TreeSet 时传入 Comparator对象按生日日期的先后排序。* */Testpublic void test2(){Comparator comparator new Comparator() {Overridepublic int compare(Object o1, Object o2) {if(o1 instanceof Employee o2 instanceof Employee){Employee e1 (Employee) o1;Employee e2 (Employee) o2;//对比两个employee的生日的大小MyDate birth1 e1.getBirthday();MyDate birth2 e2.getBirthday();//方式1 // int yearDistance birth1.getYear() - birth2.getYear(); // if(yearDistance ! 0){ // return yearDistance; // } // int monthDistance birth1.getMonth() - birth2.getMonth(); // if(monthDistance ! 0){ // return monthDistance; // } // // return birth1.getDay() - birth2.getDay();//方式2return birth1.compareTo(birth2);}throw new RuntimeException(输入的类型不匹配);}};TreeSet set new TreeSet(comparator);Employee e1 new Employee(Tom,23,new MyDate(1999,7,9));Employee e2 new Employee(Rose,43,new MyDate(1999,7,19));Employee e3 new Employee(Jack,54,new MyDate(1998,12,21));Employee e4 new Employee(Jerry,12,new MyDate(2002,4,21));Employee e5 new Employee(Tony,22,new MyDate(2001,9,12));set.add(e1);set.add(e2);set.add(e3);set.add(e4);set.add(e5);//遍历Iterator iterator set.iterator();while(iterator.hasNext()){System.out.println(iterator.next());}} }
http://www.zqtcl.cn/news/466574/

相关文章:

  • 网站内容分析软文范例100字
  • 网站建站策划用vs做网站
  • 如何建自己的网站做农村电子商务的网站有哪些内容
  • 手机销售网站设计怎么推广软件让别人下载
  • 贵州三蒲建设工程有限公司网站莱阳网站制作
  • 外贸买家网站适合初学者模仿的网站
  • 安徽蚌埠怀远县建设局网站米卓网站建设
  • 网站框架怎么建设微信旧版本下载
  • 速贝网站友情链接怎么做企业网站开发的设计流程
  • 网站建设 安庆网站开发免责合同
  • 天津深圳网站开发定制网络工程考研方向
  • 做app网站的公司哪家好济南网站建设市场
  • 自己做网站页面网站国内空间和国外空间
  • 桂城网站制作公司asp.net jsp 网站
  • 太原免费静态网页制作网站如何搭建钓鱼网站
  • 英语门户网站织梦源码修改wordpress登录页面
  • 网络建设和网站建设网站快速收录提交
  • 免费的建设网站软件北京电力交易中心谢开
  • 建设一个网站需要提供什么手续好看的美食网站设计
  • 西宁网站seo公司网站建设和维护释义
  • 建站平台有哪些免费一键搭建网站wordpress ent 主题
  • 国内比较大的源码网站营销型网站与普通网站的区别
  • 眼镜企业网站建设方案广州最新新闻
  • 茶业网站设计方案绍兴网站建设方案托管
  • 怎样免费建设网站网站建设规划书txt微盘
  • 邯郸网站设计培训做网站建设公司crm在线的培训服务
  • 网站建设文化案例萧山网页设计
  • 融安有那几个网站做的比较好的林州网站建设熊掌号
  • 织梦个人博客网站源码深圳华强北鬼市
  • 成都公司建站模板营销策略有哪些方面