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

福州网站建设推广公司建筑工程网站模板

福州网站建设推广公司,建筑工程网站模板,php网页游戏源码,淘宝网站开发用到哪些技术List 转为 MapString, Entity 要将 ListEntity 转换为 MapString, Entity#xff0c;你需要指定一个属性作为 Map 的键#xff0c;然后将 List 中的每个实体对象的该属性值作为键#xff0c;实体对象本身作为值放入 Map 中。以下是一个示例代码String, Entity 要将 ListEntity 转换为 MapString, Entity你需要指定一个属性作为 Map 的键然后将 List 中的每个实体对象的该属性值作为键实体对象本身作为值放入 Map 中。以下是一个示例代码 假设你有一个名为 Entity 的实体类其中有一个属性名为 id你想将 ListEntity 根据 id 属性转换为 MapString, Entity import java.util.List; import java.util.Map; import java.util.stream.Collectors;public class Main {public static void main(String[] args) {ListEntity entityList ...; // 获取入参 List// 将 List 转换为 MapMapString, Entity entityMap entityList.stream().collect(Collectors.toMap(Entity::getId, entity - entity));// 现在你可以使用 entityMap 了}static class Entity {private String id;// 其他属性和方法包括 getter 和 setterpublic String getId() {return id;}public void setId(String id) {this.id id;}} }在上面的示例中我们使用了 Java 8 的 Stream API通过 Collectors.toMap() 方法将 ListEntity 转换为 MapString, Entity。在 toMap() 方法中第一个参数 Entity::getId 指定了将实体对象的 id 属性作为 Map 的键第二个参数 entity - entity 指定了将实体对象本身作为 Map 的值。 MapString, Entity转为List 要将 MapString, Entity 转换为 ListEntity你可以使用 Map 的 values() 方法获取所有的实体对象然后将它们放入一个 List 中。以下是一个示例代码 import java.util.List; import java.util.Map; import java.util.stream.Collectors;public class Main {public static void main(String[] args) {MapString, Entity entityMap ...; // 获取入参 Map// 将 Map 转换为 ListListEntity entityList entityMap.values().stream().collect(Collectors.toList());// 现在你可以使用 entityList 了}static class Entity {// 实体类的属性和方法} }在上面的示例中我们使用了 Java 8 的 Stream API通过 Collectors.toList() 方法将 Map 的值集合转换为 List。 entityMap.values().stream() 获取 Map 的值集合的 Stream 流然后通过 collect(Collectors.toList()) 将 Stream 流转换为 List。 将List按多个指定的属性进行排序 方式1 如果要按多个指定的属性对 ListEntity 进行排序你可以使用 Java 的 Comparator 接口来自定义排序规则。以下是一个示例代码演示了如何按多个指定的属性对 ListEntity 进行排序 假设你的实体类 Entity 包含了多个属性比如 property1 和 property2你希望先按 property1 排序然后在 property1 相同的情况下再按 property2 排序 import java.util.*;public class Main {public static void main(String[] args) {ListEntity entityList ...; // 获取入参 List// 使用 Comparator 自定义排序规则ComparatorEntity comparator Comparator.comparing(Entity::getProperty1).thenComparing(Entity::getProperty2);// 对 List 进行排序Collections.sort(entityList, comparator);// 现在 entityList 已按指定属性排序}static class Entity {private int property1;private String property2;// 其他属性和方法包括 getter 和 setterpublic int getProperty1() {return property1;}public void setProperty1(int property1) {this.property1 property1;}public String getProperty2() {return property2;}public void setProperty2(String property2) {this.property2 property2;}} }在上面的示例中我们使用了 Java 8 的 Comparator 接口的 comparing() 方法来按照 property1 进行排序然后使用 thenComparing() 方法来在 property1 相同的情况下按照 property2 进行排序。最后我们使用 Collections.sort() 方法将 List 按照自定义的排序规则进行排序。 方式2 当然可以使用实现接口的方式来实现多属性排序。你可以编写一个实现 Comparator 接口的类并在其中定义自定义的排序逻辑。以下是一个示例代码 import java.util.*;public class Main {public static void main(String[] args) {ListEntity entityList ...; // 获取入参 List// 使用自定义的排序类进行排序ComparatorEntity comparator new MultiPropertyComparator();Collections.sort(entityList, comparator);// 现在 entityList 已按指定属性排序}static class Entity {private int property1;private String property2;// 其他属性和方法包括 getter 和 setterpublic int getProperty1() {return property1;}public void setProperty1(int property1) {this.property1 property1;}public String getProperty2() {return property2;}public void setProperty2(String property2) {this.property2 property2;}}static class MultiPropertyComparator implements ComparatorEntity {Overridepublic int compare(Entity entity1, Entity entity2) {// 先按 property1 排序int result Integer.compare(entity1.getProperty1(), entity2.getProperty1());if (result ! 0) {return result;}// 如果 property1 相同则按 property2 排序return entity1.getProperty2().compareTo(entity2.getProperty2());}} }在上面的示例中我们创建了一个名为 MultiPropertyComparator 的实现了 Comparator 接口的内部类实现了 compare() 方法来定义多属性排序逻辑。然后我们使用这个自定义的排序类进行排序。 方式3 是的你可以通过让实体类实现 Comparable 接口来定义排序规则。这样你就可以直接使用 Collections.sort() 方法对实体对象的 List 进行排序而无需显式地传递 Comparator。 以下是一个示例代码演示了如何让实体类实现 Comparable 接口来完成排序功能 import java.util.*;public class Main {public static void main(String[] args) {ListEntity entityList ...; // 获取入参 List// 对 List 进行排序Collections.sort(entityList);// 现在 entityList 已按指定属性排序}static class Entity implements ComparableEntity {private int property1;private String property2;// 其他属性和方法包括 getter 和 setter// 实现 compareTo 方法定义排序规则Overridepublic int compareTo(Entity other) {// 先按 property1 排序int result Integer.compare(this.property1, other.property1);if (result ! 0) {return result;}// 如果 property1 相同则按 property2 排序return this.property2.compareTo(other.property2);}// 其他属性的 getter 和 setter 方法} }在上面的示例中我们让实体类 Entity 实现了 Comparable 接口并重写了 compareTo 方法来定义排序规则。然后我们可以直接使用 Collections.sort() 方法对实体对象的 List 进行排序因为实体类已经定义了排序规则。 List 根据id排序 方式1 要根据实体类中的 id 属性对 ListEntity 进行排序你可以按照以下步骤操作 让实体类实现 Comparable 接口并在其中重写 compareTo 方法根据 id 属性进行比较。调用 Collections.sort() 方法对 List 进行排序。 下面是一个示例代码 import java.util.*;public class Main {public static void main(String[] args) {ListEntity entityList ...; // 获取入参 List// 对 List 进行排序Collections.sort(entityList);// 现在 entityList 已按 id 排序}static class Entity implements ComparableEntity {private Long id;// 其他属性和方法包括 getter 和 setter// 实现 compareTo 方法定义排序规则Overridepublic int compareTo(Entity other) {return this.id.compareTo(other.id);}// id 的 getter 和 setter 方法public Long getId() {return id;}public void setId(Long id) {this.id id;}} }在上面的示例中我们让实体类 Entity 实现了 Comparable 接口并重写了 compareTo 方法来根据 id 属性进行比较。然后我们可以直接使用 Collections.sort() 方法对实体对象的 List 进行排序因为实体类已经定义了排序规则。 方式2 不使用实现接口的方式 如果不想使用实现接口的方式你可以使用 Comparator 来定义排序规则并将其作为参数传递给 Collections.sort() 方法。以下是一个示例代码 import java.util.*;public class Main {public static void main(String[] args) {ListEntity entityList ...; // 获取入参 List// 使用 Comparator 定义排序规则ComparatorEntity comparator Comparator.comparingLong(Entity::getId);// 对 List 进行排序Collections.sort(entityList, comparator);// 现在 entityList 已按 id 排序}static class Entity {private Long id;// 其他属性和方法包括 getter 和 setter// id 的 getter 和 setter 方法public Long getId() {return id;}public void setId(Long id) {this.id id;}} }在上面的示例中我们使用 Comparator.comparingLong() 方法创建了一个按照 id 属性进行比较的 Comparator并将其传递给 Collections.sort() 方法来对 List 进行排序。 List指定多个属性判断是否存在重复的数据 如果在实体类的多个属性中存在 null 值并且你想要判断是否存在重复的数据可以使用 Map 来记录已经出现过的属性组合然后进行遍历判断。以下是一个示例代码演示了如何判断 ListEntity 中是否存在重复的数据其中 name 和 age 属性中可能存在 null 值 import java.util.*;public class Main {public static void main(String[] args) {ListEntity entityList ...; // 获取入参 List// 使用 Map 记录已经出现过的属性组合MapString, SetInteger attributeMap new HashMap();boolean hasDuplicate false;for (Entity entity : entityList) {// 根据实体对象的属性值构建属性组合的字符串表示String attributeKey entity.getName() - entity.getAge();// 如果属性组合已经存在则说明存在重复数据if (attributeMap.containsKey(attributeKey)) {hasDuplicate true;break;}// 将属性组合放入 Map 中SetInteger ids attributeMap.computeIfAbsent(attributeKey, k - new HashSet());ids.add(entity.getId());}if (hasDuplicate) {System.out.println(存在重复数据);} else {System.out.println(不存在重复数据);}}static class Entity {private Integer id;private String name;private Integer age;// 其他属性和方法包括 getter 和 setter// id、name 和 age 的 getter 和 setter 方法public Integer getId() {return id;}public void setId(Integer id) {this.id id;}public String getName() {return name;}public void setName(String name) {this.name name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age age;}} }在上面的示例中我们使用了一个 HashMap 来记录已经出现过的属性组合其中键是由 name 和 age 组成的字符串值是出现过该属性组合的实体对象的 ID 集合。然后我们遍历 List对每个实体对象构建属性组合的字符串表示检查该字符串是否已经在 Map 中存在如果存在则说明存在重复数据。 实战 package com.exer;import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import java.util.stream.Collectors;public class Main {public static void main(String[] args) {Entity entity1 new Entity(1L,qq, 10);Entity entity2 new Entity(3L,null, 12);Entity entity3 new Entity(2L,ww, 11);ListEntity entityList1 Arrays.asList(entity1, entity2, entity3);// 将 List 转换为 MapMapLong, Entity entityMap entityList1.stream().collect(Collectors.toMap(Entity::getId, entity - entity));System.out.println(entityMap entityMap);System.out.println(entityList1 entityList1);entityList1.sort(Comparator.comparingLong(Entity::getId));System.out.println(entityList1 entityList1);// 添加不重复的Map idEntity entity6 new Entity(null,aa, 13);Entity entity7 new Entity(null,null, 12);Entity entity8 new Entity(2L,dd, 15);ListEntity entityList2 Arrays.asList(entity6, entity7, entity8);for (int i 0; i entityList2.size(); i) {if (entityList2.get(i).getId() ! null) {entityMap.put(entityList2.get(i).getId(),entityList2.get(i));} else {entityMap.put((entityList1.get(entityList1.size() - 1).getId() i 1), entityList2.get(i));}}System.out.println(entityMap entityMap);// 将 Map 转换为 ListListEntity entities new ArrayList(entityMap.values());System.out.println(entities entities);// ListEntity 判断是否有重复SetString keySet new HashSet();for(Entity entity : entities) {if(!keySet.add(entity.getKey())){System.out.println(存在重复数据);}}} } package com.exer;public class Entity {private Long id;private String name;private String age;public String getKey(){return name age;}public Entity() {}public Entity(Long id, String name, String age) {this.id id;this.name name;this.age age;}public Long getId() {return id;}public void setId(Long id) {this.id id;}public String getName() {return name;}public void setName(String name) {this.name name;}public String getAge() {return age;}public void setAge(String age) {this.age age;}Overridepublic String toString() {return Entity{ id id , name name \ , age age \ };}}
http://www.zqtcl.cn/news/102768/

