我有一个带有 name 、ageBand 字符串间隔的 Person 类,它是参数化的构造函数、getter、setter.class Person {字符串名称;字符串年龄带;//说它是字符串0-50",我在创建一个人时传入了构造函数.//获取者//二传手}class TestAgeBand {公共静态无效主(字符串参数[]){ArrayListperson = new ArrayList();Person p1 = new Person("Mike1", "0-5");Person p2 = new Person("Mike2", "6-10");Person p3 = new Person("Mike3", "11-30");Person p4 = new Person("Mike4", "31-45");Person p5 = new Person("Mike5", "50-100");Person p6 = new Person("Mike6", "46-50");Person p7 = new Person("Mike7", "100-110");人.添加(p1);//将所有人员添加到列表中.}}这是我用我的代码对间隔进行排序的操作.我需要根据增加的间隔对人进行排序.我正在使用 Treemap 对间隔进行排序.MapageBandMap = new TreeMap(){for(Person p: 人) {ageBandMap.put(p.ageBand, p.name);}}当我打印间隔键集时,我得到输出:[0-5, 100-110, 11-30, 31-45, 46-50, 50-100, 6-10]我不需要的.我需要这样排序的间隔:[0-5, 6-10, 11-30, 31-45, 46-50, 50-100, 100-110] 解决方案 尝试拆分你的 ageBand 字符串并将其转换为 Integer,这样会更容易排序.person.stream().sorted(Comparator.comparing(element -> Integer.parseInt(element.getAgeBand().split("-")[0]))).collect(Collectors.toList());如果您不想使用 Java 8,您可以使用 Collections.sort() 方法来实现. Collections.sort(person, new Comparator() {@覆盖公共 int 比较(人 o1,人 o2){return Integer.parseInt(o1.getAgeBand().split("-")[0]) - Integer.parseInt(o2.getAgeBand().split("-")[0]);}});I'm having an Person class with some Person and there details as there name, age band.The ageband interval is {"0-5", "6-10", "11-30","31-45", "46-50","50-100", "100-110"};I'm having a Person class with name , ageBand String interval and it's parameterised constructor, getters, setters.class Person { String name; String ageBand; //say it is string "0-50" which i pass in constructor while creating a person. //getters //setters}class TestAgeBand { public static void main(String args[]) { ArrayList<Person> person = new ArrayList<Person>(); Person p1 = new Person("Mike1", "0-5"); Person p2 = new Person("Mike2", "6-10"); Person p3 = new Person("Mike3", "11-30"); Person p4 = new Person("Mike4", "31-45"); Person p5 = new Person("Mike5", "50-100"); Person p6 = new Person("Mike6", "46-50"); Person p7 = new Person("Mike7", "100-110"); person.add(p1); //adding all persons to list. }}Here's what I'm doing with my code to sort the interval.I need to sort persons according to increasing intervals.I'm using Treemap to sort the intervals.Map<String, Person> ageBandMap = new TreeMap<String, Person>(){ for(Person p: person) { ageBandMap.put(p.ageBand, p.name); }}When I print interval keyset, I getOutput: [0-5, 100-110, 11-30, 31-45, 46-50, 50-100, 6-10]Which I don't need. I need intervals sorted like this: [0-5, 6-10, 11-30, 31-45, 46-50, 50-100, 100-110] 解决方案 Try splitting the your ageBand string and converting it into an Integer, it will be easier to sort.person.stream().sorted(Comparator.comparing(element -> Integer.parseInt(element.getAgeBand().split("-")[0]))) .collect(Collectors.toList());If you don't want to use Java 8, you can do it with Collections.sort() method. Collections.sort(person, new Comparator<Person>() { @Override public int compare(Person o1, Person o2) { return Integer.parseInt(o1.getAgeBand().split("-")[0]) - Integer.parseInt(o2.getAgeBand().split("-")[0]); } }); 这篇关于在java中对数字字符串间隔进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-23 00:16