I have an array of strings plus one additional string. I want to use this string and values of array to get a set of string. Then I want to order the set of string alphabetically and extract the string which is the first in the list. What is the easiest way to do it in Java?
ADDED:
I wanted to do it this way:
List<String> playersList = Arrays.asList(players);
playersList.add(userName); // <---------- HERE IS A PROBLEM
Collections.sort(playersList);
I do not get any errors during the compilation. But during the execution I get a "UnsopportedOperationException". And it happens in the second line.
Arrays.asList wraps the array with an unmodifiable List, so when you try to add to it it throws UnsupportedOperationException. What you could do it create a new a ArrayList and add your elements to it, then you're free to modify it afterwards.
List<String> list = new ArrayList<String>(Arrays.asList(players));
list.add(userName);
If you just want to get the minimum of an array of String with an additional external element, then you don't have to sort and extract first (which would be O(N log N)). You can do it in O(N).
String minPlayer = Collections.min(Arrays.asList(players));
minPlayer = Collections.min(Arrays.asList(minPlayer, extraPlayer));
Either append the value to the array and sort it with Arrays.sort or create a List of the items and sort them using Collections.sort. The natural ordering of the strings will be alphabetical.
Related
I have a list optionDetails and in it another list called content. I want to match those content with another array and return the descriptions from the second array
second array has 90 items in the optionCodeContent. I want to match the option code in the first array with the optionCode in the optionCodeContent array and return its optionDescription in the second array.
Im not very good with java 8 streams and i dont know where to begin.
i need to match the option codes so i can get the right descriptions into another arraylist. In the end, i should have two descriptions in another arraylist
You have to get a stream from the optionDetails array, then map it to a String that gets its value from an inner stream of optionCodeContent. It's funky but it works.
optionDetails.stream()
.map(optionItem -> optionCodeContent.stream()
.filter(occ -> occ.optionCode.equals(optionItem.optionCode))
.findFirst().get())
.collect(Collectors.toList())
I have a string array where I want to check each element in the array with 8 other array elements to see if any of those element in the first array element categorize under any of them.
Simply I want to categorize a string array. So in order to do that I have to check with 8 other arrays (Because I have 8 categories) I want to know an efficient way to do this without looping one by one
You can use HashMap instead of array or arraylist.
Implement the 8 categories as HashMap.
From the element you want to check, match it against the the 8 HashMaps. This will give you a worst case of 8 checks.
If you can combine the values of all 8 categories into one HashMap, the worst case will be 1.
I believe you could combine the values from various categories and have only 1 HashMap by appending a checksum to each value. Something like:
//values in the hash map
xxxx_cat1
yyyy_cat1
zzzz_cat1
xxxx_cat2
yyyy_cat2
zzzz_cat2
After you get the value from the HashMap, just based on the checksum (the appended text) to get its category.
You can sort the array and then use Arrays.binarySearch() method instead of looping one by one. It is more efficient way to search a particular element.
I have this string:
string1="A.1,B.2,C.4"
I want to get the following arraylist of arraylist:
<<"A","1">,<"B","2">,<"c","4">>
is there any way other than using for loop?
suppose that arraylists are unique, so I would have
Set<String> set= new HashSet<>(Arrays.asList(string1.split(",")));
now I want to split each element in the above set on ., without using for loop.
I was thinking of first splitting based on ,. Then, split each of those on ., put both values in a list, and then put that list in another list. Something pseudo-code-y that should work. :)
string1="A.1,B.2,C.4"
stringsWithDots[] = string1.split(",");
List<List<String>> result = new ArrayList<List<String>>();
for(String stringWithDots: stringsWithDots) {
finalSplit[] = stringWithDots.split(".");
List<String> list1 = Arrays.asList(finalSplit);
result.add(list1);
}
"A.1,B.2,C.4" would look like [[A,1],[B,2],[C,4]]
Edit.
[asList source]
[split] which is used to split a string [has a loop].
ArrayList is backed by an array. The asList function simply sets a reference to that array [source].
So, that thing you say about not using a loop. Well, be it using stream or some internal function, loops happen; just that you might not be seeing it in the immediate code that you write.
static ArrayList random_ints=new ArrayList();
static ArrayList mean_storage=new ArrayList();
static ArrayList diff_means=new ArrayList();
random_ints = {834,438,234,124};
mean_storage = {867,459,254,189};
I have to find out the difference b/w these 2 array lists and store in "diff_means" array list
if your requirement is absolute difference between the list's element values then
Try This
int length=firstList.size();
for(int i=0;i<length;i++){
resultList.add(Math.abs(firstList.get(i)-secondList.get(i)));
}
Assuming Size of Both the list is same !
So unless you are looking for a specifically optimized solution, you can iterate the lists, get the numbers from both input lists at a particular index and find the difference (believing that mere subtraction would do, judging which number is greater!) and add it to third list.
You may need to handle situation where size of one list is less than other.
I am currently making a tiny little program that should be able to select a random element i put into the array using a Random of course (For practice purposes) and when a element in the array has been chosen at random. i want to remove this element in the array, so how do you remove a element in a array the easiest way?
Its the only thing i want to know. I got everything else sorted. It's just removing the element it has chosen (the random takes a random number between 0 and the amount of elements in the array, so if it chooses 0, it will take the first element in the array, and so on)
You can use ArrayList which support remove or add function, which actually is an resizable array.
You can't remove an element from an array. You can replace it with some other value that indicates "nothing", null for example.
An easy solution is to convert the array into a list.
list = Arrays.asList(array);
Remove any element from the list and then revert it back to an array using
array = list.toArray();
Hope it helps.
Use List instead of Array, and if you want to stay on Array than there is 2 solution,
Create another Array ignoring your element which you want to delete.
create a List using Array.asList(...) than remove element from list and convert back to Array.
but according to me its better for you as well as java to use List. because List provide a many build-in functions.