自己建设的网站如何优化购物平台软件开发
web/
2025/9/29 6:00:10/
文章来源:
自己建设的网站如何优化,购物平台软件开发,开发一个彩票网站多少钱,wordpress+分享后下载文章目录 7.1 常用方法 参考操作数组的工具类#xff1a;Arrays#xff0c;Collections 是一个操作 Set、List 和 Map 等集合的工具类。 7.1 常用方法
Collections 中提供了一系列静态的方法对集合元素进行排序、查询和修改等操作#xff0c;还提供了对集合对象设置不可变、… 文章目录 7.1 常用方法 参考操作数组的工具类ArraysCollections 是一个操作 Set、List 和 Map 等集合的工具类。 7.1 常用方法
Collections 中提供了一系列静态的方法对集合元素进行排序、查询和修改等操作还提供了对集合对象设置不可变、对集合对象实现同步控制等方法均为static方法
排序操作
reverse(List)反转 List 中元素的顺序shuffle(List)对 List 集合元素进行随机排序sort(List)根据元素的自然顺序对指定 List 集合元素按升序排序sort(ListComparator)根据指定的 Comparator 产生的顺序对 List 集合元素进行排序swap(Listint int)将指定 list 集合中的 i 处元素和 j 处元素进行交换
查找
Object max(Collection)根据元素的自然顺序返回给定集合中的最大元素Object max(CollectionComparator)根据 Comparator 指定的顺序返回给定集合中的最大元素Object min(Collection)根据元素的自然顺序返回给定集合中的最小元素Object min(CollectionComparator)根据 Comparator 指定的顺序返回给定集合中的最小元素int binarySearch(List list,T key)在List集合中查找某个元素的下标但是List的元素必须是T或T的子类对象而且必须是可比较大小的即支持自然排序的。而且集合也事先必须是有序的否则结果不确定。int binarySearch(List list,T key,Comparator c)在List集合中查找某个元素的下标但是List的元素必须是T或T的子类对象而且集合也事先必须是按照c比较器规则进行排序过的否则结果不确定。int frequency(Collection cObject o)返回指定集合中指定元素的出现次数
复制、替换
void copy(List dest,List src)将src中的内容复制到dest中boolean replaceAll(List listObject oldValObject newVal)使用新值替换 List 对象的所有旧值提供了多个unmodifiableXxx()方法该方法返回指定 Xxx的不可修改的视图。
添加
boolean addAll(Collection c,T... elements)将所有指定元素添加到指定 collection 中。
同步
Collections 类中提供了多个 synchronizedXxx() 方法该方法可使将指定集合包装成线程同步的集合从而可以解决多线程并发访问集合时的线程安全问题 import org.junit.Test;
import java.text.Collator;
import java.util.*;public class TestCollections {Testpublic void test01(){/*public static T boolean addAll(Collection? super T c,T... elements)将所有指定元素添加到指定 collection 中。Collection的集合的元素类型必须T类型*/CollectionObject coll new ArrayList();Collections.addAll(coll, hello,java); // 调用方法添加Collections.addAll(coll, 1,2,3,4);System.out.println(coll);CollectionString coll2 new ArrayList();Collections.addAll(coll2, hello,java);//Collections.addAll(coll2, 1,2,3,4);//String和Integer之间没有父子类关系,这个要报错 类型错误}Testpublic void test02(){/** public static T extends Object Comparable? super T T max(Collection? extends T coll)* 在coll集合中找出最大的元素集合中的对象必须是T或T的子类对象而且支持自然排序** public static T T max(Collection? extends T coll,Comparator? super T comp)* 在coll集合中找出最大的元素集合中的对象必须是T或T的子类对象按照比较器comp找出最大者**/ListMan list new ArrayList();list.add(new Man(张三,23));list.add(new Man(李四,24));list.add(new Man(王五,25));/** Man max Collections.max(list);//要求Man实现Comparable接口或者父类实现* System.out.println(max);*/Man max Collections.max(list, new ComparatorMan() {Overridepublic int compare(Man o1, Man o2) {return o2.getAge()-o2.getAge();}});System.out.println(max);}Testpublic void test03(){/** public static void reverse(List? list)* 反转指定列表List中元素的顺序。*/ListString list new ArrayList();Collections.addAll(list,hello,java,world);System.out.println(list); // [hello, java, world]Collections.reverse(list);System.out.println(list); // [world, java, hello]}Testpublic void test04(){/** public static void shuffle(List? list)* List 集合元素进行随机排序类似洗牌打乱顺序*/ListString list new ArrayList();Collections.addAll(list,hello,java,world);Collections.shuffle(list); // 随机ArrayListSystem.out.println(list);}Testpublic void test05() {/** public static T extends Comparable? super T void sort(ListT list)* 根据元素的自然顺序对指定 List 集合元素按升序排序** public static T void sort(ListT list,Comparator? super T c)* 根据指定的 Comparator 产生的顺序对 List 集合元素进行排序*/ListMan list new ArrayList();list.add(new Man(张三,23));list.add(new Man(李四,24));list.add(new Man(王五,25));Collections.sort(list); // 这个要在ManSystem.out.println(list);// 自己定义排序Collections.sort(list, new ComparatorMan() {Overridepublic int compare(Man o1, Man o2) {return Collator.getInstance(Locale.CHINA).compare(o1.getName(),o2.getName());}});System.out.println(list);}Testpublic void test06(){/** public static void swap(List? list,int i,int j)* 将指定 list 集合中的 i 处元素和 j 处元素进行交换*/ListString list new ArrayList();Collections.addAll(list,hello,java,world);Collections.swap(list,0,2);System.out.println(list);}Testpublic void test07(){/** public static int frequency(Collection? c,Object o)* 返回指定集合中指定元素的出现次数*/ListString list new ArrayList();Collections.addAll(list,hello,java,world,hello,hello);int count Collections.frequency(list, hello);System.out.println(count count);}Testpublic void test08(){/** public static T void copy(List? super T dest,List? extends T src)* 将src中的内容复制到dest中*/ListInteger list new ArrayList();for(int i1; i5; i){//1-5list.add(i);}ListInteger list2 new ArrayList();for(int i11; i13; i){//11-13list2.add(i);}Collections.copy(list, list2);System.out.println(list);ListInteger list3 new ArrayList();for(int i11; i20; i){//11-20list3.add(i);}//java.lang.IndexOutOfBoundsException: Source does not fit in dest//Collections.copy(list, list3);//System.out.println(list);// copy 方法必须前面的数组size()必须比后面的大 /test 10 有描述}Testpublic void test09(){/** public static T boolean replaceAll(ListT listT oldValT newVal)* 使用新值替换 List 对象的所有旧值*/ListString list new ArrayList();Collections.addAll(list,hello,java,world,hello,hello);Collections.replaceAll(list, hello,song);System.out.println(list);}Testpublic void test10(){List src Arrays.asList(45, 43, 65, 6, 43, 2, 32, 45, 56, 34, 23);//void copy(List dest,List src)将src中的内容复制到dest中//错误的写法
// List dest new ArrayList();//正确的写法List dest Arrays.asList(new Object[src.size()]);Collections.copy(dest,src);System.out.println(dest);}Testpublic void test11(){//提供了多个unmodifiableXxx()方法该方法返回指定 Xxx的不可修改的视图。List list1 new ArrayList();//list1可以写入数据list1.add(34);list1.add(12);list1.add(45);List list2 Collections.unmodifiableList(list1);//此时的list2只能读不能写list2.add(AA);//不能写System.out.println(list2.get(0));//34}Testpublic void test12(){//Collections 类中提供了多个 synchronizedXxx() 方法List list1 new ArrayList();//返回的list2就是线程安全的List list2 Collections.synchronizedList(list1);list2.add(123);HashMap map1 new HashMap();//返回的map2就是线程安全的Map map2 Collections.synchronizedMap(map1);}
}class Man implements Comparable {private String name;private int age;public Man(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 Man{ name name \ , age age };}Overridepublic boolean equals(Object o) {if (this o) return true;if (o null || getClass() ! o.getClass()) return false;Man man (Man) o;return age man.age Objects.equals(name, man.name);}Overridepublic int hashCode() {return Objects.hash(name, age);}Overridepublic int compareTo(Object o) {if(this o){return 0;}if(o instanceof Man){Man man (Man) o;int value this.age - man.age;if(value ! 0){return value;}return - this.name.compareTo(man.name);}throw new RuntimeException(输入的类型不匹配!);}
}
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/83739.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!