Java 8 frequency Object in array [duplicate] - arrays

This question already has answers here:
Word frequency count Java 8
(11 answers)
Closed 7 years ago.
I have an Object[] array
I need to create map Map<Obejct, Integer>, where Integer value contains frequency of key Object in array.
How can i do it in java 8 style, using Collectors?

You can do (I hope I don't have any typos) :
Map<Object,Long> map = Stream.of(array)
.collect(Collectors.groupingBy(o -> o,
Collectors.counting()));
This should group the elements of the array by equality and count the number of Objects in each group.

Related

What does the # operator do between two 2D arrays [duplicate]

This question already has answers here:
What does the "at" (#) symbol do in Python?
(14 answers)
What is the '#=' symbol for in Python?
(3 answers)
Closed 3 months ago.
I'm studying a code for 2D correlation spectroscopy.
The code takes 2 arrays (a and b) with the same numbers of rows, and calculated the pair-wise relationship between all the possible combination of columns in the two arrays. The operand to perform such calculation is "#".
For example:
arr1=np.array([[1,2,3], [2,3,4]])
arr2=np.array([[1,2,2,6], [3,4,5,7]])
Final_array = arr1.T # arr2 / (len(arr1) - 1)
I'm not able to an explanation of what the # operand does.

Check item contains in model object array in swift 3 [duplicate]

This question already has answers here:
how can I check if a structure is in the array of structures based on its field in Swift3?
(3 answers)
Get element from array of dictionaries according to key
(1 answer)
Closed 5 years ago.
I have a model object called user.
how to find my user.id from all users collection.
I need something like this UsersCollection.contains(user.id)
and returns Bool value.
Here you go.
let contains = UsersCollection.contains{ $0.id == 0 }
Edit:
let object = UsersCollection.first{ $0.id == 0 } // object is optional here.

How to create array and display required array value [duplicate]

This question already has answers here:
How does one declare an array in VBScript?
(2 answers)
Closed 5 years ago.
how to create array and display required array value. For example:
Arr[0]="anand" Arr[1]="bala".print Arr optin:2, Bala .
i need the required array output
Queat seems a bit unclear.
May be below piece of code can be relevent to your Question.
'Initializing & Filling up array
Dim testArr(3)
testArr(0)="First"
testArr(1)="Second"
testArr(2)="Third"
Dim Iterator,userInput
'If you Need ro validate the User input you need to use Err Object.
'Like if it's number or not/Is teh User Provided value is positive or not/ etc. etc. which i'm not doing now)
userInput=Cint(inputbox("Please enter a Positive number between 1-" & Ubound(testArr)))
If userInput<=Ubound(testArr) Then
MsgBox(testArr(userInput-1))
Else
MsgBox("Wrong Value Provided. Not Applicable Array Index Number !!!")
End If

Swift - Formatting an Array of Phone Number Strings [duplicate]

This question already has answers here:
string replace phone number iOS swift
(4 answers)
Closed 7 years ago.
I have an array of phonenumbers from my phone's contacts list that are are in different formats ranging from +1 (123) 123-1234, 1231231234, +11231231234, and 123-123-1234. I want them to all be in the format of 1231231234, but I don't much about string formatting. Can anyone point me in the right direction?
Try something like this:
let digits = NSCharacterSet.decimalDigitCharacterSet()
var phoneNumberDigits = ""
for character in phoneNumber.unicodeScalars {
if digits.longCharacterIsMember(character.value) {
phoneNumberDigits += String(character)
}
}

Delete all occurrences of value in Swift Array [duplicate]

This question already has answers here:
Remove matched item from array of objects?
(5 answers)
Closed 7 years ago.
So I have an array of Int's and I am trying to create a function to delete a certain value from the array. However, the removeAtIndex() function only deletes the first occurrence and the removeLast() only removes the last. I tried enumerating through the array but I end up with an Array index out of range error, possible due to the reshifting that occurs when deleting an item from the array.
for (index, value) in connectionTypeIDs.enumerate() {
if (value == connectionTypeToDelete){
connectionTypeIDs.removeAtIndex(index)
}
}
Any ideas on how to accomplish this?
The quickest way is to use filter. I don't know is that answer your question but you can have a look on this:
// remove 1 from array
arr = arr.filter{$0 != 1}

Resources