Using Arraylist Index And Items As Hashmap Key And Values?
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....
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.
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());
First i want to read the text file and I want to count the number of occurrence of each item and after that i want store the result in hashmap structure...
I am working on a class that sorts and matches items in a collection of arrays. In the end, elements common to all 3 arrays should be printed. The idea is that as the first array is compared to the second it stores the matched items in tempArray. When all items are compared the tempArray should overwrite the checked array, and the process continues until all arrays are checked. I have set all the checking in a do loop that should run while the value is <= to the array length. This allows all items in the reference array to be checked. I get an index out of bounds message but not where I would expect it. I have tried varying the condition in the do while loop in the match method, but it did not change the result. There may be other issues I have not addressed with solving the algorithm, but this one has me stumped and I am not able to progress.
package testing; public class TestMatchMain { public static void main(String[] args) { Comparable[] innerCollection0 = {1,2,3,4,5}; Comparable[] innerCollection1 = {1,1,5,6,7};
I'm trying to search a library program by book index and running into some trouble with my HashMap. Here's the code in question:
Java Code:
public String searchByBookIndex(int input) { NumberFormat fmt = NumberFormat.getCurrencyInstance(); Map<Integer, Book> bookMap = new HashMap<>(); for (int i = 0; i < author.booksByAuthor.size(); i++) { bookMap.put(author.booksByAuthor.get(i).getBookIndex(), book);
[Code] ....
The search itself works fine; however, the problem is that the values of everything in the HashMap are overwritten by the last book entry. For example, let's say there are four books (index in parenthesis):
The Book (1), A Wonderful Book (2), A Great Book (3), A Bad Book (4).
Once the code above iterates, the HashMap out is this (via println):
1 = A Bad Book, 2 = A Bad Book, 3 = A Bad Book, 4 = A Bad Book.
I try to create a jsf project within ejb which is add new car with entering attributes listing attributes and search by make.
Add and list methods working well , but have problem of list method. I tryed many combinations (using enhanced loop, iterative loop) but i cant provide working well. Always outputText returns nothing ,when i enter attributes.
Here is ejb code for adding ,getting and listing car items :
@Stateful public class CarsBusiness implements CarsBusinessLocal { List<Car> cars; public CarsBusiness() { cars = new ArrayList<Car>();
My current lesson in Java is about ArrayLists and I'm having a tough time to understand this bit of code: This exercise is concerned with the problem of deleting a block of items from an ArrayList.
public static void deleteBlock( ArrayList<String> strings, int n ) { for ( int i = 0; i < n; i++ ) { if ( strings.size() > 0 ) strings.remove( i );
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?
I managed to retrieve data, and set data in my own ways in which I like. But my problem is, if the file does not contain anything (fully empty), when I try to use my
set("", ""); method, it only sets the last one that is called.
Basically I am trying to make a POS system and need a way to print a receipt whether there are 1 items sold or 50. So I used a Arraylist. This is the format for the printer that is needed:
printer.printNormal(POSPrinterConst.PTR_S_RECEIPT, " 2 200 Tx Hashi Chop Sticks" + LF);
This is what I tried to do:
ArrayList<String> receiptLine = new ArrayList<>(); public void captureSales(){ receiptLine.add("Test"); System.out.println("Size of the receipt list is: "+receiptLine.size()); System.out.println("Element at index 1: " + receiptLine.get(0));
I managed to retrieve data, and set data in my own ways in which I like. But my problem is, if the file does not contain anything (fully empty), when I try to use my
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.
I have a 2D array and the elements are listed as follows:
outlook temperature humidity windy gooutside sunny hot high false n overcast hot high false y ....
I need to put these values into a HashMap, where the elements of the first row are the keys and the elements from row 1 to n-1 are the values. What would be the best way to make sure the key and values are matched correctly?
Currently working on a simple file processing problem, namely, reorder a set of strings (called bid phrases) in terms of the score value associated to them in descending order.
Here is my HashMap and a method for listing all the keys in it
HashMap<String, String> exampleOne = new HashMap<String, String>(); public void allKeys() { int i; i =0; for (String name: exampleOne.keySet())
[Code]....
Now I want to return all values that associated with one key. How do I do this? Or is it possible to other way round? I mean return All keys associated with a value?
So I am working on an assignment and ran into an annoying bug. Basically i have a menu that accesses an ArrayList of Videos which may or may not be read from a file, one of the options of the menu is to edit an existing Video. For this I ask the user for the number of the video and it is checked against the list of video numbers if it returns a match, the method gets the index of the Video object and stores it in a temporary variable the user is allowed to edit the details and the object is put back into the ArrayList using the variable and the ArrayList's set() method
My problem is that once i finish editing the details of a video it gives me a indexOutOfBounds exception
On further investigation using a method that goes through the index of every object in the ArrayList using indexOf() i found out that every single object has been given the index of -1 and not 0,1,2,3 etc.. This is my first error and have not expierenced any other before.
The objects do exist because I have read them from a file. I can also add new Objects and view them successfully but they still have the same index . I have checked my syntax and everything and no errors, this happens at run time only.
I have even created some other ArrayLists seperately and debugged them and their index order is fine. I am too far into this project to start over. I've also tried cleaning the project(my IDE is Eclipse).
How could I check if an index is exist in an array list? I mean, I should enter an integer and it should return me a boolean result that saying whether if that entered value is an index or not.
I am just studying over ArrayLists and i have encountered a problem, i'm trying to get the index of an element but it keeps returning -1. Here's the code
Java Code:
import java.util.ArrayList; public class PhoneEntry { String name; String phone; public PhoneEntry(String name, String phone) {
[Code] ...
I know the list is not empty because it returns a size.
and the corresponding snippets of the Java code are:
// Class member variables // ... private String product; private ArrayList<String> productValues; private ArrayList<String> productLabels; private SelectItem[] products; // ... Various properties etc. public String getLocation() { // Displayed on a page
[code]....
Most of this works correctly using only ArrayList SelectItem products without the two ArrayList and the separate SelectItem[], and the values and labels are put directly into products here. The menu works and I can select an item. However, I am unable to find the correct method for finding the index in the submit method,namely:
public void submit(ActionEvent e) { showProduct = true; prodNum = products.indexOf(product); // --- Here is the problem! updateProduct(); }
which has not been changed here. In spite of trying out various ideas, prodNum always returns with -1, which means it cannot find the index of the selected product, where product is a String. Everything else seems to work correctly, and products.get(prodNum).getLabel() works if I manually give prodNum a valid index, but because it's -1 it fails.
I have been trying to get this method to work for a few hours now, for some reason I get an IndexOutOfBounds exception every time. Basically I am trying to read a txt file and add the words to an ArrayList as strings separated by a space .
private ArrayList<String> readLinesFromFile(BufferedReader inputFile) throws IOException { String value = null; ArrayList<String> result = new ArrayList<String>(); while((value = inputFile.readLine()) != null){ String[] values = value.split(" "); for (int i = 0; i < values.length; i++){ result.add(values[i]); } } return result; }
I'm a total newbie to Java, and until now all I've done was draw some shapes and flags. I'm struggling to understand the code I've been given. I need to access values stored in an ArrayList within another class. Here are the two classes Seat and Mandate:
package wtf2; import java.util.*; public class Seat { public int index; public String place; public int electorate;
[Code] ....
The main class contains code that feeds data from 2 text files into Seat and Mandate. From there I managed to access the date in Seat (at the end):
package wtf2; import java.io.*; import java.util.*; public class CW2 { public static void main(String[] args)throws Exception {
[Code] ....
Now,instead of getting just the mp for Edinburgh South I need to get the vote values, compare them to each other, take the second biggest and display the associate party value. How to access data from that Array to get started at least.