arraylist

Java, searching within a list of objects?

感情迁移 提交于 2019-12-17 18:40:01
问题 I'm a bit lost on the way to make this happen the fastest. I have a large list of objects that have basic variable attributes (with getters / setters) and I need to do a search in this list to find the objects within the list that match a given parameter I have found how to do a regular list search but I need to, for example search for the value of the result of doing a call getName() for each object in the list and get objects that have a result that matches my input. Something like below

Java Vector or ArrayList for Primitives

。_饼干妹妹 提交于 2019-12-17 18:26:22
问题 Is there an expandable array class in the Java API equivalent to the Vector or ArrayList class that can be used with primitives (int, char, double, etc)? I need a quick, expandable array for integers and it seems wasteful to have to wrap them in the Integer class in order to use them with Vector or ArrayList . My google-fu is failing me. 回答1: There is unfortunately no such class , at least in the Java API. There is the Primitive Collections for Java 3rd-party product. It's pretty dangerous to

How to remove Duplicate value from arraylist in Android [duplicate]

雨燕双飞 提交于 2019-12-17 18:25:31
问题 This question already has answers here : How do I remove repeated elements from ArrayList? (38 answers) Closed 3 years ago . ArrayList<String> values=new ArrayList<String>(); values.add("s"); values.add("n"); values.add("a"); values.add("s"); In this Array, I want to remove repeated values. 回答1: Try this... ArrayList<String> values=new ArrayList<String>(); HashSet<String> hashSet = new HashSet<String>(); hashSet.addAll(values); values.clear(); values.addAll(hashSet); Happy coding... 回答2: Try

Find the most common String in ArrayList()

心不动则不痛 提交于 2019-12-17 18:24:15
问题 Is there a way to find the most common String in an ArrayList ? ArrayList<String> list = new ArrayList<>(); list.add("test"); list.add("test"); list.add("hello"); list.add("test"); Should find the word "test" from this list ["test","test","hello","test"] 回答1: Don't reinvent the wheel and use the frequency method of the Collections class: public static int frequency(Collection<?> c, Object o) Returns the number of elements in the specified collection equal to the specified object. More

Handling servlet output in AJAX

六月ゝ 毕业季﹏ 提交于 2019-12-17 17:46:55
问题 My problem: I am sending a request to a servlet from an AJAX function in a JSP. The servlet processes the data and returns a ArrayList . My question is how to handle the ArrayList inside AJAX, and display it as a table in same JSP. The code is function ajaxFunction ( ) { // var url= codeid.options[codeid.selectedIndex].text; url="mstParts?caseNo=9&cdid=QCYST0020E1"; // alert(cid); var httpRequest; if (window.XMLHttpRequest) { httpRequest = new XMLHttpRequest(); } else if (window.ActiveXObject

What does this boolean “(number & 1) == 0” mean?

为君一笑 提交于 2019-12-17 17:44:27
问题 On CodeReview I posted a working piece of code and asked for tips to improve it. One I got was to use a boolean method to check if an ArrayList had an even number of indices (which was required). This was the code that was suggested: private static boolean isEven(int number) { return (number & 1) == 0; } As I've already pestered that particular user for a lot of help, I've decided it's time I pestered the SO community! I don't really understand how this works. The method is called and takes

Arraylist in parcelable object

落花浮王杯 提交于 2019-12-17 17:37:05
问题 I have seen many parcelable examples so far, but for some reason I can't get it to work when it gets a bit more complex. I have a Movie object, which implements Parcelable. This book object contains some properties, such as ArrayLists. Running my app results in a NullPointerException when executing the ReadTypedList ! I'm really out of ideas here public class Movie implements Parcelable{ private int id; private List<Review> reviews private List<String> authors; public Movie () { reviews = new

Why does Java's ArrayList's remove function seem to cost so little?

此生再无相见时 提交于 2019-12-17 17:25:42
问题 I have a function which manipulates a very large list, exceeding about 250,000 items. For the majority of those items, it simply replaces the item at position x. However, for about 5% of them, it must remove them from the list. Using a LinkedList seemed to be the most obvious solution to avoid expensive removals. However, naturally, accessing a LinkedList by index becomes increasingly slow as time goes on. The cost here is minutes (and a lot of them). Using an Iterator over that LinkedList is

retrieving data from firebase database and storing in an array list

浪子不回头ぞ 提交于 2019-12-17 17:17:28
问题 I am attempting to create a small course aggregator program in the form of an android application. My courses are all stored in a Firebase Realtime Database, which is viewable from the firebase console and everything appears fine. The issue is that I have written a java method to connect to the DB, retrieve the data from the DB, cast the data as a Custom Java object Course , attach it to another custom Java object CourseCardModel , and then save the CourseCardModel object into an ArrayList.

Generics <? super A> doesn't allow superTypes of A to be added to the list

烂漫一生 提交于 2019-12-17 17:13:59
问题 I am using wildcard and lower bound generics on list and yet compiler is throwing error. Code: int intStart = 0; Number num = new Integer(2); List<? super Integer> listOfNumbers = new ArrayList<>(); listOfNumbers.add(intStart); listOfNumbers.add(num); //throws compiler error Error : The method add(capture#8-of ? super Integer) in the type List is not applicable for the arguments (Number) With List<? super Integer> , I should have been allowed to add any objects of type Integer or its