Friends in common java? - linker

my idea is this.
I want to create a script, linking mutual friends
For example:
Friends of Damian = [Cristian, Pedro, Juan, Andres]
Friends of Andres = [Marcela, Damian, Julian, Carlos]
Friends of Lucas = [John, Damian, Andres, Francisco]
Friends of Roman = [Albert, Lourdes, Jennifer, Diego]
Friends of Diego = [Magali, Brian, Andrew, Peter]
Friends of Brian = [Etc.]
Friends of Leo = [Etc.]
Friends of Ramiro = [Etc.]
Friends of Franco = [Etc.]
I should select 5 names, and I would have to say if any friends in common and what are the names. The names will be limited to 60 approx. And they will be 4 names for groups.

Use Collection#retainAll().
listA.retainAll(listB);
// listA now contains only the elements which are also contained in listB.
So make a copy of the first list and repeat retainAll for all five lists.
That I think would be an easy way to do this.
Referring this answer
Common elements in two lists

I assume by common Java, you are talking about Java, and not Javascript. However you called it a script... a snippet of code is also a near must to get good answers on stack overflow, I don't know whether you are using arrays, lists, or even Maps... I will give an answer for each. But please, do give more detailed information next time you ask a question.
Lists have a method called contains() which you can pass an object into.
From here there are two ways to work with it, the preferred way is through Lists
package com.gamsion.chris;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class SOListExample {
public static void main(String[] args) {
List<String> list1 = new ArrayList<String>();
List<String> list2 = new ArrayList<String>();
List<String> list3 = new ArrayList<String>();
String name1 = "John";
String name2 = "Chris";
String name3 = "Madalyn";
list1.add(name1);
list2.add(name1);
list2.add(name2);
list3.add(name2);
list3.add(name3);
list1.add(name3);
list2.add(name3);
List<List<String>> lists = new ArrayList<List<String>>();
lists.add(list1);
lists.add(list2);
lists.add(list3);
System.out.println(getCommonFriends(lists));
}
public static List<String> getCommonFriends(List<List<String>> lists) {
List<String> commonfriends = lists.get(0);
Iterator<List<String>> it = lists.iterator();
while(it.hasNext()){
commonfriends.retainAll(it.next());
}
return commonfriends;
}
}
That is the best answer I can give. Of course adapt to your needs and it should fit well. (I just made that as a testable example. The way you get those lists and how many is totally up to you.) It's also the most effective as far as I know, although the initial "commonfriends = lists,get(0)" does annoy me haha.
EDIT: Adding answer for comment below.
To do that, suggest adding another method that just compares two Lists like this:
public static List<String> compareCommonFriends(List<String> list1, List<String> list2){
list1.retainAll(list2);
return list1;
}
Also in case you want to do an if something was found, whether it was the compareCommonFreinds() or the getCommonFriends(), you can do something like this:
if(compareCommonFriends(list1, list2).isEmpty()){
//your code
}
//OR
if(!compareCommonFriends(list1, list2).isEmpty()){
//your code
}

Related

How to split an Array of Strings to another array with respect to lines?

So I am trying to get a list a list of WebElements to an Array of String. I have written the below code, which is helping me to get the WebElements to an array
Code -
List<WebElement> statusLabelSection = driver.findElements(By.xpath("//div[#class='MuiGrid-root paboxLayout MuiGrid-item']//table"));
List<String> stringsOutput = new ArrayList<String>();
for(WebElement ele:statusLabelSection) {
stringsOutput.add(ele.getText());
}
System.out.println(stringsOutput);
Output –
[Not Required
0001F - Code Recovery Composite
Recovery Which That May Apply
None
Additional static Information
None]
Problem Statement -
I want the output to be in an array like this
expected -
[Not Required, 0001F - Code Recovery Composite, Recovery Which That May Apply, None, Additional static Information, None]
Can you please help!!!
Might not be the best way but solved it. Any feedback to improvise is welcome!!!
code
List<WebElement> statusLabelSection = driver.findElements(By.xpath("//div[#class='MuiGrid-root paboxLayout MuiGrid-item']//table"));
List<String> stringsOutput = new ArrayList<String>();
for(WebElement ele: statusLabelSection) {
stringsOutput.add(ele.getText());
}
String str = stringsOutput.get(0).toString();
String[] inputArr = str.split("\n");
for (int i=0; i<inputArr.length; i++) {
System.out.println("num " + i + " - "+ inputArr[i]);
}

