Can element inside both arraylist be added together? - java

is there anyway where i can add the element inside both List 19 and List 20 together?
i have both of the element in integer form and by using List.get i can get the number. but i have trouble adding them in together as they are from different list and different if/else statement. is there anyway that i could add them up without changing the algorithm here?
List11.add(driver
.findElement(
By.xpath("html/body/table[2]/tbody/tr/td[4]/table/tbody/tr[3]/td/table/tbody/tr[2]/td[2]/div/span"))
.getAttribute("class"));
if (List11.get(0).equals(List10.get(0))) {
String c = "1";
ArrayList<String> List19 = null;
List19 = new ArrayList<String>();
List19.add(c);
System.out.println(List19.get(0));
System.out.println("Access Right add + 1point");
} else {
String c = "0";
ArrayList<String> List19 = null;
List19 = new ArrayList<String>();
List19.add(c);
System.out.println(List19.get(0));
System.out.println("Access Right no point will be added");
}
List12 = new ArrayList<String>();
System.out
.println("Edit profile: "
+ driver.findElement(
By.xpath("html/body/table[2]/tbody/tr/td[4]/table/tbody/tr[3]/td/table/tbody/tr[3]/td[2]/div/span"))
.getAttribute("class"));
List12.add(driver
.findElement(
By.xpath("html/body/table[2]/tbody/tr/td[4]/table/tbody/tr[3]/td/table/tbody/tr[3]/td[2]/div/span"))
.getAttribute("class"));
if (List12.get(0).equals(List10.get(0))) {
String d = "2";
ArrayList<String> List20 = null;
List20 = new ArrayList<String>();
List20.add(d);
System.out.println(List20.get(0));
System.out.println("Access Right add + 2point");
} else {
String d = "0";
ArrayList<String> List20 = null;
List20 = new ArrayList<String>();
List20.add(d);
System.out.println(List20.get(0));
System.out.println("Access Right no point will be added");
}

Related

Arraylist contain, & retain all not giving the same result as Sql inner join

