Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What are the common operations of java List

Shulou Source: shulou.com Published: 2022-06-02 07:55:46 10月04日 Update

本篇内容主要讲解"java List常见操作有哪些",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"java List常见操作有哪些"吧!

迭代器Iterator ite = resultList.iterator();while (ite.hasNext()) { Map result = ite.next(); ite.remove();}排序Collections.sort(userInfoList, new Comparator() { public int compare(Map o1, Map o2) { return ((Date) o1.get("createTime")).compareTo((Date) o2.get("createTime")); }});Collections.sort(downList, new Comparator() { @Override public int compare(Map o1, Map o2) { String timeStr1 = ObjectUtil.objToString(o1.get("statDate")); String timeStr2 = ObjectUtil.objToString(o2.get("statDate")); timeStr1 = timeStr1.replaceAll("(?:年|月)", ""); timeStr2 = timeStr2.replaceAll("(?:年|月)", ""); Integer yearMonth2 = Integer.valueOf("".equals(timeStr1) ? "0" : timeStr1); Integer yearMonth3 = Integer.valueOf("".equals(timeStr2) ? "0" : timeStr2); return yearMonth2 > yearMonth3 ? -1 : 1; }});去重Set setMap = new HashSet(); for (int i = 0; i < md5s.size(); i++ ) { tempList = scenesDomain.queryScenAppList(rootScenId, null, md5s.get(i), crowdDates.get(i), Constants.QUERYAPPBYSCENID_TOPSIZE); for (Map map : tempList) { if (setMap.add(map)) { appList.add(map); } } }中文排序public static void main(String[] args) { ArrayList list = new ArrayList(); list.add("一鸣惊人-Y"); list.add("人山人海-R"); list.add("海阔天空-H"); list.add("空前绝后-K"); list.add("后来居上-H"); Comparator cmp = Collator.getInstance(java.util.Locale.CHINA); Collections.sort(list, cmp); for (String str : list) { System.out.println(str); }}实体类排序import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.List;public class SortUser { public static void main(String[] args) { List userlist = new ArrayList(); userlist.add(new User("H - 哈哈 ", 32)); userlist.add(new User("O - 噢哈哈", 22)); userlist.add(new User("W - 哇哈哈", 22)); Collections.sort(userlist, new Comparator() { @Override public int compare(User u1, User u2) { // 先按年龄排序 int flag = u1.getAge().compareTo(u2.getAge()); // 年龄相等比较姓名 if (flag == 0) { return u1.getName().compareTo(u2.getName()); } else { return flag; } } }); for (User user : userlist) { System.out.println(user.getName()); } }}class User { private String name; private Integer age; public User(String name, Integer age) { this.name = name; this.age = age; } 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; }}到此,相信大家对"java List常见操作有哪些"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

Tags: Sort common content age learning practical amazing deeper blockbuster sea of people coming from behind vast sky unprecedented sea of people interest name entity practicality practical simple operation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux macOS Xiaomi OPPO Reno MariaDB