collections

Sort List<T> of objects by a particular rule?

[亡魂溺海] 提交于 2019-12-29 09:25:07
问题 I have this list: List<Country> countryList = new ArrayList<Country>(); And I want to sort the countries by their name, by using the method getCountry() . How can achieve this? UPDATE: public class Country implements Comparable @Override public int compareTo(Object another) { // TODO Auto-generated method stub return 0; } Can you tell how to compare them to get it sorted by Strings? Argentina, Austria, Brazil, etc. 回答1: There are two ways: Have Country implement the java.util.Comparable

Why can't a range of char be collected?

这一生的挚爱 提交于 2019-12-29 08:48:06
问题 I'm trying to generate a vector containing lowercase ASCII characters. This more convoluted approach works: let ascii_lowercase = (b'a'..=b'z').map(|b| b as char).collect::<Vec<char>>(); But this more straightforward one, which I came up with in the first place, does not: let ascii_lowercase = ('a'..='z').collect::<Vec<char>>(); The error is: error[E0599]: no method named `collect` found for type `std::ops::RangeInclusive<char>` in the current scope --> src/main.rs:2:39 | 2 | let ascii

Multi-line pretty-printing of (nested) collections in Java

萝らか妹 提交于 2019-12-29 07:33:08
问题 I want to be able to (pretty-)print the contents of my maps. They should have newlines and indentation rather than on a single line; ignoring the toString methods of collections/iterables/etc; and recursing into nested collections. This is especially of interest for me regarding maps. I suppose JSON'ing might be relevant, but I don't want to go that far, or at least - I don't want my code to have to know about JSON just for me to pretty-print it. What are my options (other than writing this

What collection to store a tree structure?

こ雲淡風輕ζ 提交于 2019-12-29 07:33:07
问题 I want to store an organisation chart in a collection. I think a tree data structure will be best suited to my needs, as I need to add multiple nodes to one node. LinkedList only provides adding one node to another node, if I understand it correctly. I have looked at C5 treeset collection, but it doesn't seem to have Add() method to add more than 2 nodes to one node. I have also looked at Treeview class from Windows Forms library, but I do not want to add Windows forms dll to my project,

When to use GlueList over ArrayList or LinkedList? [duplicate]

元气小坏坏 提交于 2019-12-29 07:17:49
问题 This question already has answers here : When to use LinkedList over ArrayList in Java? (32 answers) Closed 4 years ago . I came across new list implementation which called GlueList I want to know when i should use over ArrayList or LinkedList. 回答1: To be honest, if you cant decide after reading info at GlueList which states in few sentences why it is better (different) and even have benchmark to see the real values and also Big-O notation - you are not in position where you have to think

How to use Collections methods(removeAll() and retainAll()) for two objects. (objects are parent-child relation)

别说谁变了你拦得住时间么 提交于 2019-12-29 07:12:39
问题 I expected to result below but actually not. I would like to know how to show the differences between two Collections. (objects are parent and child relationship) In this case, can I use standard method like removeAll() or can you recommend another approach like using apache-commons. Thanks. CONSTRAINT ------------------------------ 1.Item.class is unmodifiable(eg. I can not add equals method) 2.If id is same between two objects, they are assumed as same things. ------------------------------

How to use two numbers as a Map key

无人久伴 提交于 2019-12-29 06:50:32
问题 I have two numbers and I want to use them together as a key in a Map . Currently, I'm concatenating their string representations. For example, suppose the key numbers are 4 and 12. I use: String key = 4 + "," + 12; The map is declared as Map<String, Object> . I think this is so bad! I like to use something other than a String as the key! I want the fastest way to create these keys. Who has a good idea? 回答1: Create an object that holds the two numbers and use it as the key. For example: class

Should I use put() or putIfAbsent() after using getOrDefault()?

倖福魔咒の 提交于 2019-12-29 06:44:22
问题 Java8 introduced those nice methods getOrDefault() and putIfAbsent() , allowing to write code like: Map<Foo, List<Bar>> itemsByFoo = ... List<Bar> bars = itemsByFoo.getOrDefault(key, new ArrayList<>()); bars.add(someNewBar); Now I am wondering if there are good factual reasons to either do: itemsByFoo.put(key, bars); or itemsByFoo.putIfAbsent(key, bars); Both would work: option 1 might do a lot of unnecessary "put" calls when adding elements to lists happens often option2 might do a lot of

convert ArrayList.toString() back to ArrayList in one call

送分小仙女□ 提交于 2019-12-29 06:15:07
问题 I have a toString() representation of an ArrayList . Copying the toString() value to clipboard, I want to copy it back into my IDE editor, and create the ArrayList instance in one line. In fact, what I'm really doing is this: my ArrayList.toString() has data I need to setup a unit test. I want to copy this ArrayList.toString() into my editor to build a test against this edge case I don't want to parse anything by hand My input looks like this: [15.82, 15.870000000000001, 15.92, 16.32, 16.32,

Restrict type in a Collection inside a class module

那年仲夏 提交于 2019-12-29 05:36:06
问题 I have a collection inside a class module. I'd like to restrict the object type that is "addable" to this collection, i.e. collection should only ever accept objects of one given type and nothing else. Is there any way to enforce the type of objects added to a collection? From what I can tell, there is no built-in way to do this. Is the solution then to make this collection private, and build wrapper functions for the methods usually accessible for Collections, i.e. Add , Remove , Item , and