sorry if this is an stupid question or anything; but I'm having a problem looping through two different objects in java. one of them is a hashmap and the other one is an arraylist. the objects look something like this:
Map<String,Integer> myMap = new HashMap();
ArrayList<String> myArray = new ArrayList();
I need to loop through each item in "myArray" and check if the item exists in one of "myMap"'s keys.
I actually know how to do that but the way I do it, contains a lot of unnecessary looping and I want to know if there is an faster way to do it or not.
the way I do it:
Map<String,Integer> myMap = new HashMap();
ArrayList<String> myArray = new ArrayList();
for(String i:myArray) {
for(String j:myMap.keySet()) {
if(i == j) {
myMap.put(j, myMap.get(j) + 1)
}
}
}
In Java 8 and later you can use a stream (I didn't see the increment, thx jhamon for the advice):
myArray.stream().filter(myMap::containsKey).forEach(
item -> myMap.put(item, myMap.get(item) + 1)
);
You can alternativley use Map#computeIfPresent to get a one-liner like:
myArray.forEach(str -> myMap.computeIfPresent(str, (k,v) -> v+1) );
Following is the simple working example.
public class TestSample {
public static void main(String[] args) {
Map<String,Integer> myMap = new HashMap();
myMap.put("some", 0);
myMap.put("string", 0);
ArrayList<String> myArray = new ArrayList<String>(Arrays.asList("some", "some", "string", "to", "check"));
for(String i:myArray) {
int val = myMap.getOrDefault(i, -1);
if (val != -1)
myMap.put(i, val+1);
}
System.out.println(myMap);
}
}
Related
I have an ArrayList which contains several String[]. I want to loop through the ArrayList and group the inner String[] that contain a matching value. In this case, I'm looking specifically at the second value in the String[] (ex. "CompanyA").
I'm having difficulty understanding what the best logical pattern for this is and how it might be implemented. Any help is greatly appreciated.
List<String[]> attArrayList = new ArrayList<String[]>();
String[] entry1 = { "EventOne", "CompanyA", "Matthew" };
String[] entry2= { "EventOne", "CompanyA", "Mary" };
String[] entry3 = { "EventOne", "CompanyB", "Bates" };
String[] entry4 = { "EventOne", "CompanyC", "Carson" };
attArrayList.add(entry1);
attArrayList.add(entry2);
attArrayList.add(entry3);
attArrayList.add(entry4);
for (int i = 0; i < attArrayList.size(); i++) {
// ...
}
My desired result is something like this:
[[EventOne, CompanyA, Matthew], [EventOne, CompanyA, Mary]], [EventOne, CompanyB, Bates], [EventOne, CompanyC, Carson]
Map<String, ArrayList<String[]>> map = new TreeMap<>();
for (String[] strs : attArrayList) {
if (!map.containsKey(strs[1])) {
ArrayList<String[]> list = new ArrayList<>();
list.add(strs);
map.put(strs[1], list);
} else {
map.get(strs[1]).add(strs);
map.put(strs[1], map.get(strs[1]));
}
}
for (String[] strs : map.get("CompanyA")) {
System.out.println(Arrays.toString(strs));
}
You can try this.I think it`s easier to understand than using stream(though stream has less code).
You could use groupingBy to group the items based on the second column:
Map<String, List<String[]>> groups = attArrayList.stream()
.collect(groupingBy(arr -> arr[2]));
I am a bit stuck with my application, and I am not quite sure what to search for. So I am hoping someone here may help me out.
I have a list of Strings, that looks like this:
Cake;carrot
Cake;apple
Cake;spicy
Pizza;pepperoni
Pizza;mozzarella
... and so on. I want to put this data into a Map<String, List<String>>, where Cake and Pizza will make up the keys in my Map. Having [carrot, apple, spicy] as Cake's values, and [pepperoni, mozzarella] as Pizza's values.
How may I achieve this? Thanks in advance for any help.
Just iterate over your list using String.split()
ArrayList<String> myList;
HashMap<String, List<String>> myMap = new HashMap<>();
for(String s : myList)
{
String[] split = s.split(";");
List<String> bucket = myMap.get(split[0]);
if(bucket == null)
{
bucket = new ArrayList<String>();
myMap.put(split[0], bucket);
}
bucket.add(split[1]);
}
You can try this, use a hashmap, store consecutive strings with (space) as the delimiter, finally split the string when you want it as a list
//Assuming your list to be the variable 'list'
HashMap<String,String> hm = new HashMap<>();
for(val : list){
String st[] = val.split(";");
if(hm.get(st[0])==null){
hm.put(st[0],st[1]);
}
else{
hm.put(st[0],hm.get(st[0])+" "+st[1]);
}
}
when You want the string array of say pizza back then
String pizz[] = (hm.get("pizza")).split(" ");
pizz[] will have your array, cheers!
public static void main(String[] args) {
List<String> data = new ArrayList<String>();
Map<String, List<String>> finalData = new HashMap<String,List<String>>();
data.add("Cake;carrot");
data.add("Cake;apple");
data.add("Cake;spicy");
data.add("Pizza;pepperoni");
data.add("Pizza;mozzarella");
for (String dataString : data) {
List<String> temp = null;
if (finalData.get(dataString.split(";")[0]) == null) {
temp = new ArrayList<String>();
temp.add(dataString.split(";")[1]);
finalData.put(dataString.split(";")[0], temp);
} else {
temp = finalData.get(dataString.split(";")[0]);
temp.add(dataString.split(";")[1]);
finalData.put(dataString.split(";")[0], temp);
}
}
System.out.println(new Gson().toJson(finalData));
}
Complete working solution.
Method :
public void itemAmountCollection() {
Map<String, List<Integer>> orderItemDetails = new LinkedHashMap<String, List<Integer>>();
ArrayList<Integer> itemsAmount = new ArrayList<Integer>();
WebElement orderItemTable = driver.findElement(By
.xpath("//*[#id='tblInfo']/tbody"));
List<WebElement> noOfItems = orderItemTable.findElements(By
.tagName("tr"));
for (int i = 1; i <= noOfItems.size(); i++) {
String itemAmount = driver.findElement(
By.xpath("//*[#id='tblInfo']/tbody/tr[" + i
+ "]/td[8]")).getText();
itemsAmount.add(Integer.parseInt(itemAmount));
orderItemDetails.put("amount", itemsAmount);
}
}
with above method we collected all the item amount with Map Collections and Output for the above method is (345,7905,345)
how can we add all the values in an particular Key (amount)
Expected Output :
8595 (i.e 345+7905+345)
I don't really get what you mean, but I'm amusing that you're trying to add all values in a List. To do this:
int result = 0;
for(int i : itemsAmount)
{
result+=1;
}
System.out.println(result);//This should print 8595.
In general Map<Key,List<Value>> structures end up needing code that looks as follows:
public addValue(Key key, Value value) {
if (!map.containsKey(key)) {
map.put(key, new ArrayList<>());
}
map.get(key).add(value);
}
In your case you should replace orderItemDetails.put with similar code.
Alternatively you could use a true Multimap from a third party library such as guava.
Summing the values would simply be:
map.get(key).stream().sum();
Assuming that the values are List which makes the stream an IntStream.
I have a problem concerning a HashMap in Java. To explain the problem in a detailed way, i will first post some code you can refer to.
public void BLASTroute(String args[]) throws IOException, InterruptedException{
...
correctMapping CM = new correctMapping();
CM.correctMapping(RB.BLASTresults, exists);
CalculateNewConsensusSequence CNCS =
new CalculateNewConsensusSequence();
char[] consensus = CNCS.calculateNewConsensusSequence(
CM.newSeq, CM.remindGaps, EMBLreaderReference.sequence, exists);
HashMap<Integer, ArrayList<String>> gapsFused =
new HashMap<Integer, ArrayList<String>>();
for (Integer i : CM.remindGaps.keySet()) {
ArrayList<String> newList = CM.remindGaps.get(i);
gapsFused.put(i, newList);
}
GenerateGeneLists GGL = new GenerateGeneLists(
EMBLreaderReference, CM.newSeq, gapsFused, exists,
GQList, allMappedPositions);
System.out.println(CM.remindGaps.hashCode());
gapsFused=GGL.generateGeneListSNP(gapsFused);
System.out.println(CM.remindGaps.hashCode());
System.out.println(gapsFused.hashCode());
GGL.generateGeneListFrameShift(gapsFused);
}
The following occurs:
in my class correctMapping, i fill a global variable called remindGaps. I use it later in some functions, and nothing happens/everything works as expected.
Then, i make a copy of the HashMap called gapsFused (i don't know if this has something to do with my problem).
Now comes the interesting part: In the class GenerateGeneLists, i don't do a thing with the remindGaps HashMap.
However, after the function generateGeneListSNP is performed, remindGaps changed! I'll post the code for you as well, so that you can help me better:
public GenerateGeneLists(EMBL_reader EMBLreaderReference,
HashMap<String,ArrayList<String>> newSeq,
HashMap<Integer,ArrayList<String>> gapsFused, File exists,
ArrayList<GeneQualifier> GQlist,
HashMap<Integer,Integer> allMappedPositions)
throws InterruptedException{
this.EMBLreaderReference=EMBLreaderReference;
this.newSeq=newSeq;
//this.gapsFused=gapsFused;
this.exists=exists;
this.GQlist=GQlist;
this.allMappedPositions=allMappedPositions;
for (GeneQualifier GQ : this.GQlist){
startlist.add(GQ.start);
stoplist.add(GQ.stop);
startMap.put(GQ.start,GQ);
}
}
public HashMap<Integer,ArrayList<String>> generateGeneListSNP(
HashMap<Integer,ArrayList<String>> gapsFused)
throws IOException{
File GQSNP = new File (exists+"/GQsnp.txt");
BufferedWriter SNP = new BufferedWriter(new FileWriter(GQSNP));
SNP.write("#Gene_start\tGene_stop\tlocus_tag\tproduct" +
"\tputative_SNP_positions(putative_changes)\n");
HashMap<GeneQualifier,ArrayList<Integer>> GQreminder =
new HashMap<GeneQualifier,ArrayList<Integer>>();
for (String s : newSeq.keySet()){
ArrayList<String> blub = newSeq.get(s);
char[] qrySeq = blub.get(0).toCharArray();
char[] refSeq = blub.get(1).toCharArray();
int start = Integer.valueOf(blub.get(2));
int stop = Integer.valueOf(blub.get(3));
for (int i=0;i<refSeq.length;i++){
if (qrySeq[i]!=refSeq[i]&&qrySeq[i]!='-'&&qrySeq[i]!='.'){
if (mismatchList.containsKey(start+i)){
ArrayList<Character> blah = mismatchList.get(start+i);
blah.add(qrySeq[i]);
mismatchList.put(start+i, blah);
}
else {
ArrayList<Character> blah = new ArrayList<Character>();
blah.add(qrySeq[i]);
mismatchList.put(start+i,blah);
}
}
else if (qrySeq[i]!=refSeq[i]&&(qrySeq[i]=='-'||qrySeq[i]=='.')){
if (!gapsFused.containsKey(start+i)){
ArrayList<String> qwer = new ArrayList<String>();
qwer.add(String.valueOf(qrySeq[i]));
gapsFused.put(start+i,qwer);
}
else {
ArrayList<String> qwer = gapsFused.get(start+i);
qwer.add(String.valueOf(qrySeq[i]));
gapsFused.put(start+i,qwer);
}
if (!deletionPositionsAndCount.containsKey((start+i))){
int count = 1;
deletionPositionsAndCount.put(start+i, count);
}
else {
int count = deletionPositionsAndCount.get(start+i);
count = count+1;
deletionPositionsAndCount.put(start+i, count);
}
}
}
}
for (Integer a : mismatchList.keySet()){
for (int i=0;i<startlist.size();i++){
int start = startlist.get(i);
int stop = stoplist.get(i);
if (a>=start && a<=stop){
GeneQualifier GQ = startMap.get(start);
if (!GQreminder.containsKey(GQ)){
ArrayList save = new ArrayList<Integer>();
save.add(a);
GQreminder.put(GQ,save);
}
else {
ArrayList save = GQreminder.get(GQ);
save.add(a);
GQreminder.put(GQ,save);
}
break;
}
}
}
for (GeneQualifier GQ : GQreminder.keySet()) {
ArrayList<Integer> save = GQreminder.get(GQ);
int start = GQ.start;
int stop = GQ.stop;
String locus_tag =
GQ.geneFeatures.get("locus_tag").get(0).replace("\n", "");
String product =
GQ.geneFeatures.get("product").get(0).replace("\n", "");
SNP.write(start + "\t" + stop + "\t" + locus_tag +
"\t" + product + "\t");
boolean end = false;
for (int i = 0; i < save.size(); i++) {
if (i==save.size()-1) end=true;
int posi = save.get(i);
SNP.write(posi + "(");
ArrayList<Character> mismatches = mismatchList.get(posi);
for (int j = 0; j < mismatches.size(); j++) {
char snipp = mismatches.get(j);
if (j == mismatches.size() - 1) {
SNP.write(snipp + ")");
} else {
SNP.write(snipp + ",");
}
}
if (end == false){
SNP.write(",");
}
}
SNP.write("\n");
}
SNP.close();
return gapsFused;
}
As you can see, remindGaps is not used in this class, but still it undergoes changes. Do you have an idea why this is the case?
What I tested is, whether remindGaps changes if i manually change gapsFused (the made copy of the first HashMap). This is not the case, so i don't think that the copying process went wrong (for example only points to the other HashMap or references it).
I would really appreciate your ideas and help in order to solve this problem.
You have to remember that in Java all objects are passed as reference. So, when you did:
ArrayList<String> newList = CM.remindGaps.get(i);
you basically pointed newList to the same list as contained in the remindGaps map. Now, even though you work with the gapsFused, any changes to its values effect the same underlying list in the memory - to which both remindGaps and gapsFused are pointing.
Change your copy code to the following and see if it makes a difference:
ArrayList<String> newList = new ArrayList<String>(CM.remindGaps.get(i));
By doing this, you are creating a new list that newList will be pointing to and thus the changes will be encapsulated.
Your code is very long and hard to read (mainly because it doesn't respect Java naming conventions), but my guess is that your problem comes from the fact that your copy of the map simply copies the ArrayList references from one map to another:
HashMap<Integer, ArrayList<String>> gapsFused = new HashMap<Integer, ArrayList<String>>();
for (Integer i : CM.remindGaps.keySet()) {
ArrayList<String> newList = CM.remindGaps.get(i);
gapsFused.put(i, newList);
}
In the above code, you don't create any new list. You just store the same lists in another map. If you need a new list, the code should be:
Map<Integer, List<String>> gapsFused = new HashMap<Integer, List<String>>();
for (Integer i : CM.remindGaps.keySet()) {
List<String> newList = new ArrayList<STring>(CM.remindGaps.get(i));
gapsFused.put(i, newList);
}
Without analyzing all your code:
HashMap<Integer, ArrayList<String>> gapsFused = new HashMap<Integer, ArrayList<String>>();
for (Integer i : CM.remindGaps.keySet()) {
ArrayList<String> newList = CM.remindGaps.get(i);
gapsFused.put(i, newList);
}
After this code gapFused will contain entries that are copies of the entries of remindGaps, therefore those entries will reference the same objects (key and values). So if you add or remove entries in one Map it will have no effect on the other, but if you change a value accessing it through one Map you will see the change also accessing it through the other map (for example remingGaps.get(1).add("hello")).
The name "newList" used in your code is confusing because it is not a new list, just a reference on an existing one...
Since the value of the Map is an ArrayList and you are doing just a shallow copy (meaning the new Map has a reference to the same Lists as are in the first Map) and changes to the lists in the second map would be reflected in the first map. To avoid this you would need to make deep copies of the lists when you create the new Map.
I am having one problem in java arraylist. I am good in databases :) We normally use
"group by" to group rows. I want the same thing but in java for one of my project
I have following format in arraylist
name1:val1
name1:val2
name1:val3
name2:val8
name2:val7
name7:val54
name7:val76
name7:val34
I want to convert this arraylist to give me following output:
-name1
val1
val2
val3
-name2
val8
val7
-name7
.
.
.
val34
this is not a school assignment :). may be for some of Java Guru it looks like a small
thing to do.
I like to do that kind of thing with a map.
import java.util.*;
public class DoIt {
public static void main(String[] args) {
List l = new ArrayList<String>();
l.add("name1:val1");
l.add("name1:val2");
l.add("name1:val3");
l.add("name1:val4");
l.add("name2:val1");
Map results = new HashMap<String,String>();
for (Iterator i = l.iterator(); i.hasNext();) {
String s = (String)i.next();
String[] tmp = s.split(":");
if (!results.containsKey(tmp[0])) {
System.out.println("-"+tmp[0]+"\n"+tmp[1]);
results.put(tmp[0], tmp[1]);
} else {
System.out.println(tmp[1]);
}
}
}
}
Use a Map<String, List<Integer>>. You could use the following snippets:
// declare
Map<String, List<Integer>> m = new HashMap<String, List<Integer>>();
// insert into the structure the pair 'a':2
List<Integer> l = m.get("a");
if ( l == null ) {
l = new ArrayList<Integer>();
m.put("a", l);
}
l.add(2);
// iterate over the values
for (Map<String, List<Integer>>.Entry e : m) {
System.out.println("-" + e.getKey());
for (Integer i : m.getValue()) {
System.out.println(" " + i);
}
}
What you are looking for is called multi-map.
There is no standard multi-map in java.util, but Google collections has implemented it -> here. The project home page is here
Use Comparator
List<Samp> bla = new ArrayList<Samp>();
Collections.sort(bla, new Comparator<Samp>() {
#Override
public int compare(Samp s1, Samp s2) {
return s1.getCategory().compareTo(s2.getCategory());
}
});
then Create 1 List .,. compare if that list already contains the category, if not,
add Category and Name, else just add Name. see code below :)
List<String> catList = new ArrayList<String>();
for(Samp s : bla){
if (!catList.contains(s.getCategory())){
catList.add(s.getCategory());
System.out.println(s.getCategory() + " - ");
System.out.println(s.getName());
} else {
System.out.println(s.getName());
}
}