Trouble finding the correct syntax creating vars in objects

Up until now I have been creating var inside the classes I made. e.g.
var backpack:Array = new Array("food", "water");
I want to create objects dynamically now like:
player = {};
player.backpack = ("food", "water"); // not the right syntax
OR
player = {backpack:Array = new Array("food", "water")} // not right either.
Any help? Thanks in advance. I can do this with simple vars like int, but can't find the answer to arrays.
ActionScript's generic object properties don't have any variable type associated with them. You assign them one of the following ways.
Example 1
player = {backpack: new Array("food", "water")};
Example 2
player.backpack = new Array("food", "water");
Example 3
player["backpack"] = new Array("food", "water");
You can use square brackets to define literal arrays. Not only is it shorter, but it's also faster (see this post).
The correct syntax for your two examples are
player = {};
player.backpack = ["food", "water"];
and
player = {backpack: ["food", "water"]};
Also, if you find it easier, you can use it in the first line of code you wrote.
var backpack:Array = ["food", "water"];

RESTEasy: getPathSegments().get(1);

Can someone tell me what "PathSegment model = info.getPathSegments().get(1);" do, specifically, what does he getPathSegments().get(1) mean? Please provide a sample URL for demonstration. The book didn't give an example URL for this one.
Also, is there such a thing as get(0); ?
#Path("/cars/{make}")
public class CarResource
{
#GET
#Path("/{model}/{year}")
#Produces("image/jpeg")
public Jpeg getPicture(#Context UriInfo info)
{
String make = info.getPathParameters().getFirst("make");
PathSegment model = info.getPathSegments().get(1);
String color = model.getMatrixParameters().getFirst("color");
...
}
}
Thanks again,
If you split the path of a URL by a '/' you'll get a list of path-segments. So e.g. the path /cars/ford/mustang/1976 contains the four segments [cars, ford, mustang, 1976]. info.getPathSegments().get(1) should return the segment ford.
The PathSegment holds also the associated MatrixParameters of the current segment. MatrixParameters can be used if you want to filter the resources with a parameter that affects only one segment like here:
/cars/ford/mustang;generation=two/1976

KnockoutJS Accessing an array of objects in a separate class

So i have 2 classes, users and health readings. i made a array of user objects with an array of readings inside it.i just want access to the date and weight in the reading array, i have been trying for a long time to figure this out! please help this simple problem is driving me nuts!
// Class to represent a row in the seat reservations grid
function Reading(theDate,theWeight)
{
self.theDate=ko.observable(theDate);
self.theWeight=ko.observable(theWeight);
}
function User(name,weight,date) {
var self = this;
self.name = name;
self.date = date;
self.weight = ko.observable(weight);
self.theReadings = ko.observableArray([
new Reading(12,13)
]);
}
// Editable data
self.users = ko.observableArray([
new User("George",1,2012),
new User("Bindu",2,2012)
]);
/this alerts an object but i dont know how to access the weight/date variable
alert(self.users()[0].theReadings()[0]);
self.readings = ko.observableArray([
new Reading(12,13)
Just missed a few things on this one.
Here ya go.
http://jsfiddle.net/EN7y4/3/
Namely. You had "self.theWeight" in function Reading instead of "this."...
Happy Coding!
alert(self.users()[0].theReadings()[0].theWeight();
I would recommend removing the 'thes'. That's an uncommon style.

Creating an array of properties of items in an existing array

Probably not the best title, but I'll explain:
I have an array of objects - lets call them Person.
Each Person has a Name. I want to create an array of Name respectively.
Currently I have:
def peopleNames = new ArrayList<String>()
for (person in people)
{
peopleNames.add(person.name)
}
Does groovy provide a better means of doing so?
Groovy provides a collect method on Groovy collections that makes it possible to do this in one line:
def peopleNames = people.collect { it.name }
Or the spread operator:
def peopleNames = people*.name
The most concise way of doing this is to use a GPath expression
// Create a class and use it to setup some test data
class Person {
String name
Integer age
}
def people = [new Person(name: 'bob'), new Person(name: 'bill')]
// This is where we get the array of names
def peopleNames = people.name
// Check that it worked
assert ['bob', 'bill'] == peopleNames
This is one whole character shorter than the spread operator suggestion. However, IMO both the sperad operator and collect{} solutions are more readable, particularly to Java programmers.
Why don't you try this? I like this one because it's so understandable
def people = getPeople() //Method where you get all the people
def names = []
people.each{ person ->
names << person.name
}

Resources