Accessing a specific element of HasMap of String & ArrayList - arraylist

I was using an array double xyz[20000][20000] in my project but java is giving "Heap Space" error. I am planning to use Hashmaps instead of the above array. So I need to use Hashmap .
Now the question is that as with java array if an element needs to be accessed we can access it like xyz[10][10], but if I use the above Hashmap then how can i access a specific element like key=10 and the 10th element in the respective array?

HashMap always works that way. You have specified functions in the HashMap class in java to get values of a key, getKey() and getValue() should get you the key and values of the respective key.
Map<int, List<Integer>> entry = new HashMap<int, List<Integer>>();
//rest of the code, putting values etc.
//the accessing data of 10th index
int key = entry.getKey();
List<Integer> values = entry.getValue();
System.out.println("Key = " + key);
System.out.println("Values = " + values.get(10) + "\n");

Related

Which is the current method for table.raw in Cucumber

IDEA does not allow me to use table.raw();
I am new in cucumber so while I was learning/practising I tried to get the data from a DataTable by the following code
public void iEnterTheFollowingForLogin(DataTable table) {
List<List<String>> data = table.raw();
System.out.println("The value is : "+ data.get(1).get(0).toString());
System.out.println("The value is : "+ data.get(1).get(1).toString());
}
I realized that IDEA type the raw method in red so I think maybe it is obsolete and now I should use a newer one.
Rather then accessing the raw table you can address individual cells directly using cell(row, column) or use cells() to get a list of lists.
import io.cucumber.datatable.DataTable;
import java.util.List;
class Scratch {
public static void main(String[] args) {
DataTable data = //...create data table here
System.out.println("The value is : " + data.cell(1, 0));
System.out.println("The value is : " + data.cell(1, 1));
List<List<String>> cells = data.cells();
System.out.println("The value is : " + cells.get(1).get(0));
System.out.println("The value is : " + cells.get(1).get(1));
}
}
Just Want to explain MP's answer in detail for others easy understanding-
Yes you wont be able to use raw() method anymore as, its not supported by cucumber api with newer versions of cucumber i.e. io.cucumber. However One can still still use it with older info.cukes dependencies.
So an alternative to raw() is as answered by MP.
For ex- Let's say you have below- Sample gherkin Step:
Then I should see following sections in my app detail page
|Basic Details|Bank Details|Reconciliation|Summarised Options|Currency Code|
>> The Cucumber step definition for above step should be like belwo-
#Then("I should see following sections in my app detail page")
public void verifySectionsOnDetailPageUI(List<List<String>> dTable) {
//Create your table as below (notice- dataTable declared as- List<List<String>> in method argument above)
DataTable data= DataTable.create(dTable);
// to get number of rows from DataTable
int i=data.cells().size();
// To print entire row's column (iterate via for loop using size if u have more than one row defined in data table
System.out.println("All Cells Data: "+data.cells());
//Read cell by cell data as below, row index to be started from 1 if you have column headings provided in ur table
System.out.println(data.cell(0,0));
System.out.println(data.cell(0,1));
System.out.println(data.cell(0,2));
.....
......... so On .. to be used as per your step's objective .....
O/P:
Basic Details
Bank Details
Reconciliation

How to copy or clone mutableList of data object without using collection map in Kotlin

I create copy of one MutableList. When I update element value of copy MutableList But Original List element value also changed. When I use map It working fine but It is like a iteration of whole list, Is any way to do achieve without iteration ? how to copy elements of the MutableList.
val array: MutableList<UserData> = ArrayList()
val userData = UserData("DataOne")
array.add(userData)
val arrayCopy = ImmutableList.copyOf(array)// not working
//val arrayCopy = array.toMutableList()// not working
// val arrayCopy = array.map { it.copy() }.toMutableList()//working
Log.i("----> array ", array[0].name)//print DataOne
Log.i("----> arrayCopy ", arrayCopy[0].name)//print DataOne
arrayCopy[0].name = "DataTwo"
Log.d("----> array ", array[0].name)//print DataTwo
Log.d("----> arrayCopy", arrayCopy[0].name) //print DataTwo
ImmutableList.copyOf does copy the list. The problem is that you want to copy elements of the list as well. Of course you have to iterate over the list to do that, but so does copyOf and I don't see why you expect it's possible to avoid. A slightly better version of map is
array.mapTo(mutableListOf()) { it.copy() }
because it iterates only once.
Sorry but there wouldn't be any other way cause to convert one element you will have to read/copy it once, for n number of element you'll have to iterate n times to get the proper copy.
The only other way I can think of is to create the desired immutable/mutable list in the first place and not copying it all at once later.
Hope this helps

When a variable added into an ArrayList changes, does ArrayList update on its own?

private static ArrayList<ArrayList<Integer>> listlist = new ArrayList<ArrayList<Integer>>();
private static ArrayList<Integer> list = new ArrayList<Integer>();
private static ArrayList<Integer> temp = new ArrayList<Integer>();
public static void main(String args[]) {
list.add(10);
list.add(20);
listlist.add(list);
temp = list;
temp.add(30);
listlist.set(0,temp);
System.out.println("size before temp is cleared: " + listlist.get(0).size());
temp.clear();
System.out.println("size after temp is cleared: " + listlist.get(0).size());
}
Output:
size before temp is cleared: 3
size after temp is cleared: 0
Here is a simplified portion of my code that is causing the problem. You can see how temp is being added to the listlist, and when temp is cleared after that action, the listlist seems to update itself and clear its contents in index 0 as well.
Is it true that the ArrayList updates itself whenever a variable used to insert a value is changed? Help needed.
The arrayList will keep references to the things you give it. In your case, temp and list are both references to the same object (So there is no need to do the set in listlist). When you do temp.clear, the list object referenced by "temp" and "list" (Which is the same) gets cleared. Your list of lists keeps a reference to that same object. It's not "updating itself automatically". It doesnt need to, it never made a copy. Im attaching a diagram that might help you understand. The circles represent objects. the arrows mean that a reference exists to the object being pointed (Either contained in another obect or its yours and i wrote the name/identifier)

dini: how to return key, but not key value?

I need return list of keys, but next code return me only keys values.
string confpath = buildPath(getcwd, "config.ini");
if (!exists(confpath)) throw new Exception("ERROR: config.ini do not exists");
auto config = Ini.Parse(confpath);
foreach (key; config.keys())
{
writeln(key);
}
config.ini:
images = C:\images
photos = D:\photos
pictures = E:\stuff\pictures
Expected output:
images
photos
pictures
code output:
C:\images
D:\photos
E:\stuff\pictures
I looked at sources, but do not found where I can return only keys.
In dini, the keys property returns the _keys associative array, which is a
string[string].
So your foreach should be:
foreach (key, value; config.keys())
{
writeln(key);
}
Alternatively, you can call the associative arrays keys property to get just the keys.
Edit:
IMO, the naming here is a little confusing. I'd personally call dini's keys function "asMap" or something like this, making it obvious that your getting back a mapping of keys=values.
If you use my ini wrapper you can return the keys by .keys from IniSection. IMO "dini" is not that good and offers a "non-userfriendly" inifile wrapper. Besides it doesn't follow SafeD which IMO an ini wrapper definitely should as you shouldn't need pointers for parsing a text-format.
Ex.
auto keys = ini.getSection("Root").keys;
Or .values for the values.
You can get it here:
https://github.com/BaussProjects/baussini/

Is it possible to add selector text to a By object

I am wondering if it is possible to add selector text to an existing By object.
That is, suppose I have a popup with a class name ".popup". But then I want to be a little more specific as the webapp is utilized. Now, our popup has an added class name ".cart". My new selector wants to be ".popup.cart". Here is what I have. Is there a better or more efficient way to do this? (I'm coding in C#, but it can be relevant to any language.)
By selector= By.CssSelector("div.popup");
selector = By.CssSelector(
selector.ToString().Remove(0, selector.ToString().LastIndexOf(":") + 1)
+ ".cart");
What is going on above, selector becomes "div.popup".
selector.ToString() returns "By.CssSelector: div.popup"
So I remove all text from the beginning to the last ":" and add the remaining text to ".cart".
selector then becomes "div.popup.cart", which is what I want.
The problem comes when I try to combine any selectors that have been "ByChained." The ToString() method will return By.Chained([By.CssSelector: .popup, By.CssSelector: .title])
If it helps, I am also Sizzle. So, if there's a JQuery way to do this, I'm open.
Put the "div.popup" in a string say "str" like this:-
String str = div.popup
Then you can append ".cart" to the string, make it "div.popup.cart", and use the cssSelector as:
By selector= By.cssSelector(str+".cart");
Note:- Above is a java code.
As mentioned above, I'm basically solving this by unrolling the By.CssSelector().ToString() output and/or the ByChained().ToString() output. There are some caveats. Mainly that the last selector in a ByChained() must be of type By.CssSelector (since I'm adding a JQuery pseudo class):
private static By AddPseudoFunction(By selector, string pseudoFunc)
{
string type = selector.GetType().ToString();
string selectorValue = selector.ToString();
if (type.Contains("Chained"))
{
selectorValue = selectorValue.Remove(0, selector.ToString().IndexOf("[") + 1);
int start = selectorValue.LastIndexOf("]");
int delta = selectorValue.Length - start;
selectorValue = selectorValue.Remove(start, delta);
}
StringBuilder builder = new StringBuilder();
foreach (string s in selectorValue.Split(','))
{
builder.Append(" ");
builder.Append(s.ToString().Remove(0, s.ToString().LastIndexOf(":") + 2));
}
selectorValue = builder.ToString().TrimStart(' ') + pseudoFunc;
return new ByJQuery.ByJQuerySelector(selectorValue, true);
}
Of course, if you want to make this so that it will return a By.CssSelector, just replace the return with:
return By.CssSelector(selctorValue + addSelector);
Where addSelector would be a valid CSS Selector value (like what I describe in the question).