Sql output : 312
Sql Script inner join:
SELECT count(*) FROM COST_TYPE_SELECTION CST
LEFT JOIN GBL_HYPACCT_RSLV_TBL_NEW_CA GBL
ON GBL.TREE_NODE = CST.GL_TREE_NODE
AND GBL.TREE_LEVEL = CST.GL_TREE_LEVEL
LEFT JOIN DIM_HYP_PCCODE_FLATTEN DIM
ON CST.PC_TREE_LEVEL = DIM.TREE_LEVEL
AND CST.PC_TREE_NODE = DIM.TREE_LEVEL_NODE
AND DIM.LEGAL_ENTITY='007'
AND DIM.PC_CODE='0200'
and CST.COST_TYPE='Direct_Expenses'
INNER JOIN EPM_CONSOLIDATED_LEDGER_M EPM
ON EPM.LEGAL_ENTITY = DIM.LEGAL_ENTITY
AND EPM.gl_profit_centre_cd = DIM.PC_CODE
AND EPM.GL_ACCOUNT_NUMBER = GBL.TREE_LEAF
WHERE EPM.sourcedataloccd='SG'
Jdbc Script 1 : storing the GLaccount in arraylist Gllishive
SELECT GBL.TREE_LEAF FROM COST_TYPE_SELECTION CST
LEFT JOIN GBL_HYPACCT_RSLV_TBL_NEW_CA GBL
ON GBL.TREE_NODE = CST.GL_TREE_NODE
AND GBL.TREE_LEVEL = CST.GL_TREE_LEVEL
LEFT JOIN DIM_HYP_PCCODE_FLATTEN DIM
ON CST.PC_TREE_LEVEL = DIM.TREE_LEVEL
AND CST.PC_TREE_NODE = DIM.TREE_LEVEL_NODE
AND DIM.LEGAL_ENTITY='007'
AND DIM.PC_CODE='0200'
Jdbc Script 2 : storing the GLaccount in arraylist GllistMaria
select gl_account from EPM_CONSOLIDATED_LEDGER_M EPM
ON EPM.LEGAL_ENTITY ='007' AND EPM.gl_profit_centre_cd = '7482'
WHERE EPM.sourcedataloccd='SG'
Then comparing the using below code
System.out.println("size of hive: " + Gllishive.size());
System.out.println("size of maria join: " + GllistMaria.size());
List<String> joins = new ArrayList<String>(GllistMaria);
joins.retainAll(Gllishive);
System.out.println("after retailall: " + joins.size());
ArrayList<String> MatchingGL = new ArrayList<String>();
for(int i=0;i<GllistMaria.size();i++){
if(Gllishive.contains(GllistMaria.get(i))){
MatchingGL.add(GllistMaria.get(i));
}
}
System.out.println("after contains using for loop :"+MatchingGL);
Output I'm getting : 873 in both retain all and contains method was not matching with sql output : 312
Complete Code below
public void Script1(String ss) {
try{
String consolm = "SELECT * FROM COST_TYPE_SELECTION CST\n" +
"LEFT JOIN GBL_HYPACCT_RSLV_TBL_NEW_CA GBL ON GBL.TREE_NODE = CST.GL_TREE_NODE AND GBL.TREE_LEVEL = CST.GL_TREE_LEVEL\n" +
"LEFT JOIN DIM_HYP_PCCODE_FLATTEN DIM ON CST.PC_TREE_LEVEL = DIM.TREE_LEVEL AND CST.PC_TREE_NODE = DIM.TREE_LEVEL_NODE\n" +
"AND DIM.LEGAL_ENTITY='007'\n" +
"AND DIM.PC_CODE=?" +
"and CST.COST_TYPE='Direct_Expenses'\n";
PreparedStatement sta2 = con.prepareStatement(consolm);
sta2.setString(1, ss);
ResultSet result3 = sta2.executeQuery();
HashMap<String, List<String>> map = new HashMap<>();
HashMap<String, List<String>> map5 = new HashMap<>();
HashMap<String, List<String>> map6 = new HashMap<>();
List<String> glAcs = new ArrayList<>();
List<String> glAcs2 = new ArrayList<>();
List<String> glAcs3 = new ArrayList<>();
List<String> glAcs4 = new ArrayList<>();
while(result3.next()){
String de = result3.getString("gbl.descr");
if(map.get(de)!=null){
glAcs = map.get(de);
glAcs.add(result3.getString("gbl.tree_leaf"));
map.put(de,glAcs);
data.add(result3.getString("gbl.tree_leaf"));
}else{
List<String> glAcs1 = new ArrayList<>();
glAcs1.add(result3.getString("gbl.tree_leaf"));
map.put(de,glAcs1);
data.add(result3.getString("gbl.tree_leaf"));
}
String pe = result3.getString("gbl.descr");
if(map5.get(pe)!=null){
glAcs3 = map5.get(pe);
glAcs3.add(result3.getString("cst.legal_entity"));
map5.put(pe,glAcs3);
data22.add(result3.getString("cst.legal_entity"));
}
else{ List<String> glAcs13 = new ArrayList<>();
glAcs13.add(result3.getString("cst.legal_entity"));
map5.put(pe,glAcs13);
data22.add(result3.getString("cst.legal_entity"));
}
String he = result3.getString("gbl.descr");
if(map6.get(he)!=null){
glAcs4 = map6.get(he);
glAcs4.add(result3.getString("dim.pc_code"));
map6.put(he,glAcs4);
data33.add(result3.getString("dim.pc_code"));
}
else{
List<String> glAcs14 = new ArrayList<>();
glAcs14.add(result3.getString("dim.pc_code"));
map6.put(he,glAcs14);
data33.add(result3.getString("dim.pc_code"));
}
}
Set<String> hs = map.keySet();
Iterator itr = hs.iterator();
while(itr.hasNext()){
String key = itr.next().toString();
Glac.addAll(map.get(key));
}
System.out.println(" Final count of hive was :"+Glac.size());
System.out.println(" Final count of hive was :"+data.size());
Set<String> hs22 = map5.keySet();
Iterator itr22 = hs22.iterator();
while(itr22.hasNext()){
String key = itr22.next().toString();
legen.addAll(map5.get(key));
}
System.out.println(" Final count of hive was :"+legen.size());
System.out.println(" Final count of hive was :"+data22.size());
Set<String> hs33 = map6.keySet();
Iterator itr33 = hs33.iterator();
while(itr33.hasNext()){
String key = itr33.next().toString();
pc.addAll(map6.get(key));
}
System.out.println(" Final count of hive was :"+pc.size());
System.out.println(" Final count of hive was :"+data33.size());
con.close();
}catch(ClassNotFoundException |IOException e){
}catch (SQLException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
System.out.println(e.toString());
}
}
public void script2(String ss) {
try{
String consolm = "select * from EPM_CONSOLIDATED_LEDGER_M EPM\n" +
"WHERE EPM.sourcedataloccd='SG'AND EPM.primarysourcesyscd NOT IN('EPMCA','EPMTX','EPMOS')\n" +
"AND EPM.businessdt='20181130' AND EPM.rundt='20181204' AND EPM.GL_BU_CD IN ('DBU','ACU')\n" +
"AND EPM.LEGAL_ENTITY = '007'\n" +
"AND EPM.gl_profit_centre_cd = ?";
PreparedStatement sta2 = con.prepareStatement(consolm);
sta2.setString(1, ss);
ResultSet result3 = sta2.executeQuery();
HashMap<String, List<String>> map = new HashMap<>();
HashMap<String, List<String>> map3 = new HashMap<>();
HashMap<String, List<String>> map4 = new HashMap<>();
List<String> glAcs2 = new ArrayList<>();
List<String> glAcs3 = new ArrayList<>();
List<String> glAcs4 = new ArrayList<>();
while(result3.next())
{
String de = result3.getString("epm.product_cd_hyperion");
if(map.get(de)!=null){
glAcs2 = map.get(de);
glAcs2.add(result3.getString("epm.gl_account_number"));
map.put(de,glAcs2);
data2.add(result3.getString("epm.gl_account_number"));
}else{
List<String> glAcs12 = new ArrayList<>();
glAcs12.add(result3.getString("epm.gl_account_number"));
map.put(de,glAcs12);
data2.add(result3.getString("epm.gl_account_number"));
}
String pe = result3.getString("epm.product_cd_hyperion");
if(map3.get(pe)!=null){
glAcs3 = map3.get(pe);
glAcs3.add(result3.getString("epm.legal_entity"));
map3.put(pe,glAcs3);
data3.add(result3.getString("epm.legal_entity"));
}
else{ List<String> glAcs13 = new ArrayList<>();
glAcs13.add(result3.getString("epm.legal_entity"));
map3.put(pe,glAcs13);
data3.add(result3.getString("epm.legal_entity"));
}
String he = result3.getString("epm.product_cd_hyperion");
if(map4.get(he)!=null){
glAcs4 = map4.get(he);
glAcs4.add(result3.getString("epm.gl_profit_centre_cd"));
map4.put(he,glAcs4);
data4.add(result3.getString("epm.gl_profit_centre_cd"));
}
else{
List<String> glAcs14 = new ArrayList<>();
glAcs14.add(result3.getString("epm.gl_profit_centre_cd"));
map4.put(he,glAcs14);
data4.add(result3.getString("epm.gl_profit_centre_cd"));
}
}
Set<String> hs = map.keySet();
Iterator itr = hs.iterator();
while(itr.hasNext()){
String key = itr.next().toString();
Glac2.addAll(map.get(key));
}
System.out.println(" Final count of hive2 was :"+Glac2.size());
System.out.println(" Final count of hive2 was :"+data2.size());
Set<String> hs2 = map3.keySet();
Iterator itr2 = hs2.iterator();
while(itr2.hasNext()){
String key = itr2.next().toString();
legen2.addAll(map3.get(key));
}
System.out.println(" Final count of hive3 was :"+legen2.size());
System.out.println(" Final count of hive3 was :"+data3.size());
Set<String> hs3 = map4.keySet();
Iterator itr3 = hs3.iterator();
while(itr3.hasNext()){
String key = itr3.next().toString();
pc2.addAll(map4.get(key));
}
System.out.println(" Final count of hive4 was :"+pc2.size());
System.out.println(" Final count of hive4 was :"+data4.size());
con.close();
}catch(ClassNotFoundException |IOException e){
}catch (SQLException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
System.out.println(e.toString());
}
}
#Test
public void Innerjoin() throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {
Script1("0200");
script2("0200");
ArrayList<String> MatchingGL = new ArrayList<String>();
for (int i = 0; i < Glac2.size(); i++) {
if (Glac.contains(Glac2.get(i))) {
MatchingGL.add(Glac2.get(i));
}
}
System.out.println("after contains using for loop :" + MatchingGL.size());
ArrayList<String> MatchingGL2 = new ArrayList<String>();
for (int i = 0; i < Glac2.size(); i++) {
if (legen.contains(legen2.get(i))&&pc.contains(pc2.get(i))
&&Glac.contains(Glac2.get(i))) {
MatchingGL2.add(Glac2.get(i));
}
}
System.out.println("after contains using for loop :" + MatchingGL2.size());
}
Lets assume for the 1st query that COST_TYPE_SELECTION contains 10 valid rows and that the LEFT JOIN with DIM_HYP_PCCODE_FLATTEN returns 5 rows from DIM.
This means the inner join with EPM_CONSOLIDATED_LEDGER_M will be done against 5 rows so if even if all rows are matched in this join the total number of rows for the query will be 5.
With the same data the first jdbc query will return all 10 rows since there is no inner join, how many rows the second jdbc query will return can not be determined but it feels safe to say from your results that it would be much more than 5 rows.
So to solve this in java you need to include all columns used in the inner join and then perform the same match as in the inner join between the lists for all those columns.

