collections

Java: Sorting a queue

人走茶凉 提交于 2021-01-23 12:33:14
问题 I'm making a wrapper to queue type, but each time I add element, I want to sort everything inside. Mostly it will be Integer . I'm not too familiar with Collections framework, is there any simple solution ? public class Round<Type> { private Queue<Type> qe; public Round(){ this.qe = new LinkedList<Type>(); } public void push(Type p){ this.qe.offer(p); //Collections.sort(this.qe); Here I want to sort this } public Type pop(){ return this.qe.poll(); } } 回答1: Are you sure that is what you want?

UnsupportedOperationException on Collection

流过昼夜 提交于 2021-01-20 17:33:47
问题 While studying the Collection API, we find that some methods ( add , remove ,...) may throw a java.lang.UnsupportedOperationException if the current implementation of the Collection does not support those functionalities. Is there,actually, in the JDK, a concrete Collection that does not support those methods ? Thanks a lot for your answers. 回答1: The obvious examples are the implementations returned from, say, Collections.unmodifiableCollection() and other similar methods. Methods that would

Sort List<Map<String,Object>> based on value

蹲街弑〆低调 提交于 2021-01-19 09:03:43
问题 Basically I have a List<Map<String,Object>> , and I want to sort it by the values of certain key in the map. The problem is that I do not know the type... This map can contain Strings, Integers, Doubles, Floats etc.... I would like to sort it: So far List<Map<String,Object>> data = getResults(); Collections.sort(data, (o1, o2) -> (String.valueOf(o2.get("Field1"))) .compareTo((String.valueOf(o1.get("Field1"))))); It is not a great Idea since numbers are not properly sorted.... How can I handle

Sort List<Map<String,Object>> based on value

拟墨画扇 提交于 2021-01-19 09:01:26
问题 Basically I have a List<Map<String,Object>> , and I want to sort it by the values of certain key in the map. The problem is that I do not know the type... This map can contain Strings, Integers, Doubles, Floats etc.... I would like to sort it: So far List<Map<String,Object>> data = getResults(); Collections.sort(data, (o1, o2) -> (String.valueOf(o2.get("Field1"))) .compareTo((String.valueOf(o1.get("Field1"))))); It is not a great Idea since numbers are not properly sorted.... How can I handle

Invert a Map with redundant values to produce a multimap

痴心易碎 提交于 2021-01-19 06:45:44
问题 Given a map such as this where we have a frequency count per day-of-week for a year: Map.of( DayOfWeek.MONDAY , 52 , DayOfWeek.TUESDAY , 52 , DayOfWeek.WEDNESDAY, 53 , DayOfWeek.THURSDAY , 53 , DayOfWeek.FRIDAY , 52 , DayOfWeek.SATURDAY , 52 , DayOfWeek.SUNDAY , 52 ) …or as text: {MONDAY=52, TUESDAY=52, WEDNESDAY=53, THURSDAY=53, FRIDAY=52, SATURDAY=52, SUNDAY=52} …how can I invert to produce a multimap of distinct numbers each leading to a collection (list? set?) of the DayOfWeek which owned

Invert a Map with redundant values to produce a multimap

孤人 提交于 2021-01-19 06:40:07
问题 Given a map such as this where we have a frequency count per day-of-week for a year: Map.of( DayOfWeek.MONDAY , 52 , DayOfWeek.TUESDAY , 52 , DayOfWeek.WEDNESDAY, 53 , DayOfWeek.THURSDAY , 53 , DayOfWeek.FRIDAY , 52 , DayOfWeek.SATURDAY , 52 , DayOfWeek.SUNDAY , 52 ) …or as text: {MONDAY=52, TUESDAY=52, WEDNESDAY=53, THURSDAY=53, FRIDAY=52, SATURDAY=52, SUNDAY=52} …how can I invert to produce a multimap of distinct numbers each leading to a collection (list? set?) of the DayOfWeek which owned

When do I keep a map<Identifier, Object> vs a Collection<Object with identifier as field>

不羁岁月 提交于 2021-01-08 02:04:15
问题 There is one question that I often ask myself while designing a program, and I am never quite sure how to answer it. Let's say I have an object with multiple fields, amongst which there is one serving as the identifier to that specific object. Let's also say that I need to keep track of a List of such objects somewhere else. I now have three, and probably even more, options on how to go about it: Have my object contain its own identifier, and all its other fields. I now use a simple array (or

When do I keep a map<Identifier, Object> vs a Collection<Object with identifier as field>

回眸只為那壹抹淺笑 提交于 2021-01-08 02:01:27
问题 There is one question that I often ask myself while designing a program, and I am never quite sure how to answer it. Let's say I have an object with multiple fields, amongst which there is one serving as the identifier to that specific object. Let's also say that I need to keep track of a List of such objects somewhere else. I now have three, and probably even more, options on how to go about it: Have my object contain its own identifier, and all its other fields. I now use a simple array (or

When do I keep a map<Identifier, Object> vs a Collection<Object with identifier as field>

ε祈祈猫儿з 提交于 2021-01-08 02:01:24
问题 There is one question that I often ask myself while designing a program, and I am never quite sure how to answer it. Let's say I have an object with multiple fields, amongst which there is one serving as the identifier to that specific object. Let's also say that I need to keep track of a List of such objects somewhere else. I now have three, and probably even more, options on how to go about it: Have my object contain its own identifier, and all its other fields. I now use a simple array (or

When do I keep a map<Identifier, Object> vs a Collection<Object with identifier as field>

懵懂的女人 提交于 2021-01-08 02:01:15
问题 There is one question that I often ask myself while designing a program, and I am never quite sure how to answer it. Let's say I have an object with multiple fields, amongst which there is one serving as the identifier to that specific object. Let's also say that I need to keep track of a List of such objects somewhere else. I now have three, and probably even more, options on how to go about it: Have my object contain its own identifier, and all its other fields. I now use a simple array (or