Skip to content
Advertisement

Tag: arraylist

Java invert a map

I need to invert an original map. which type is <Integer, String>, like {1 = A, 2 = A, 3 = B….}. I want to create a new map which is String to ArrayList because if 1 = A, and 2 = A, than I want to have something like this: A = [1, 2]. So how can I do

Latency in Iterator or foreach

i have one ArrayList that has a 1000 Insert SQL statement. but rather in execution time the latency of Iterator or (enhanced for loop) for this ArrayList take 1 minute . and my JFrame is not responding in this period. what can i do? Thanks Answer foreach internally uses iterator only, in order to handle bulk updates and inserts from

How to find the longest string object in an arrayList

Here is is my problem, I have to explain a lot, because it’s a quite complicated. I have created an arrayList<Word>, that contains strings as objects. In my case, I have three classes, all working together, that is supposed to represent a dictionary. The first class is called “Word”, it has a constructor, and has simple methodes like .length, adds

Removing Sublist from ArrayList

For simplicity, let’s say I have an ArrayList whose indices contain exactly one single-digit integer. For instance: I would like to filter out all occurrences of the sublist 6 0 6, such that the new list becomes: Is there any way of doing this? Using ListIterator doesn’t seem to work for me, because I have to consider three consecutive elements

Java – Slice any array at steps

In python we are able to do the following: Is there an equivalent to this in Java? I have been looking for this type of array slicing, but I have had no luck. Any help would be great, Thanks! Answer If you are using Java 8, then you can make use of streams and do the following: Outputs: [0, 3,

Creating a Matrix with ArrayLists

I want to create an ArrayList-matrix with n-rows and m-columns, for example I’ve already written a code for creating such a matrix but my code doesn’t display the values when I clear the list containing the column-data. Here is my Is there any possibility to clear the contents of intList without clearing the contents of mainList?? Answer When calling mainList.add(intList);

Getting only the first name in an array

I have an array list like this: ArrayList names = new ArrayList<>(); that stores people’s first and last names when they are entered in different textbooks. So when prompted to Joe Biden would be element number 1 then Barack Obama would be element number 2 in the array list. My question is that if it is possible to only get

Advertisement