I have an ArrayList (~900 entries) containing arrays of user information:
[swaschit, Sophia Waschitz, Dormant, Inactive, 1/1/2018]
[kolanday, Kyle Olanday, Dormant, Inactive, 1/1/2018]
[npowers, Neil Powers, Assigned, Active, 2/11/2018]
I want to generate an array from this list containing only the first elements of each object:
[swaschit, kolanday, npowers, ...]
What is the most efficient way of doing this?
One way is to use a Stream and map each of the inner arrays to its first element:
List<String> firstElements = yourList.stream()
.map(x -> x[0].toString()) // you might need toString() here if your array is an Object[]
.collect(Collectors.toList());
If you would like an array of strings instead:
String[] firstElements = yourList.stream()
.map(x -> x[0].toString()) // you might need toString() here if your array is an Object[]
.toArray(String[]::new);
I also suggest you to not use nested arrays like this. You should create a class with the properties you want to store and create a List of your class.
As I suppose the first entry is unique (as it seems to be a username), I would suggest using a Map. That way, you could simply list the keys.
HashMap<String,ArrayList<String>> hashmap=new HashMap<>();
Alternatively, you could simply create a class containing that information, to avoid needing the use of an ArrayList, but I don't know if this is an option.
As pointed out using an appropriately designed Object rather than an array of such heterogeneous data (guessing they are Strings in reality).
If you insist on keeping it the way you have though, you just need to allocate a new array with the size of the original ArrayList and iterate through, fetching the desired column. Assuming ArrayList<String[]> list
int size = list.size();
String[] firstColumn = new String[size];
for (int i = 0; i < list.size(); i++) {
firstColumn[i] = list.get(i)[0];
}
I suppose that you can do
list.stream().map(array -> array[0].toString()).toArray()
Related
I have a HashMap, let's say, with entries {1=1, 3=2, 4=1, 5=1, 6=1} . I'd like to be able to create an array just containing [1,2,1,1,1]. Is that possible?
To give some context, I'm trying to get something I can iterate over using an index, and anything like an array or ArrayList would work (the order of the items does matter).
I tried assigning the values to an array using
for (int j=0; j< frequencies.size(); j++){
for (int i : frequencies.values()) {
arr[j] = i;
}
}
frequencies.values() (frequencies is the name of my HashMap) wasn't of particular use, and I tried both iterating over that (didn't work) and assigning it to Collections wasn't useful. I tried assigning myHashMap.values() to an ArrayList as well, which obviously didn't work.
What would you recommend?
The type of frequencies.values() is a generic Collection, not necessarily an ArrayList. As a result, to convert it to an ArrayList, you must copy it -- fortunately, ArrayList has a constructor that accepts a generic Collection and copies it. As a result, all you have to do is write new ArrayList<>(frequencies.values()).
Is there a way to put all the values of a HashMap in an array?
Since you did specify an array as the target you can do it like this.
Map<Integer,Integer> map = new HashMap<>(Map.of(1,1,3,2,4,1,5,1,6,1));
int[] vals = map.values().stream().mapToInt(Integer::valueOf).toArray();
System.out.println(Arrays.toString(vals));
Prints
[1, 2, 1, 1, 1]
This is a kinda hacky way but it works well for me.... it assumes the values of the numbers from your hashmap are Longs.
Collect the values from your "frequencies" hashmap:
1. Collection<Long> values_from_hashmap = frequencies.values();
Create an array list from the collection of values:
2. ArrayList<Long> listOfValues = new ArrayList<Long>(values_from_hashmap);
Boom your listOfValues array list will contain the values from the orig hashmap
I have an custom class InfoAQ which has a method called public String getSeqInf(). Now I have an ArrayList<InfoAQ> infList and
I need an ArrayList<String>strList = new ArrayList<String>with the content from getSeqInf()for each element.
This is the way Im doing it right now ...
for(InfoAQ currentInf : infList)
strList.add(currentInf.getSeqInf());
Is there an alternative way to make it ? Maybe a faster one or one liner ?
Yes, there is:
strList = infList.stream().map(e -> g.getSeqInf()).collect(Collectors.toList());
The map step can be also written in another way:
strList = infList.stream().map(InfoAQ::getSeqInf).collect(Collectors.toList());
which is know as method reference passing. Those two solutions are equivalent.
Also maybe this one:
List<String> strList = new ArrayList<String>();
infList.forEach(e -> strList.add(e.getSeqInf()));
And there is another one (-liner, if you format it in a single line):
infList.forEach(currentInf -> {strList.add(currentInf.getSeqInf());});
while I would prefer a formatting in more lines:
infList.forEach(currentInf -> {
strList.add(currentInf.getSeqInf());
});
Using streams
infList.stream()
.map(InfoAQ::getSeqInf)
.collect(Collectors.toCollection(ArrayList::new))
Using Collectors.toCollection here to create an ArrayList that will hold the results as you do in your case. (Important if you do care about the result list type as Collectors.toList() does not guarantee this)
May not be the fastest as using stream has some overhead. You need to measure/benchmark to find out its performance
This code will iterate all the data in the list, as getSeqInf returns a String, the collect method will store all returns of the getSeqInf method in a list.
`List listString = infList.stream().map(InfoAQ::getSeqInf).collect(Collectors.toList());`
or
`
ArrayList<String> listString = new ArrayList<>();
for(int i = 0; i < infoAq.size(); i++) {
listString.add(infoAq.get(i).getSeqInf());
}`
I have two lists that each contain 2500 values.
List<String> headers = new ArrayList<String>();
List<String> data = new ArrayList<String>();
I need to combine these two lists into a two dimensional list or any other appropriate data structure that allows me to only iterate through once rather than doing the operation below which is very slow when dealing with big lists:
for (int i = 0; i < headers.size(); i++) {
for (int in = 0; in < data.size(); in++) {
}
}
It's critical the index of each list match each other when combining these two lists. So headers.get(9); must match data.get(9);.
I need a new list or other data structure that will allow me to store each value in the two lists as pairs. So headers.get(0); would be paired with data.get(0);.
Any help would be appreciated.
About
It's critical the index of each list match each other when combining these two lists.
Create a simple class to hold both value
class MyData{
String header;
String data;
}
Then use a single List<MyData>. You will only have one List of 2500 items.
Based on what you do with those data, other collection could be more efficient to read, but not to update.
Make a new class:
class HeadersWithData{
String header;
String data;
HeadersWithData(String header, String data){
this.header = header;
this.data = data;
}
}
Declare your list:
List<HeadersWithData> combinedList = new ArrayList<>();
And fill the list:
for (int i = 0; i < headers.size(); i++){
combinedList.add(new HeadersWithData(headers.get(i), data.get(i)));
}
Note: This is a one-dimensional list. A two-dimensional list would not make any sense in your case.
If you need the one list to contain the same pairs that you'd get doing the nested loop, there's nothing you can do. You're iterating over 2500*2500=6250000 pairs. Nothing will make that fast. Iterating over one list instead of the nested for loops will not make a difference.
If you don't want to iterate over every combination of elements from both lists, but iterate over one list once and then iterate over the other list once, then that's equivalent to doing one for loop after the other, not one for loop inside of the other. And that will be fast whether you combine them into one big list, or iterate over one list and then the other.
I'm not really sure if the loop you presented is what what you really wanted to archieve, but if you wanted every element of the first list correspond to exactly one element of the second list I would suggest using Map<String, String>. This allows you to map every element of the first list of strings to one elements of the second list. Each pair is then stored as an Map.Entry<Key,Value>.
That way you are able to iterate only once through the map using for example loop like this:
for(Map.Entry<String,String> entry : map.entrySet()) {...}
I have elements that is declared in a list variable such as:
List<List<String>> textList = new ArrayList<>();
The elements are added such as:
textList.add(Arrays.asList(p)); //adding elements
The only way I could output the elements inside the variable is by using:
for(List<String> s: textList){
System.out.println(s); }
which output elements like this:
[He is a boy.]
[He likes apple.]
[She is a girl.]
Now, I would like to store them in an array so that the elements will look like this when outputted.
[He is a boy., He likes apple., She is a girl.]
I've tried
String[] textArr = new String[textList.size()];
textArr = textList.toArray(textArr);
for(String s : textArr){
System.out.println(s);}
but I got an error about:
Exception in thread "main" java.lang.ArrayStoreException
at java.lang.System.arraycopy(Native Method)
at java.util.Arrays.copyOf(Arrays.java:3213)
at java.util.ArrayList.toArray(ArrayList.java:407)
So, how do I convert the elements inside a list into array using the proper way. Thanks!
Your problem is that you are not storing Strings in your list textList.
textList.add(Arrays.asList(p));
As the type says, you have a List of List of String here.
So you can't take the elements of that list and assume they are Strings. Because they aren't! The error message tells you that: toArray() wants strings it can put into that array of strings, but you give it a List of List of String!
But thing is: what you are describing here doesn't make sense in the first place. Printing strings shouldn't care if strings are in an array or a List.
What I mean is: when you manually iterate a List or an array to print its content, then it absolutely doesn't matter if you iterate a List or an array. The code is even the same:
for (String someString : someCollection) {
System.out.println(someString);
}
someCollection can be both: array or List!
In other words: the idea to turn data that is nicely stored within Lists into arrays for printing simply doesn't make any sense. To the contrary: you are probably calling toString() on your List object, and the result of that ... isn't 100% what you want. But I guarantee you: calling toString() on some array will result in something you totally will not want.
Long story short: forget about converting to Arrays; simply iterate your List of List of Strings and use a StringBuilder to collect the content of that collection the way you want to see it (you simply append those [ ] chars to that builder in those places you want them to see).
(if you insist on that conversion to array, the key point there to understand is that only a List of String can be turned into an array of string. So a List of List ... doesnt work that easy).
Using streams and flatMap, you can do this:
List<List<String>> list = ...;
String[] strings = list.stream().flatMap(l -> l.stream()).collect(Collectors.toList()).toArray(new String[0]);
This is equivalent to using a loop (You can use two nested for loops as suggested in the comments instead by replacing the addAll, but why?):
List<List<String>> list = ...;
List<String> stringList = new ArrayList<>();
for (List<String> l : list)
stringList.addAll(l);
String[] strings = list.toArray(new String[stringList.size()]);
You can use Iterator in order to go over every element of the list, instance of the for each statement (I personally like the iterators more). The code you could use would be something like
//Your list
List<List<String>> textList = new ArrayList<>();
//The iterators
Iterator<List<String>> itList = textList.iterator();
Iterator<String> itString;
//The string to store the phrases
String s[] = new String[textList.size()];
int i =0;
//First loop, this seeks on every list of lists
while(itList.hasNext()){
//Getting the iterator of strings
itString = itList.next().iterator();
s[i] = "";
//2nd loop, it seeks on every List of string
while(itString.hasNext()){
s[i] = s[i].concat(itString.next());
}
s[i] = s[i].concat(".");
i++;
}
I want to make arrayList object in java that work as two dimentional array. My question is how can we access value from specific dimention from arrayList.
in two dimentional array, if i want to access value then it can be as m[i][j].
But in arraylist how can i do that ?
You mean something like a List in a List??
May be something like...
List<List<...>> twoDList = new ArrayList<>();
i want to make a List, in which each List key contains another List inside it
It should more like you want some kind of Map, which is basically a key/value pair.
Map<String, List<String>> mapValues = new HashMap<>(25);
List<String> listOfValues = ...;
//...
mapValues.put("A unique key for this list", listOfValues);
//...
List<String> thatListOfValues = mapValues.get("A unique key for this list");
List<List<Integer>> list = new ArrayList<List<Integer>>();
list.add(new ArrayList<Integer>());
list.add(new ArrayList<Integer>());
list.get(0).add(5);
list.get(1).add(6);
for(List<Integer> listiter : list)
{
for(Integer integer : listiter)
{
System.out.println("" + integer);
}
}
This way you can get the items like
list.get(1).get(0); //second dimension list -> integer
EDIT:
Although it is true that you can use a Map if you are trying to use numeric indices for example for each list, like so:
Map<Integer, List<YourObject>> map = new HashMap<Integer, List<YourObject>>();
map.put(0, new ArrayList<YourObject>());
map.put(5, new ArrayList<YourObject>());
map.get(0).add(new YourObject("Hello"));
map.get(5).add(new YourObject("World"));
for(Integer integer : map.keySet())
{
for(YourObject yourObject : map.get(integer))
{
yourObject.print(); //example method
System.out.println(" ");
}
}
Although even then the accessing of Lists would be the same as before,
map.get(0).get(1); //List -> value at index
Obviously you don't need to use Integers as the generic type parameter, that's just a placeholder type.
The solution like List<List<..>> is slow then you should use one dimention array like
// Two dimentions: m and n
List<String> arr = new ArrayList<String>(m*n);
for (int i=0; i< m; ++i) {
for (int j=0; j<n; ++j) {
String str=arr.get(i*n + j);
//You code here
}
}
Memory is an important consideration here.
It can be acceptable to model a 2D (or higher dimension) array using a 1D container. (This is how the VARIANT SAFEARRAY of Microsoft's COM works.) But, consider this carefully if the number of elements is large; especially if the container allocates a contiguous memory block. Using something like List<List<... will model a jagged-edged matrix and can fragment your memory.
With the 1D approach, you can use the get(index) method on the ArrayList appropriately transformed:
Given the (i)th row and (j)th column, transform using index = i * rows + j where rows is the number of rows in your matrix.
An arraylist is not an object to make a 2 dimentional arrays. However you can use it anyway :
You can use :
new ArrayList<ArrayList<Object>>; //or
new ArrayList<Object[]>;
But you should implement your own matrix class because you will probably have some check to do and a function get(int row, int column) would be cool
Also consider Table collection provided by Google Guava library. ArrayTable is an implementation based on 2D array.
You cane define like this
1>
List<Object[]> list = new ArrayList<Object[]>();
Fetching
list.get(i)[j];
2>
List<Map<Integer,Object>> list = new ArrayList<Map<Integer,Object>>();
Fetching
list.get(i).get(j);