collections

Sorting a collection of objects in VBA

心已入冬 提交于 2020-06-25 04:38:04
问题 I'm trying to write a function that would sort a collection of objects. Since the objects are all of the same type (the same user-defined class), their property set is the same. Is it possible to discover the object's properties (through code) so as to put the collection in a bi-dimensional array, each row being for an object, each column for one of its property? Another solution would be to copy each object from the collection to an array of objects, and sort them by one of their property,

Is there a simple way in Java to get the difference between two collections using a custom equals function without overriding the equals?

被刻印的时光 ゝ 提交于 2020-06-24 14:00:11
问题 I'm open to use a lib. I just want something simple to diff two collections on a different criteria than the normal equals function. Right now I use something like : collection1.stream() .filter(element -> !collection2.stream() .anyMatch(element2 -> element2.equalsWithoutSomeField(element))) .collect(Collectors.toSet()); and I would like something like : Collections.diff(collection1, collection2, Foo::equalsWithoutSomeField); (edit) More context: Should of mentioned that I'm looking for

Is there a way to avoid loops when adding to a list?

大城市里の小女人 提交于 2020-06-24 10:55:06
问题 I was wondering a code like this: List<String> list = new ArrayList<String>(); for(CustomObject co : objects) { list.add(co.getActualText()); } Can it be written differently? I mean of course at some point there will be a loop but I am wondering if there is an API usage I am ignoring 回答1: If you use Java 8, you can take advantage of the Stream API: List<String> list = objects.stream() .map(CustomObject::getActualText) .collect(Collectors.toList()); 回答2: If you have Java 8, what about: objects

Why does the hashCode() of an ArrayList change every time you add a new element?

旧街凉风 提交于 2020-06-24 07:19:16
问题 As per my understanding of ArrayList , the default capacity is 10 and when it grows beyond 10, it will create a new object with new capacity and so on.. So out of curiosity, I typed following program to check hashcode() for ArrayList object: public class TestCoreJava { public static void main(String [] args){ ArrayList al = new ArrayList(); for(int i=0;i<15;i++){ al.add("Temp"+i); System.out.println("Hashcode for "+i+" element "+al.hashCode()); } } } According to above scenario, when I am not

How to get a list of specific fields values from objects stored in a list?

元气小坏坏 提交于 2020-06-24 03:35:42
问题 Say I have an list of objects with two fields field1 and field2 , both of String type. How do I get a list of all field1 values without having to iterate over the list, if it is at all possible? 回答1: An object is a reference towards a memory address. Then, the fields of this objects are other references towards other memory addresses. Hence, a list of objects is a list of references. So, it's impossible for the list to direclty access the object fields (references given by the references).

Is there a dictionary-like object that is immutable? [duplicate]

强颜欢笑 提交于 2020-06-23 14:16:05
问题 This question already has answers here : What would a “frozen dict” be? (14 answers) Closed 12 days ago . I would like a Python object that can flexibly take any key and I can access by key, like a dictionary, but is immutable. One option could be to flexibly generate a namedtuple but is it bad practice to do this? In the example below a linter would not expect nt to have attribute a for example. Example: from collections import namedtuple def foo(bar): MyNamedTuple = namedtuple("MyNamedTuple

singly and doubly linked list in java?

早过忘川 提交于 2020-06-23 11:54:46
问题 Which collection interface is efficient to implement the singly and doubly linked list in java? code sample please? 回答1: The right interface to implement a doubly-linked list is, unsurprisingly, a LinkedList. See the JavaDoc. I'm not going to give you a code sample here; they're all over the web and you could find one with two minutes' research. If you want a singly-linked list for some reason, you're probably going to have to roll your own using a custom Node class. Each Node should just

Kotlin's List missing “add”, “remove”, Map missing “put”, etc?

强颜欢笑 提交于 2020-06-22 04:55:30
问题 In Java we could do the following public class TempClass { List<Integer> myList = null; void doSomething() { myList = new ArrayList<>(); myList.add(10); myList.remove(10); } } But if we rewrite it to Kotlin directly as below class TempClass { var myList: List<Int>? = null fun doSomething() { myList = ArrayList<Int>() myList!!.add(10) myList!!.remove(10) } } I got the error of not finding add and remove function from my List I work around casting it to ArrayList, but that is odd needing to

wrong number or types of arguments in call to P_AA

喜欢而已 提交于 2020-06-17 14:04:25
问题 When I try to compile the procedure with collection type as in parameter, I'm getting the error like wrong number or types of arguments in call to 'P_AA' -------Procedure created with in parameter as nested table------------ create or replace procedure p_aa(serv in t45) is aa serv_item%rowtype; begin for i in 1..serv.count loop select a.* into aa from serv_item a where a.serv_item_id = serv(i); dbms_output.put_line('Serv item '||aa.serv_item_id||' '||'status '||aa.status); end loop; end; / --

Getting last row's data in the map

本小妞迷上赌 提交于 2020-06-17 12:57:34
问题 My objective is to create a map. It have reqid , name and lowest status number of a set as a key. Here set refers to rows belonging to a specific Reqid . The value of the map is an Arraylist which consists of all rows as objects of a specific id . public class MapKey { private Integer id; private String name; private Integer status; public MapKey(Integer id, String name,Integer status) { this.id = id; this.name = name; this.status=status; } @Override toString,hashCode,equals public class