collections

ObjectiveC - Key-Value-Pair Collection with duplicate Keys

こ雲淡風輕ζ 提交于 2019-12-23 12:34:56
问题 Does ObjectiveC provide a collection for Key-Value-Pairs, that allow a key to occur multiple times? I try to parse a xml file into some simple structure. Every things is already working with nested NSDictionary, but now xml elements can occur multiple times. Edit: My Solution I choose an NSArray with KeyValuePairs, it turned out that I need something that is order sensitive, NSDictionary was not possible. Sideeffect: NSFastEnumeration is easy to implement this way for my collection. 回答1: No,

When defining 'Set set = new HashSet()', is set an instance of interface or class Set?

南笙酒味 提交于 2019-12-23 12:18:12
问题 In Java, 'Set' and 'List' are interfaces derived from 'Collection' interface. If we use the code: import java.util.*; public class SetExample{ public stactic void main(String[] args){ Set set = new HashSet(); //do something ..... } } Is there a class 'Set' in "Collection" API that we are creating an object ('set') of? or we are instantiating a interface 'Set'? Am really confused.......:O 回答1: java.util.Set is an interface, not a class. So Set set = new HashSet(); creates an object that is a

how to selectively filter items in a collection

社会主义新天地 提交于 2019-12-23 12:14:10
问题 I use the following snippet to filter the list of selected users, where isSelected is a boolean variable. Is there a simpler way (helper function) to populate the selectedUsers collection instead of writing the following lines of code. List<User> selectedUsers = new ArrayList<User>(0); for (User user : this.getUsers()) { if (user.isSelected()) { selectedUsers.add(user.getId()); } } 回答1: You can use the filter function from the Google Collections; however, you will still need to construct a

Is there an equivalent of java.util.Properties for sets?

时光总嘲笑我的痴心妄想 提交于 2019-12-23 12:08:03
问题 I want to read lines from a file into a Set or List. Is there a standard utility for doing this? If these lines are of the form [key]=[value] I can do: Properties properties = new Properties(); properties.load(new FileInputStream(file)); The values are single entries, one per line, each value becomes an entry in the Set/List I know it can be done with LineReader, InputStreams and plenty of boilerplate but I'm looking to avoid this. 回答1: yes, the Properties class itself provides accessor

com.google.common.collect.Sets.SetView bug or feature?

馋奶兔 提交于 2019-12-23 10:39:50
问题 Hello I've this piece of code: public static void main(String[] args) { Set<Integer> set1 = new HashSet<Integer>(); Set<Integer> set2 = new HashSet<Integer>(); set1.add(1); set1.add(2); set1.add(3); set1.add(4); set1.add(5); set2.add(4); set2.add(5); set2.add(6); set2.add(7); set2.add(8); SetView<Integer> x = Sets.intersection(set1, set2); set1.removeAll(x); set2.removeAll(x); } and it throws Exception in thread "main" java.util.ConcurrentModificationException at java.util.HashMap

c# : how to read from specific index in List<person>

£可爱£侵袭症+ 提交于 2019-12-23 09:56:26
问题 I have a class of persons and list collection as list contains all the values of person class such as : List ilist has 2 values [0]={firstname,lastname} . [1]={firstname2,lastname2} now when i am iterating into the list i am able to print the list but i want to change the value of some parts of my list e.g in index 1 if i want to change the value of firstname2 to firstname3 i am not able to do it . Can anyone tell me how to print the list and then on that index changing any value of the index

c# : how to read from specific index in List<person>

泄露秘密 提交于 2019-12-23 09:56:06
问题 I have a class of persons and list collection as list contains all the values of person class such as : List ilist has 2 values [0]={firstname,lastname} . [1]={firstname2,lastname2} now when i am iterating into the list i am able to print the list but i want to change the value of some parts of my list e.g in index 1 if i want to change the value of firstname2 to firstname3 i am not able to do it . Can anyone tell me how to print the list and then on that index changing any value of the index

Java lambda sublist

旧街凉风 提交于 2019-12-23 09:42:36
问题 What is the shortest way to express "get new List B from List A where condition" via a Java 8 lambda? Say I have List<Integer> a = Arrays.asList(1, 2, 3, 4, 5) and I want a new List, B, where the value is > 3. I've read through the new Collections Streams API, but I'm not convinced I have found the best way to do this, and don't want to taint the question with what is probably my less than perfect solution. 回答1: a.stream().filter(x -> x > 3).collect(Collectors.toList()); 来源: https:/

How to use JAXB to marshall/unmarshall a collection of MyBean

荒凉一梦 提交于 2019-12-23 09:13:53
问题 I have a MyBean annotated @XmlRootElement public class MyBean ... Marshalling/Unmarshalling MyBean w/o problems, e.g. JAXBContext jaxbCtx = JAXBContext.newInstance(MyBean.class); Marshaller m = jaxbCtx.createMarshaller(); m.marshal(myBean, writer); How can I use JAXB to marshall/unmarshall a Collection or List? My attempt results in this error: javax.xml.bind.MarshalException - with linked exception: [com.sun.istack.internal.SAXException2: unable to marshal type "java.util.ArrayList" as an

Map lookup performance

不羁的心 提交于 2019-12-23 09:03:10
问题 I'd like to do something using a map value for a given key only if the map contains the given key. Naively I would write: Map<String, String> myMap = ...; if(myMap.containsKey(key)) { String value = myMap.get(key); // Do things with value } The code above looks easy to understand, but from a performance point of view, wouldn't it be better the following code? Map<String, String> myMap = ...; String value = myMap.get(key); if(value != null) { // Do things with value } In the second snippet I