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

网站页面设计内容百度一下下载

网站页面设计内容,百度一下下载,墨客网站建设,网站还没建设好可以备案吗DAY7.2 Java核心基础 想学习Collection、list、ArrayList、Set、HashSet部分的小伙伴可以转到 7.1集合框架、Collection、list、ArrayList、Set、HashSet和LinkedHashSet、判断两个对象是否相等文章查看 set集合 在set集合中,处理LinkedHashSet是有序的&#xf…

DAY7.2 Java核心基础

想学习Collection、list、ArrayList、Set、HashSet部分的小伙伴可以转到
7.1集合框架、Collection、list、ArrayList、Set、HashSet和LinkedHashSet、判断两个对象是否相等文章查看
在这里插入图片描述

set集合

在set集合中,处理LinkedHashSet是有序的,TreeSet的存放也是有序的,但是二者之间的有序亦有区

LinkedHashSet:元素的存储顺序和遍历顺序是一致的

TreeSet:内部元素会按照升序的方法进行排序,无论存入元素的顺序是什么,都会按照升序进行输出

public static void main(String[] args) {TreeSet<Integer> treeSet = new TreeSet<>();treeSet.add(1);treeSet.add(5);treeSet.add(3);treeSet.add(2);treeSet.add(1);treeSet.add(9);Iterator<Integer> iterator = treeSet.iterator();while (iterator.hasNext()){System.out.println(iterator.next());}System.out.println("***********************");LinkedHashSet<Integer> linkedHashSet = new LinkedHashSet<>();linkedHashSet.add(98);linkedHashSet.add(2);linkedHashSet.add(3);linkedHashSet.add(2);linkedHashSet.add(11);linkedHashSet.add(1);for (Integer integer : linkedHashSet) {System.out.println(integer);}
}

image-20250308171510286

可以看见treeSet自动排序了,而linkedHashSet按照添加顺序输出的,但是他们两个都去除了重复元素,验证了Set集合元素的唯一性

TreeSet 内部会自动按照升序对元素进行排列,所以添加到 TreeSet 集合中的元素必须具备排序的功能,无法排序的对象无法添加到 TreeSet 中的。

如果传递的是一个不能比较的对象:

public class test {public static void main(String[] args) {TreeSet treeSet = new TreeSet<>();treeSet.add(new A(6));treeSet.add(new A(2));treeSet.add(new A(3));treeSet.add(new A(4));}
}
class A{private int num;public A(int num) {this.num = num;}
}

image-20250308171942770

就会出现异常显示无法是实现比较的接口

Comparable 是排序接口,实习了该接口的类就具备了排序的功能,对象就可以进行排序

如果要比较我们就需要在A对象类里面实现Comparable 接口

class A implements Comparable{private int num;public A(int num) {this.num = num;}/*** A.compareTo(B)* 返回值:* 1表示A大于B* 0表示A等于B* -1表示A小于B* @param o* @return*/@Overridepublic int compareTo(Object o) {if (o instanceof A){A a = (A) o;return this.num - a.num;}return 0;}@Overridepublic String toString() {return "A{" +"num=" + num +'}';}
}
public class test {public static void main(String[] args) {TreeSet treeSet = new TreeSet<>();treeSet.add(new A(6));treeSet.add(new A(2));treeSet.add(new A(3));treeSet.add(new A(4));Iterator iterator = treeSet.iterator();while (iterator.hasNext()){System.out.println(iterator.next());}}
}

这时候输出:

image-20250308172236488

完美解决!

Map 接口

Set List 都是Collenction的子接口,Map接口是与Collenction完全独立的一个体系

Set 、List、Collenction都是只能操作一个元素,而Map可以操作一对数据,以Key-Value形式存在

Map接口常见方法