How to Iterate Twice over Map values in Java

I know there are other solutions out there but nothing is working for me.
Question: In my main method, I group together IDs by rating and make the rating the key and the rest of the info the value as a List. When I create the hashmap and put in the lists I can accurately print the contents of the hashmap. However, once I pass the map the evaluate method, the values are lost and I cannot iterate in the same way that I did in the main method, even though the logic is the same. I am not experienced with the Map class in java. Can somebody please help me figure out why when I pass the Map to my evaluate method that I can no longer iterate the Map?
import java.io.*;
import java.util.*;
public class Evaluate {
public static double grandTotal;
public static void main(String[] args) throws Exception {
FileInputStream fs = new FileInputStream("testInput.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fs));
FileInputStream fs2 = new FileInputStream("testTest.txt");
BufferedReader br2 = new BufferedReader(new InputStreamReader(fs2));
String line;
String line2;
String[] bloop;
String bleep;
String flooper;
String splitter;
String[] splitInput;
List<String> oneStarList= new ArrayList<String>();
List<String> twoStarList= new ArrayList<String>();
List<String> threeStarList= new ArrayList<String>();
List<String> fourStarList= new ArrayList<String>();
List<String> fiveStarList= new ArrayList<String>();
List<String> values2 = new ArrayList<String>();
try {
while ((line=br.readLine()) != null) {
bloop = new String[10];
bloop = line.split("\\s+");
bleep = bloop[1].toString();
flooper = (bloop[0]+" "+bloop[2]+" "+bloop[3]+" "+bloop[4]);
if (bleep.equals("1")){
oneStarList.add(flooper);
}
else if (bleep.equals("2")){
twoStarList.add(flooper);
}
else if (bleep.equals("3")){
threeStarList.add(flooper);
}
else if (bleep.equals("4")){
fourStarList.add(flooper);
}
else if (bleep.equals("5")){
fiveStarList.add(flooper);
}
grandTotal+=(Double.parseDouble(bloop[2]));
}
}
catch (Exception e){
}
Map<String,List<String>> hmap = new HashMap<String,List<String>>();
hmap.put("1",oneStarList);
hmap.put("2", twoStarList);
hmap.put("3", threeStarList);
hmap.put("4", fourStarList);
hmap.put("5", fiveStarList);
while ((line2=br2.readLine()) != null) {
splitInput = new String[5];
splitInput = line2.split("\\s+");
evaluate(splitInput[0],splitInput[1],hmap);
}
br.close();
br2.close();
}
public static void evaluate(String movID, String usrID, Map<String,List<String>> hash) throws Exception{
FileWriter fw = new FileWriter("outputTest.txt");
BufferedWriter bwr = new BufferedWriter(fw);
List<String> values = new ArrayList<String>();
List<String> outputList = new ArrayList<String>();
String[] floop;
String fleep;
int movIDtotal=0;
int usrIDtotal=0;
int totalValues=0;
double pmovIDStar=0;
double pusrIDStar=0;
double pmovID=0;
double pusrID=0;
double numID=0;
double keyTotalProb=0;
String keyOutputStr;
String keyHold;
final Set<Map.Entry<String,List<String>>> entries = hash.entrySet();
for (String key : hash.keySet()){
values = hash.get(key);
System.out.println(key + ":");
for (int i=0;i<values.size();i++){
System.out.println(values.get(i));
floop = new String[5];
fleep = values.get(i);
floop = fleep.split("\\s+");
if (movID.equals(floop[0])){
movIDtotal++;
totalValues++;
}
if (usrID.equals(floop[0])){
usrIDtotal++;
totalValues++;
}
}
values.clear();
}
for (Map.Entry<String, List<String>> entry: entries){
values= entry.getValue();
keyHold = entry.getKey();
for (int j=0;j<values.size();j++){
floop = new String[5];
fleep = values.get(j);
floop = fleep.split("\\s+");
if (movID.equals(floop[0])){
pmovIDStar = Double.parseDouble(floop[3]);
numID = Double.parseDouble(floop[1]);
pmovID = (numID/movIDtotal);
}
if (usrID.equals(floop[0])){
pusrIDStar = Double.parseDouble(floop[3]);
numID = Double.parseDouble(floop[1]);
pusrID = (numID/usrIDtotal);
}
}
keyTotalProb = ((totalValues/grandTotal)*(pmovIDStar)*(pusrIDStar))/(pusrID*pmovID);
keyOutputStr = Double.toString(keyTotalProb);
outputList.add(keyHold);
outputList.add(keyOutputStr);
values.clear();
}
double max = Double.MIN_VALUE;
for (int m=0;m<outputList.size();m+=2){
double coolguy = Double.parseDouble(outputList.get(m+1));
int index = 0;
if(coolguy>max){
max = coolguy;
index = m;
}
try {
bwr.write(String.format("%-1s %-1s %-1s%n", movID,usrID,outputList.get(index)));
bwr.close();
fw.close();
}
catch(Exception e) {
}
}
}
}
Backup info: I'm trying to build a java program that essentially performs the final stage of the Naive Bayes algorithm to predict user ratings (1-5) for movies. I have used MapReduce to train data and now I have an input file where each line contains a string containing information in this order without the commas (movie or user id,rating , number of times rating and ID occur together in total, number of times ID occurs in total, probability that ID and rating occur together out of all ratings for ID). Essentially this is the classification stage.
never suppress excetions. especially when you do coding/debugging.
catch (Exception e){ } is very bad practice
When you do:
final Set<Map.Entry<String,List<String>>> entries = hash.entrySet();
it does not copy hash.entrySet to entries. It creates another reference to it.
same is for values= entry.getValue();
then what do you expect after your first loop (and others too)?
when you do:
values.clear();
your values gone from the lists which are in hash and since entries is just a reference to hash.entrySet() you have what you've done - empty lists.

Output is Displayed in console and not in the File

try {
BufferedReader sc = new BufferedReader(new FileReader("/home/aravind/Desktop/India.txt"));
ArrayList<String> name = new ArrayList<>();
ArrayList<String> Location = new ArrayList<>();
ArrayList<String> Id = new ArrayList<>();
ArrayList<String> Details = new ArrayList<>();
String line = " ";
while ((line = sc.readLine()) != null) {
if (!line.trim().equals("")) {
System.out.println(line);
if (line.toLowerCase().contains("name")) {
name.add(line.split(":")[1].trim());
}
if (line.toLowerCase().contains("Location")) {
Location.add(line.split(":")[1].trim());
}
if (line.toLowerCase().contains("Id")) {
Id.add(line.split(":")[1].trim());
}
if (line.toLowerCase().contains("Details")) {
Details.add(line.split(":")[1].trim());
}
}
}
for (int i = 0; i < name.size(); i++) {
PrintWriter out = new PrintWriter(newFileWriter("output.csv"));
out.println("name;Location;Id;Details;");
out.println(name.get(i) + ";"
+ Location.get(i) + ";"
+ Id.get(i) + ";"
+ Details.get(i) + ";");
out.close();
}
sc.close();
} catch (Exception e) {
}
and my input file looks like
name = abc
id = 123
Place = xyz
Details = some texts with two line
name = aaa
id = 54657
Place = dfd
Details = some texts with some lines
What could be the problem why it is not printing in csv file instead prints o/p in console..Kindly help me
In your file, title and value are always separated by "=", whereas at runtime you trim strings by ":". You should replace ":" by "=", thus your trim result will not be empty at index 1.:
name.add(line.split("=")[1].trim());

java split string into string array produce first value wrong

Input string:
-;Lokacija;-;Pozicija;Grad;-;-;
Code:
public static ArrayList<String> sortList = new ArrayList<String>();
//Load
String Row = new String("-;Lokacija;-;Pozicija;Grad;-;-;");
String[] RowAsList;
RowAsList = Row.split(";");
sortList.add( RowAsList[0] );
// Check
StringBuffer minus = new StringBuffer( "-");
String itm = sortList.get(0);
if( !itm.contentEquals( minus ) )
// not minus
else
.....
Problem: this code says there is no minus on first item (0), subsequent minuses are recognized correctly.
Anyone has any ideas as to why ?
Thanks,
Kajko
Remove the static modifier before the sortList member:
public List<String> sortList = new ArrayList<String>();
Thats what you want
public class Test
{
public static ArrayList<String> sortList = new ArrayList<String>();
public static void main(String a[])
{
//Load
String Row = new String("-;Lokacija;-;Pozicija;Grad;-;-;");
String[] RowAsList;
RowAsList = Row.split(";");
for(int i=0;i<RowAsList.length-1;i++) {
sortList.add(RowAsList[i]);
}
System.out.println(sortList);
// Check
StringBuffer minus = new StringBuffer("-");
String itm = sortList.get(0);
if( itm.contentEquals( minus ) )
System.out.println(sortList.get(0));
// not minus
else
System.out.println("not found...");
}
}
Try :
String Row = new String("-;Lokacija;-;Pozicija;Grad;-;-;");
ArrayList<String> sortList = new ArrayList<String>();
String[] RowAsList= Row .split(";");
sortList.add( RowAsList[0]);
StringBuffer minus = new StringBuffer( "-");
String itm = sortList.get(0);
if( itm.equals( minus.toString())){
System.out.println("Get Here" + itm);
}
else{
System.out.println("Not Here"+ itm);
}

Duplicate entries inside loop of list<String>

I am using the following code and at the end i saw in calssEntries list the last values duplicated
i mean if i debug it i can see the right data but in the second iteration the values of the first data entries
are override and i see the second twice and so on
what i miss here?
String memberName = null;
String memberValue = null;
List<String> memberList = new ArrayList<String>();
List<String> memberValueList = new ArrayList<String>();+ArrayList<ClassEntry> calssEntries = new ArrayList<ClassEntry>();
...
while (dataRow != null) {
memberList.clear();
memberValueList.clear();
for (int i = 1; i < dataArray.length; i += 2) {
memberName = dataArray[i];
memberList.add(memberName);
memberValue = dataArray[i + 1];
memberValueList.add(memberValue);
}
ClassEntry classEntry = new ClassEntry();
classEntry.setClassName(className);
classEntry.setMemberName(memberList);
classEntry.setMemberValue(memberValueList);
calssEntries.add(classEntry);
....
I think the problem is that you need to create a new instance of memberList and memberValueList in the while loop. Something like:
calssEntries = new ArrayList<ClassEntry>();
...
while (dataRow != null) {
List<String> memberList = new ArrayList<String>();
List<String> memberValueList = new ArrayList<String>();
...
...
In your code, entries of calssEntries are referring to the same(single) instance of memberList and memberValue.
try this
for (int i = 1; i < dataArray.length; i += 2) {
int j = i;
memberName = dataArray[i];
memberList.add(memberName);
memberValue = dataArray[j + 1];
memberValueList.add(memberValue);
}

Categories