相关文章:

  • 网站建设前准备工作手机上传视频网站开发
  • 海口网站建设是什么意思wordpress推广码
  • 杭州市住房和城乡建设厅网站海南网站建设设计
  • 网站建设平台一般多少钱wordpress 本地上传服务器
  • 怎么给网站命名男女做羞羞羞的网站
  • 北京响应式网站建设公司信息流推广方式
  • 一级a做爰片迅雷网站微分销系统定制开发
  • 山东网站建设工作室网页设计全部代码
  • 用c 做网站可以吗注册网站什么要求
  • 销售网站排名销售型网站模板
  • wordpress 汽车宁波seo整体优化
  • 网站建设公司在哪里宣传c2c旅游电子商务平台
  • 网站查看空间商网站不提交表单
  • 空间怎么上传网站企业所得税怎么算公式
  • 网站建设wix建筑公司网站设计思路
  • 门户型网站都有哪些网页制作的视频教程
  • 虚拟主机 多个网站没有备案的网站
  • 河南网站建设推广公司汕尾网站建设
  • 海南省建设网站首页公司网站图片传不上去
  • 中国建设银行网站评价广告投放都有哪些平台
  • 网站系统免费wordpress附件不在数据库
  • 网站开发国外研究状况电商推广是什么意思
  • 太原建高铁站wordpress分级菜单显示
  • 工信部网站备案变更运营一个app大概多少钱
  • 杭州网站建设公司哪家好网站建设 中国联盟网
  • 成都手机网站建设价格网站安全检测软件
  • 长沙申请域名网站备案找个做游戏的视频网站
  • 网站平台开发与应用面试西安seo优化顾问
  • 苏州网站制作及推广中国优秀的企业网站
  • 网站开发语言太老东莞哪家公司做网站比较好