collections

Java Comparator for byte array (lexicographic)

风流意气都作罢 提交于 2019-12-29 05:13:24
问题 I have a hashmap with byte[] keys. I'd like to sort it through a TreeMap. What is the most effective way to implement the comparator for lexicographic order? 回答1: Using Guava, you can use either of: UnsignedBytes.lexicographicalComparator() SignedBytes.lexicographicalComparator() The UnsignedBytes comparator appears to have an optimized form using Unsafe that it uses if it can. Comments in the code indicate that it may be at least twice as fast as a normal Java implementation. 回答2: Found this

Java Comparator for byte array (lexicographic)

我是研究僧i 提交于 2019-12-29 05:13:06
问题 I have a hashmap with byte[] keys. I'd like to sort it through a TreeMap. What is the most effective way to implement the comparator for lexicographic order? 回答1: Using Guava, you can use either of: UnsignedBytes.lexicographicalComparator() SignedBytes.lexicographicalComparator() The UnsignedBytes comparator appears to have an optimized form using Unsafe that it uses if it can. Comments in the code indicate that it may be at least twice as fast as a normal Java implementation. 回答2: Found this

C++ how to copy a map to a vector [duplicate]

随声附和 提交于 2019-12-29 05:12:09
问题 This question already has answers here : Copy std::map into std::vector of pairs (3 answers) Closed last year . What's the best way in C++ to copy a pair from a map to vector? I'm doing this so I can subsequently sort the vector. 回答1: This should do what you want: #include <iostream> #include <vector> #include <map> #include <algorithm> #include <iterator> using namespace std; bool cmp(const pair<int, int> &p1, const pair<int, int> &p2) { return p1.second < p2.second; } int main() { map<int,

How to add element in List while iterating in java?

こ雲淡風輕ζ 提交于 2019-12-29 04:59:51
问题 Say I have a List like: List<String> list = new ArrayList<>(); list.add("a"); list.add("h"); list.add("f"); list.add("s"); While iterating through this list I want to add an element at the end of the list. But I don't want to iterate through the newly added elements that is I want to iterate up to the initial size of the list. for (String s : list) /* Here I want to add new element if needed while iterating */ Can anybody suggest me how can I do this? 回答1: You can't use a foreach statement

What is the Collections.checkedList() call for in java?

◇◆丶佛笑我妖孽 提交于 2019-12-29 04:42:04
问题 I just want to know for what java.util.Collections.checkedList() is actually used. I have some code that I know is returning me a List<String> but it's being passed through a chain of messaging calls and returned to me as a java.io.Serializable . Is that checkedList call good for me to turn my Serializable into a List<String> ? I know I can cast it to a java.util.List , but I'd rather not have to check each element and I'm not comfortable with assuming each element is a String . 回答1: It is

Is there a standard Java List implementation that doesn't allow adding null to it?

我只是一个虾纸丫 提交于 2019-12-29 04:30:10
问题 Say I have a List and I know that I never want to add null to it. If I am adding null to it, it means I'm making a mistake. So every time I would otherwise call list.add(item) I would instead call if (item == null) throw SomeException(); else list.add(item); . Is there an existing List class (maybe in Apache Commons or something) that does this for me? Similar question: Helper to remove null references in a Java List? but I don't want to remove all the nulls, I want to make sure they never

Convert List<String> to List<Integer> directly

浪尽此生 提交于 2019-12-29 04:05:08
问题 After parsing my file " s" contains AttributeGet:1,16,10106,10111 So I need to get all the numbers after colon in the attributeIDGet List. I know there are several ways to do it. But is there any way we can Directly convert List<String> to List<Integer> . As the below code complains about Type mismatch, so I tried to do the Integer.parseInt, but I guess this will not work for List. Here s is String. private static List<Integer> attributeIDGet = new ArrayList<Integer>(); if(s.contains(

Accessing a Collection Through Reflection

陌路散爱 提交于 2019-12-29 03:34:09
问题 Is there a way to iterate (through foreach preferably) over a collection using reflection? I'm iterating over the properties in an object using reflection, and when the program gets to a type that is a collection, I'd like it to iterate over the contents of the collection and be able to access the objects in the collection. At the moment I have an attribute set on all of my properties, with an IsCollection flag set to true on the properties that are collections. My code checks for this flag

What's the difference between () vs [] vs {}?

笑着哭i 提交于 2019-12-29 03:15:26
问题 What's the difference between () vs [] vs {} in Python? They're collections? How can I tell when to use which? 回答1: () - tuple A tuple is a sequence of items that can't be changed (immutable). [] - list A list is a sequence of items that can be changed (mutable). {} - dictionary or set A dictionary is a list of key-value pairs, with unique keys (mutable). From Python 2.7/3.1, {} can also represent a set of unique values (mutable). 回答2: () is a tuple: An immutable collection of values, usually

How to check whether key or value exist in Map?

为君一笑 提交于 2019-12-29 03:12:07
问题 I have a scala Map and would like to test if a certain value exists in the map. myMap.exists( /*What should go here*/ ) 回答1: There are several different options, depending on what you mean. If you mean by "value" key-value pair , then you can use something like myMap.exists(_ == ("fish",3)) myMap.exists(_ == "fish" -> 3) If you mean value of the key-value pair , then you can myMap.values.exists(_ == 3) myMap.exists(_._2 == 3) If you wanted to just test the key of the key-value pair , then