Which Is Efficient Arraylist Or Hashmap
Jan 30, 2014
my project flow is html,css,js<--->ajax<-->jsp<--->jdbc(.java)<--->mysql. In that jdbc i am returning the records from the database and fill into the resultset. From the resultset i put all records in arraylist and passed to jsp then i am displaying data in the screen.Whether i can use arraylist, hashmap or treetable?
View Replies
ADVERTISEMENT
Aug 20, 2014
I am kind of new to Java. I have a query.. look at it. Have an xml like
<ProductList>
<Product Quality='Good' color='Blue'>
<Details ItemID='1001'/>
</Product>
...
..
</ProductList>
The <Product> tags can run into hundreds or thousands. Quality can have values like Good, Bad, Damaged. Color can also have various values (Blue, Red ..)
ItemID can repeat or can be different. So I want group by ItemID->Color->Good count or BadCount and TotalCount as below.
I want to get a sum like
<ProductList>
<ProductType ItemID="1001">
<Product_Detail CountGood="1" CountBad="2" CountTotal="3" Type="Blue"/>
</ProductType>
...
...
</ProductList>
I think using Hashmaps (Hashmap1 for ItemID, Hashmap2 for color, Hashmap3 for quality) may do the work. But I have to parse the xml number of ItemID's multiply-by number of colors multiply by various colors times for this.
Any better algorithm is there in performance perspective using Java.
View Replies
View Related
Jul 10, 2014
What are the difference between hashmap, linkedlist and arraylist ... when they are used and why?
View Replies
View Related
Apr 6, 2014
I am working on the Kevin Bacon - 6 degrees of bacon problem. I want to take my Array List and use the index and Values that I have saved in it as the Key and Value of a Hashmap. Below is my code.
HashMap<Integer, ArrayList<String>> actors =
new HashMap<Integer, ArrayList<String>>();
ArrayList array = new ArrayList();
try (BufferedReader br = new BufferedReader(
new FileReader("actors.txt"));)
[code]...
saving each one in it's own spot. I want to use the index of each one as the key to find it in the Hashmap....
View Replies
View Related
Apr 2, 2015
I need assigning the selected hashmap values into a list and to display the values in a jsf page. The user will select certificates and will be stored in the below list:
private List<String> selectedCertificates = new ArrayList<String>();
The selectedCertificates will have the values ("AA","BB"). Now I will be passing the list into a hashmap in order to get the names and to display them on the jsf page.
My code is below:
Map<String, String> CertificatesNames =
new HashMap<String,String>(selectedCertificates.size());
CertificatesNames.put("AA", "Certificate A");
CertificatesNames.put("BB", "Certificate B");
CertificatesNames.put("CC", "Certificate C");
for(String key1: selectedCertificates) {
System.out.println(CertificatesNames.get(key1));
}
I want to display in my jsf page :
Certificate A
Certificate B
Now my issue is that is how to display all the values of the CertificatesNames.get(key1) in the jsf page as I tried to do the below and it printed all the values when I used the #{mybean.beans} using the :
List beans =
new ArrayList(CertificatesNames.values());
So how to do this?
View Replies
View Related
Jan 31, 2015
if one address point on another address. so set and get methods will be less efficient then an array, but add or remove will be more efficient by a linked list ? a linked list also inherit from queue so if i add an elemnt via "addFirst" function . where its adding the element ? to the right most or left most ?
if i have for example :
here [ ] --> [ ] --> [ ] --> [ ] -->[ ] or here
linked list its FIFO so the head will be the right most ?
Edit : its confused me a little bit but i understood now .so it will be at the left most. its actually ordered . not like the stack which is LIFO.
View Replies
View Related
Jan 23, 2014
My GCD testing:
Java Code:
public static long gcd(long a, long b) {
if(b == 0) {
return a;
} else {
return gcd(b, a % b);
}
} mh_sh_highlight_all('java');
Is there a more efficient way? Also, what can I do to speed this forloop up?
Java Code:
for(long i = 1; i < 750000; i++) { dothis; } mh_sh_highlight_all('java');
View Replies
View Related
May 27, 2015
I've a question on rounding doubles on 0.05 As far as i know there is no class method that is able to do this. To achieve this rounding i have the following code:
Example:
164.67 -> 164.70
173.33 -> 173.35
0.02 -> 0.05
double calculatedQuantity = 0.0;
// Formula to calculate the average working hours in a month
int totalWeeksInYear = 52;
int totalMonthsInYear = 12;
[Code].....
View Replies
View Related
Oct 22, 2014
I'm looking for a way to gather data from a site page. all data is shown in the same page... I am trying to get the content and parse it is a bit crazy as data seems to be not organized. Itried to get it as a document but still looks crazy.
As all data is shown very clearly in the page (I would like every row to be an object) I'm sure there is some way to collect this data easily. (the data is from this page: [URL] ....)
I'll attach a snapshot and the content I got from the website.
in-play.jpgall_in-play_page.txtin-play.jpgall_in-play_page.txt
View Replies
View Related
Oct 18, 2014
This isn't solely related to java.awt.Color, it's more of a question on how to store methods or redirections to use methods on any value passed in. To explain my question I'll describe a scenario:
ClassA is an instanstiatable class
ClassA has an instance of ClassB called cB
ClassA has methods of affecting cB
ClassD is also an instanstiatable class
ClassD has an Array[x][y] of instances of ClassA
I want to create a ClassE which I would initiate it with references to ClassA's methods or their actual methods and then I can use ClassE to easily transform ClassD's instances of ClassA by the same methods.
What I'm actually doing:
I made a class called GameColor, it has methods like changeHue, setRed, setHue, changeBrightness, setSaturation etc, these methods affect an instance of java.awt.Color in the GameColor class instances, so like they can change the hue or set it with changeHue or setHue etc...
Now I want to do something like perform a single method on a whole BufferedImage, on each of it's pixels RGB. So, I'm thinking of creating a GameColorEffects class which I store references or actual methods of GameColor like changeHue, and then I use GameColorEffects somehow to change a whole BufferedImage more efficiently, what's the most efficient way I can store methods or their reference of GameColor and then later apply them on each pixel of a BufferedImage using GameColorEffects. So all I do is initiate GameColorEffects with methods or reference from GameColor, and then I can apply it to easily transform each pixel.
View Replies
View Related
Feb 13, 2015
with arrays its binary search which finds a value in O(Logn) time but what about linked lists ? the most effiecient algorithm will be O(n) ? and i know that binary search cannot be implement on a linked list , therefore , the only way to search a linked list is a linear search ?
View Replies
View Related
Feb 28, 2014
I'm trying to create my own arraylist using Collection. My program doesn't do anything. Obviously, I haven't a clue.
import java.util.Collection;
import java.util.Iterator;
public class MyArrayList<T> implements java.util.Collection<T> {
private int size = 4;
private T[] mArray;
public MyArrayList(String[] args) {
[Code] ....
View Replies
View Related
Jan 11, 2014
Java Code:
class Program {
public static void main (String[] args) {
long max = 200; //add up all primes up to 200
long ans = 0;
for(long x = 2; x < max; x++) {
if(isPrime(x)) {
ans += x;
[Code] ....
This code adds up all of the primes before a certain number (max). Unfortunately, it is much too slow when you get up to bigger numbers - how can I do this more efficiently.
View Replies
View Related
Sep 26, 2014
In my code I read in a file of states and statecapitals then store them into a hashmap. I then ask the user what the capital is for the random state displayed.The problem I am having is getting the value for the random generated state. When I enter the correct capital for the state, it is still being marked incorrect. Here is my code.
Java Code: try {
Scanner scanner = new Scanner(file);
String[] values;
[code]....
View Replies
View Related
Jul 30, 2014
I have two Hasmaps as
Map<String,String> componentValueMap = new HashMap<String,String>();
Map<String,Map<String,String>> componentNameValueMap = new HashMap<String,Map<String,String>>();
I have for loop which are getting values from XML
XML structure as
<Raj>
<user>raj</user>
<password>123</password>
</Raj>
<Dazy>
<user>dazy</user>
<password>123</password>
</Dazy>
Now during first loop it will put user and password in map and after that put map refernce in another map. Same procedure for another values. But during iterating componentNameValueMap , i am getting Raj, Dazy as Key but not getting different values for them. I am getting latest values of Dazy in both Keys.
Because put method of Map<String,String> componentValueMap is replacing values. But I don't to replace them and want to get different values for different keys.
View Replies
View Related
Oct 23, 2014
I am trying to retrieve a object from a hashMap not I am not sure what is wrong. I am trying to calculate if a car was speeding. They're 5 cameras and as they pass each camera I can calculate the speed. They key is camera number and I am sending in a Vehicle object.
Now I am trying to retrieve variables from the Vehicle so I can do the calculations. I am getting the error in the loop in void calculateSpeeding(). The loop is only for testing at the moment.
package online.practice.averageSpeed;
import java.util.Arrays;
import java.util.HashMap;
public class Vehicle {
String licensePlates;
[Code] ....
View Replies
View Related
Mar 28, 2014
I have a hashmap of the form HashMap <String, Set<String>>I am trying to create a method with one argument. The argument is a key for the hashmap, if it exists it should print out the key and the associated values. I'm falling over at even getting it to print the key, it keeps printing all the keys from within the hashmap as I don't know how to load the argument into it. I have this so far
Java Code:
public void printValue(String club)
{
boolean result = clubMap.containsKey(club);
if (result)
{
String key = clubMap.keySet(club).toString();
System.out.println(key );
[code]....
View Replies
View Related
Dec 25, 2014
Suppose i have a hashMap which includes instances of class Employees and instances of class Customers.
How can i get the employees objects only?
And would it be possible to find the oldest staff by comparing the ages stored in the age fields of the staff objects.
View Replies
View Related
Apr 5, 2014
I am asked in my assignment to make a program that accepts a text file as an example a novel and i have to sort each word as a PERSON or ORGANIZATION or LOCATION or O as in Other , example :
Microsoft/ORGANIZATION ,/O Nelly/PERSON !/O '/O
Now we notice that microsoft is and organitzation and "," is Other and Nelly is a person's name and so on ..
Now I am asked to return the numbers of tags in the text which is 4 in our case because we have (ORGANIZATION,PERSON,LOCATION,OTHER)
My question here is my logic true ? And since i made a map of String,String ; Is there any way that i can get the size of the values in our case the values are the organization etc.. ?
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
[code].....
View Replies
View Related
Feb 9, 2014
I have one doubt.In HashMap if keys contains 1,2,3,4 and values are a,b,c,d we can get values using get(key) method like 1 will A,2 will return B and so on. Can we get the keys from values like A will get 1 and also if in key if there is a String like 1,2,3,Z and value is A,B,C,7 Z should get me 7. Here I am not using any generics.
View Replies
View Related
Jul 25, 2014
How get method of HashMap or Hashtable works internally in Java can any body let me know how does it work..
View Replies
View Related
Feb 15, 2015
I have a pretty large Hashmap (~250MB). Creating it takes about 50-55 seconds, so I decided to serialize it and save it to a file. Reading from the file takes about 16-17 seconds now.
The only problem is that lookups seems to be slower this way. I always thought that the hashmap is read from the file into the memory, so the performance should be the same compared to the case when I create the hashmap myself, right? Here is the code I am using to read the hashmap into a file:
File file = new File("omaha.ser");
FileInputStream f = new FileInputStream(file);
ObjectInputStream s = new ObjectInputStream(new BufferedInputStream(f));
omahaMap = (HashMap<Long, Integer>) s.readObject();
s.close();
300 million lookups take about 3.1 seconds when I create the hashmap myself, and about 8.5 seconds when I read the hashmap from file.
View Replies
View Related
Dec 1, 2014
SO my current code creates a graph with vertices and weighted edges. This data is stored in a hashmap. The key of the hashmap is the vertex and the value is a second hashmap. This second hashmap contains the edges with the vertex it connected to as the key and the weight as the value. My current problem is that when i try to remove vertices they are removed from the key set but they stay in the value(the second hashmap) as the key for that hashmap. IS THERE A WAY TO REMOVE THE VERTEX FROM THE KEYSET OF THE SECOND HASHMAP.
Code is as follows
constructor{
adjacencyMap = new HashMap<V, HashMap<V, Integer>>();
dataMap = new HashSet<V>();
}
removal method{
if(dataMap.contains(vertex)){
[Code]...
View Replies
View Related
Jul 2, 2014
I have a a hashmap in request attribute which is available to jsp file where i have a dropdown. Hashmap kay= value of this dropdown. As and when I select CLT i want to perform some opertation depending on value associated with that key in hashmap.
View Replies
View Related
Aug 18, 2014
I have a HashMap returned from the server. There are two conditions
1. HashMap returned with only one set of key value pair or
2. HashMap with multiple set of data key value pairs.
Now in UI I have to display either text box or drop down box depending upon the size of map for that I am using length method
Java Code:
<c:choose>
<c:when test="${fn:length(myDto.mayMapInDto) eq 1}">
display text box
</c:when>
<c:otherwise>
display drop box
</c:otherwise>
</c:choose> mh_sh_highlight_all('java');
I can display drop box by looping but not sure how I can get only one element for text box. Tricky is I can't use key value to access since UI don't know what key will be returned.
View Replies
View Related
Feb 10, 2014
So I just want to store a Key in a HashMap which can related to two values. For example, the Key "ABC" related to "Fire" which in turn relates to "Heat".
How can I code this in a HashMap?
View Replies
View Related