android Arrays.binarySearch issue - android

String[] array =
getContext().getResources().getStringArray(R.array.DevCategories);
to get an array. I the call this:
Arrays.binarySearch(array,"Plan"));
it returns -5, I am sure the first value of the array is Plan. What happend?
By the way, the current class is extended from ContentProvider. I am writing a Provider.
I use this code get -5
String.format("%d",Arrays.binarySearch(array,"Plan")));
String.valueOf(Arrays.binarySearch(array,"Plan")));
both are -5
R.array.DevCategories from string.xml file.
<string-array name="DevCategories">
<item>Plan</item>
<item>Design</item>
<item>Coding</item>
<item>Debug</item>
<item>Test</item>
<item>Release</item>
</string-array>

Are you sure that the Array is sorted according to the natural sort order (for Strings in this case)?
Results are undefined if the Array is not sorted.
A negative result normally indicates that the key was not found and the result can be used to calculate the index at which the key should be inserted to add it to the Array (to maintain the proper sorting). This will be bogus if the Array was not sorted to begin with.

Related

How to append an array into an array field in firestore?

Question
I have a collection named Users with a field named friendEmails which is an array that contains Strings.
I have a document with friendEmails = {joe#gmail.com, dan#gmail.com}, and I want to append newEmails = {mat#gmail.com, sharon#gmail.com} to it.
Problem
The only options I know for this are:
Reading the friendEmails array first, then adding the union of it and newEmails to the document.
Iterating over the elements of newEmails (let's and each iteration doing:
myCurrentDocumentReference.update(FieldValue.arrayUnion, singleStringElement);
(Since FieldValue.arrayUnion only lets me pass comma-separated elements, and all I have is an array of elements).
These two options aren't good since the first requires an unnecessary read operation (unnecessary since FireStore seems to "know" how to append items to arrays), and the second requires many write operations.
The solution I'm looking for
I'd expect Firestore to give me the option to append an entire array, and not just single elements.
Something like this:
ArrayList<String> newEmails = Arrays.asList("mat#gmail.com", "sharon#gmail.com");
void appendNewArray() {
FirebaseFirestore firestore = FirebaseFirestore.getInstance();
firestore.collection("Users").document("userID").update("friendEmails", FieldValue.arrayUnion(newEmails));
}
Am I missing something? Wouldn't this be a sensible operation to expect?
How else could I go about performing this action, without all the unnecessary read/write operations?
Thanks!
You can add multiple items to an array field like this using FieldValue.arrayUnion() as the value of a field update:
docRef.update("friendEmails", FieldValue.arrayUnion(email1, email2));
If you need to convert an ArrayList to an array for use with varargs arguments:
docRef.update("friendEmails", FieldValue.arrayUnion(
newEmails.toArray(new String[newEmails.size()])
));

How to find same values in arraylist and sort them in sections?

I have an unsorted arraylist with 70000 string values. I want to add same values in separate list.
i.e
if the sample of unsorted list is like this
Arraylist[0]->"NewYork"
Arraylist[1]->"DC"
....
....
....
Arraylist[401]->"NewYork"
Arraylist[402]->"Seoul"
Arraylist[403]->"DC"
if 2 or more same values are found from the unsorted list, i want to add in separate list or multihashmap which can store same values, as i want to create sections for same values. Result would be like this
Section1:
Arraylist1.add("NewYork");
Arraylist1.add("NewYork");
Section2:
Arraylist2.add("DC");
Arraylist2.add("DC");
In my perspective as unsorted list can be random , Creating multiple arraylist is bad approach instead i have used like multihashmap for sections.
The thing is i do not want code because i already implemented it,the thing is above scenario for finding strings and sorting them in each section but my algorithm is way slow, it takes about 30 to 40 seconds, my issue is which is the fastest way to perform this so i can do this in lesser and minimal time.

Get specific range of Firebase Database children

E.g. I have next data in my Firebase Realtime Database (my json file contains json array of some objects):
I would like to get all objects starting from a specific index (position) to the end of this array (in Firebase DB json array is converted to simple object which contains children with keys 001, 002 and so on)
So I need something like this but for Firebase query:
list.subList(10, list.size)
I know there are limitToFirst, limitToLast methods but it's different
fireDatabaseReference.child("episodes").limitToLast(10)
It won't do what I need. Because I need to know the size of this array and I need to be sure that this array won't become bigger at the moment someone makes such request (at some point this array can get become bigger due to adding new objects)
Would be great to have a method like from to get all children from 10 to the end (so first 9 children are excluded):
fireDatabaseReference.child("episodes").from(10)
But there is no such method
Is there any solution for this?
Solved :)
fireDatabaseReference.child("episodes").orderByKey().startAt("10")

Get current place type in Google Places API

I'm working with Google Places API.
I'm getting right the place in my log, but I just want the name of the type of place. Let's say this is my output
Place 'Parque Las Tejas' has likelihood: 0.950000 Type: [69, 1013, 34]
So, at first I get the position where I am, the likelihood of where I am and then I just used:
List<Integer> types = placeLikelihood.getPlace().getPlaceTypes();
thinking it would return like "park" or "square" but instead of that I get those array of numbers [69, 1013, 34].
According to what I read here, there is lots of types that defines a certain place.
What I want is to get that kind of types only, so if I'm at a restaurant I don't want the name of the restaurant but instead just the type, so "Restaurant" will be my output.
I need this because I want to give the user options depending on what type of place they are.
Any idea what I'm doing wrong?
The List<Integer> that you get is actually the id of type of places, according to the docs:
The elements of this list are drawn from Place.TYPE_*
The list is here. So basically your goal is to convert int code to a string using this list. You can find your solution here, basically you obtain all the fields from the Place class, find all the fields that start with "TYPE", get the int value and compare it to the value that you get from the getPlaceTypes().
even though this is an old post, it will help you if you an encountering this issue,
i was encountering this issue
and here is my approach
List<Place.Type> types = placeLikelihood.getPlace().getTypes();
to get all the type you can use a foreach loop
for(Object type:types){
// get all individual type
}

Why ArrayList hash map assigns same values to same objects at different positions;

My app calculates distances between cities. At some time, i get an ArrayList hash map of the form:
[{username=p, startcity=A, finalcity=B},
{username=p, startcity=C, finalcity=D},
{username=f, , startcity=E, finalcity=D},
{username=e, startcity=F, finalcity=L}]
Few lines below, i get ArrayList
[{username=p, startcity=A, finalcity=B},
{username=e, startcity=F, finalcity=L},
{username=p, startcity=C, finalcity=D},
{username=f, , startcity=E, finalcity=D},
{username=e, startcity=F, finalcity=L}]
Note that objects at positions 1 and 4 are exactly the same.
Then i try to assign at every object a different number. Problem is that the aforementioned objects are getting every time same value...
Code...
for(n=0;n<5;n++){
ArrayList.get(n).put("n",String.valueof(n));
}
Log.d("ArrayList", ArrayList.toString.....
leads to...
[{username=p, startcity=A, finalcity=B , n=0},
{username=e, startcity=F, finalcity=L,n=4!!!!//here...},
{username=p, startcity=C, finalcity=D,n=2},
{username=f, , startcity=E, finalcity=D,n=3},
{username=e, startcity=F, finalcity=L,n=4}]
So i can't differentiate those two objects. Why; Isn't that weird; Is there a way to do this; Thanks in advance
Firstly, I don't understand what ArrayList hash map means. ArrayList and HashMap is entirely different. Did you do some transformation?Maybe Something wrong happens when you transform the ArrayList to HashMap.
Secondly, you said "few lines below", I think you should paste that few lines, it's necessary for others to help solve your problems.
I hope you can edit your problem again. More details is necessary for solving this problem.
ArrayList.get(n)
will get the same object when the value of n will be 4 and will override the same object which is placed when n is 1.

Categories

Resources