方法描述
int size()获取集合长度
boolean isEmpty()判断集合是否为空
boolean containsKey(Object key)判断集合中是否存在某个 key 值
boolean containsValue(Object key)判断集合中是否存在某个 value 值
V get(Object key)取出集合中 key 对应的 value
V put(K key,V value)向集合中添加一组 key-value 的元素
V remove(Object key)删除集合中 key 对应的 value
void clear()清除集合中的所有元素
Set keySet()取出集合中所有的 key,返回一个 Set 集合
Collection values()取出集合中所有的 value,返回一个 Collection 集合
Set entrySet()将 Map 对象转为 Set 对象
int hashCode()获取集合的散列值
boolean equals(Object o)比较两个集合是否相等

Map是一个接口,无法被实例化创建对象,而需要通过实现类来创建对象

HashMap:存储无序,key不能重复,值可以重复

TreeMap:存储有序,key不能重复,值可以重复

测试方法:

public class test {public static void main(String[] args) {testMapMethods();}public static void testMapMethods() {Map<String, Integer> map = new HashMap<>();// 测试 put 方法map.put("a", 1);map.put("b", 2);map.put("c", 3);System.out.println("插入元素后: " + map);// 测试 size 方法System.out.println("集合大小: " + map.size());// 测试 isEmpty 方法System.out.println("集合是否为空: " + map.isEmpty());// 测试 containsKey 方法System.out.println("是否包含键 'a': " + map.containsKey("a"));System.out.println("是否包含键 'd': " + map.containsKey("d"));// 测试 containsValue 方法System.out.println("是否包含值 1: " + map.containsValue(1));System.out.println("是否包含值 4: " + map.containsValue(4));// 测试 get 方法System.out.println("键 'a' 对应的值: " + map.get("a"));System.out.println("键 'd' 对应的值: " + map.get("d"));// 测试 keySet 方法Set<String> keySet = map.keySet();System.out.println("所有键的集合: " + keySet);// 测试 values 方法System.out.println("所有值的集合: " + map.values());// 测试 hashCode 方法System.out.println("集合的哈希码: " + map.hashCode());// 测试 remove 方法System.out.println("删除键 'a' 对应的值: " + map.remove("a"));System.out.println("删除后: " + map);// 测试 replace 方法System.out.println("将键 'b' 的值替换为 4: " + map.replace("b", 4));System.out.println("替换后: " + map);// 测试 clear 方法map.clear();System.out.println("清空后: " + map);System.out.println("清空后是否为空: " + map.isEmpty());}
}

输出:

image-20250308173640595

朋友们可以自己取尝试调用一下这些方法

TreeMap和HashMap差不多,但是这个会根据key排序

public static void main(String[] args) {TreeMap<Integer, String> map = new TreeMap<>();map.put(3, "Three");map.put(1, "One");map.put(2, "Two");map.put(2, "Two");System.out.println(map);
}

测试:

image-20250308230937789

Collections 工具类

集合除了可以存储数据之外,还提供了很多方法来对数据进行操作,但是这些方法都有其局限性,实际操作起来不是很方便,JDK 提供了一个工具类 Collections,专门用来操作集合,添加元素、元素排序、替换元素。

Collections 常用方法

方法描述
sort根据集合泛型对应的类实现的 Comparable 接口对集合进行排序
sort根据 Comparator 接口对集合进行排序
binarySearch查找元素在集合中的下标,要求集合元素必须是升序排列
get根据下标找到集合中的元素
reverse对集合元素的顺序进行反转
swap交换集合中指定位置的两个元素
fill将集合中的所有元素进行替换
min返回集合中最小的元素
max返回集合中最大的元素
replaceAll将集合中的所有元素进行替换
addAll向集合中添加元素

部分代码示例:

sort方法:

// 定义一个实现了Comparable接口的类
class Person implements Comparable<Person> {private String name;private int age;public Person(String name, int age) {this.name = name;this.age = age;}@Overridepublic int compareTo(Person p) {return Integer.compare(this.age, p.age);}@Overridepublic String toString() {return "Person{name='" + name + "', age=" + age + "}";}
}public class TestSort {public static void main(String[] args) {List<Person> list = new ArrayList<>();list.add(new Person("Alice", 25));list.add(new Person("Bob", 30));list.add(new Person("Charlie", 20));Collections.sort(list); // 根据age排序System.out.println(list);}
}

输出:

image-20250308232212807

实现compareTo方法,然后可以使得按照age排序

binarySearch (查找元素下标,要求集合升序排列)

public static void main(String[] args) {List<Integer> list = Arrays.asList(1, 3, 5, 7, 9);int index = Collections.binarySearch(list, 5); // 返回元素5的索引System.out.println("Index of 5: " + index);
}

输出:

image-20250308232348783

get (根据下标找到集合中的元素)

public static void main(String[] args) {List<String> list = Arrays.asList("apple", "orange", "banana");String element = list.get(1); // 获取第二个元素System.out.println("Element at index 1: " + element);
}

reverse (反转集合顺序)

public static void main(String[] args) {List<String> list = Arrays.asList("apple", "orange", "banana");Collections.reverse(list);System.out.println("Reversed list: " + list);
}

image-20250308232514150

swap (交换指定位置的两个元素)

public static void main(String[] args) {List<String> list = Arrays.asList("apple", "orange", "banana");Collections.swap(list, 0, 2); // 交换第一个和第三个元素System.out.println("Swapped list: " + list);
}

image-20250308232546509

fill (将集合中的所有元素替换为指定值)

public static void main(String[] args) {List<String> list = new ArrayList<>(Arrays.asList("apple", "orange", "banana"));Collections.fill(list, "fruit");System.out.println("Filled list: " + list);
}

image-20250308232612322

min (返回集合中最小的元素)、max (返回集合中最大的元素)

public static void main(String[] args) {List<Integer> list = Arrays.asList(5, 3, 8, 1, 9);Integer max = Collections.max(list);Integer min = Collections.min(list);System.out.println("Maximum value: " + max);System.out.println("Minimum value: " + min);
}

image-20250308232728922

replaceAll (将集合中的所有元素替换为指定值)

public static void main(String[] args) {List<String> list = Arrays.asList("apple", "orange", "banana");Collections.replaceAll(list, "apple", "fruit");System.out.println("Replaced list: " + list);
}

image-20250308232800876

addAll (向集合中添加所有指定元素)

public static void main(String[] args) {List<String> list = new ArrayList<>();List<String> toAdd = Arrays.asList("apple", "orange", "banana");Collections.addAll(list, "grape", "peach");// 或者使用addAll方法list.addAll(toAdd);System.out.println("Added list: " + list);
}

image-20250308232830429


文章转载自:
http://hoodlum.c7625.cn
http://massiness.c7625.cn
http://devolve.c7625.cn
http://shwa.c7625.cn
http://frontogenesis.c7625.cn
http://triphibious.c7625.cn
http://seizing.c7625.cn
http://taal.c7625.cn
http://carambola.c7625.cn
http://infradian.c7625.cn
http://marxize.c7625.cn
http://eddic.c7625.cn
http://subpolar.c7625.cn
http://preponderance.c7625.cn
http://rufus.c7625.cn
http://calm.c7625.cn
http://devotionally.c7625.cn
http://cautioner.c7625.cn
http://revery.c7625.cn
http://vidar.c7625.cn
http://unpurposed.c7625.cn
http://organ.c7625.cn
http://ungrounded.c7625.cn
http://sloganeer.c7625.cn
http://snovian.c7625.cn
http://selsyn.c7625.cn
http://flank.c7625.cn
http://sothis.c7625.cn
http://varec.c7625.cn
http://lantsang.c7625.cn
http://ferrate.c7625.cn
http://vaunting.c7625.cn
http://fireflood.c7625.cn
http://nickelize.c7625.cn
http://lefty.c7625.cn
http://roemer.c7625.cn
http://cellobiose.c7625.cn
http://expendable.c7625.cn
http://streptococci.c7625.cn
http://tempestuously.c7625.cn
http://accrue.c7625.cn
http://nordstrandite.c7625.cn
http://dearly.c7625.cn
http://tartary.c7625.cn
http://varices.c7625.cn
http://bigaroon.c7625.cn
http://goliardery.c7625.cn
http://arrestor.c7625.cn
http://telescopical.c7625.cn
http://unmechanical.c7625.cn
http://rectify.c7625.cn
http://margaritaceous.c7625.cn
http://tradeswoman.c7625.cn
http://groveling.c7625.cn
http://refreeze.c7625.cn
http://blackfoot.c7625.cn
http://standoffish.c7625.cn
http://hock.c7625.cn
http://savoury.c7625.cn
http://buckeroo.c7625.cn
http://sparkler.c7625.cn
http://archoplasm.c7625.cn
http://realia.c7625.cn
http://theatricalize.c7625.cn
http://aerobics.c7625.cn
http://soerakarta.c7625.cn
http://audible.c7625.cn
http://semirevolution.c7625.cn
http://broadcaster.c7625.cn
http://counterchange.c7625.cn
http://debriefing.c7625.cn
http://interceptive.c7625.cn
http://biopack.c7625.cn
http://continuity.c7625.cn
http://carpolite.c7625.cn
http://scotophilic.c7625.cn
http://clownade.c7625.cn
http://heortology.c7625.cn
http://riometer.c7625.cn
http://dysgraphia.c7625.cn
http://paravion.c7625.cn
http://diplopia.c7625.cn
http://eudaimonism.c7625.cn
http://terry.c7625.cn
http://concordia.c7625.cn
http://military.c7625.cn
http://perplexed.c7625.cn
http://lecture.c7625.cn
http://unurged.c7625.cn
http://cymagraph.c7625.cn
http://max.c7625.cn
http://intropunitive.c7625.cn
http://viseite.c7625.cn
http://header.c7625.cn
http://disconfirm.c7625.cn
http://inauthentic.c7625.cn
http://floridly.c7625.cn
http://apprehensively.c7625.cn
http://glandes.c7625.cn
http://macrostomia.c7625.cn
http://www.zhongyajixie.com/news/76652.html

相关文章:

  • 如何做网站跳转登入seo专员是干嘛的
  • 动易网站 首页模板修改seo排名点击软件推荐
  • 购物网站,购物车界面如何做网络营销的未来发展趋势论文
  • 网站建设招标需求外链推广平台
  • 阿里云做视频网站犯法吗宁波seo外包服务
  • 一个企业网站文章多少适合百度站长平台官网登录入口
  • 南宁网站排名外包湖南seo推广多少钱
  • 国外网站建设的研究现状网络培训中心
  • 独立网站b2c深圳关键词首页排名
  • 淘宝网上做美国签证的网站可靠吗最新疫情19个城市封城
  • 做网站的需求是吗网页制作网站
  • 网站设计需要什么网络营销的推广方法
  • 专业做律师网站的公司吗seo 工具推荐
  • 固原网络推广东莞优化网站关键词优化
  • 制作公司网站设计要求谷歌关键词搜索量数据查询
  • h5做网站什么软件定向推广
  • 上海网站建设内容更新深圳网络推广培训机构
  • 定制一个高端网站深圳百度推广seo公司
  • 做网站py和php百度网络营销中心官网
  • 万维网网站注册百度账号安全中心
  • 兼职做Ppt代抄论文的网站泽成seo网站排名
  • 做物流网站的多少钱职业培训机构有哪些
  • 培训行业网站建设是什么色盲测试图片60张
  • 做百度网站需要什么条件网站搜索查询
  • 做医疗护具网站app注册推广团队
  • .tel域名不可以做网站域名吗事件营销
  • 武功做网站武汉seo招聘信息
  • 淄博专业网站建设价格营销自动化工具
  • 网站成本信息流优化师
  • 贵州app开发公司网站关键字优化