Values are overridden and not added - java

The values in the list are overriding in my program. I want to use the same object to add different values.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Scanner;
public class CommonValue {
static int key = 100;
public static void main(String[] args) throws IOException {
HashMap<Integer, ArrayList<String>> map = new HashMap<Integer, ArrayList<String>>();
ArrayList<String> list = new ArrayList<String>();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
StringBuffer sBuffer = new StringBuffer();
Scanner scan = new Scanner(System.in);
String choice = null;
do {
System.out.println("enter the how many element to add");
int numOfElement = Integer.parseInt(reader.readLine());
String userInput;
int i = 0;
do {
// adding element in the list
System.out.println("enter the element to add in the list");
userInput = scan.next();
list.add(userInput);
i++;
} while (i < numOfElement);
// adding list in the map with key
map.put(key, list);
System.out.println(map);
list.clear();
// my intial key is 100 and it will incremented when i am going for another key
key++;
System.out.println("do you want to go for next key");
System.out.println("y or n");
choice = scan.next();
} while (choice.equals("y"));
for (Entry<Integer, ArrayList<String>> entry : map.entrySet()) {
key = entry.getKey();
ArrayList<String> value = entry.getValue();
System.out.println("key" + entry.getKey() + ": value " + entry.getValue());
}
}
}
Output:
enter the how many element to add
2
enter the element to add in the list
a
enter the element to add in the list
x
{100=[a, x]}
do you want to go for next key
y or n
y
enter the how many element to add
1
enter the element to add in the list
z
{100=[z], 101=[z]}
do you want to go for next key
y or n
Actually the output I need is:
{100=[a,x], 101=[z]}

The issue is that you keep adding the same instance of List to the Map without making a copy. This will not work, because clearing the list outside the map also clears the list inside the map - after all, it's the same object.
Replace list.clear(); with list = new ArrayList<String>(); to fix this problem.

You have to instantiate a new List for every Entry in your HashMap.
Currently, you are adding the very same List instance to every entry.
In combination with list.clear() this produces the observed output. The last entries in a (the only!) list will define the output for every key.

Dear you are making mistake at belove line
list.clear();
instead of this just initialize list again with new instance as
list = new ArrayList<String>();

Related

Reduce loop function

I have my program working but I'm just want to reduce it. This is only a small part of my code. I created a list for each zipcode to be assigned to different people. I want to reduce it because I currently have more than 20 lists assigned to the same amount of loop.
public class zipcodes {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
List<Integer> pe1a = new ArrayList<>(Arrays.asList(1547,1549 ));
List<Integer> pe1b = new ArrayList<>(Arrays.asList(1606, 2458));
List<Integer> pe1c = new ArrayList<>(Arrays.asList(3058, 2214, 3895));
System.out.print("Enter the zipcode: ");
int zipCodeNumber = 0;
if (scnr.hasNextInt()) {
zipCodeNumber = scnr.nextInt();
} else {
System.out.println("Please enter a valid ZipCode:");
}
for (Integer list : pe1a) if (zipCodeNumber == list) System.out.println("John");
for (Integer list : pe1c) if (zipCodeNumber == list) System.out.println("Mark");
for (Integer list : pe1d) if (zipCodeNumber == list) System.out.println("Luna");
First at all I suggest to you to use a different data structure. Probably, I your case the best structure is an HashMap. The HashMap will allow you to have a single structure that hold all of your data.
var zipcodeMap = new HashMap<String, HashSet<Integer>>();
zipcodeMap.put("John", new HashSet<>(Arrays.asList(1547,1549)));
zipcodeMap.put("Mark", new HashSet<>(Arrays.asList(1606, 2458)));
zipcodeMap.put("Luna", new HashSet<>(Arrays.asList(3058, 2214, 3895)));
As you can see I put as a key of the HashMap a String object that hold the person name and as a value an HashSet that hold all the zip codes.
Now, that we have an HashMap we can easily replace all of these for-loops with a single forEach().
zipcodeMap.forEach((k, v) -> {
if (v.contains(zipCodeNumber)) {
System.out.println(k);
}
});
In practice, the forEach() go trough each key-value pair and check if the HashSet contains the zipCodeNumber. If the HashSet contains the zipCodeNumber print the key (that's the String object that hold the person name).

How to check the number of keys have a certain value in a hashmap

What I am trying to figure out is how do you check if a hashmap (which in my case could have any number of keys) has only one of a certain value assigned to it. I'm struggling to explain this here.
If I have a hashmap with 10 keys (each is players in a game assigned to have a gamestate depending on what "gamestate" they are in) and there is only one player with the game state IN_GAME, then how do I check that there is in fact only one Key assigned with the value IN_GAME, and there isn't two keys with that value?
Use streams for that:
Map<String, String> data = new HashMap<>();
// adding data
long count = data.values().stream().filter(v -> v.equals("IN_GAME")).count();
Count will return the number of "IN_GAME" values in your map.
If you want to check if there are any duplicates values, the simplest solution is to dump it in a set and compare the result size to the original:
boolean hasDuplicates = new HashSet<>(map.values()).size() < map.size();
Using an Iterator instance seems like solve the problem.
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class Stack{
public static void main(String[] args){
//create a hashmap
HashMap<Integer, String> map = new HashMap<Integer,String>();
//populate with dummy out-game entries
for(int i = 0; i < 8;i++){
map.put(i, "OUT-GAME");
}
// add the last two with in-game value
map.put(8, "IN-GAME");
map.put(9, "IN-GAME");
//declare an iterator on map
Iterator it = map.entrySet().iterator();
//number of time "in-game" is encountered
int check = 0;
//while the iterator has more to go
while(it.hasNext()){
//get the key-value pairs and print them just for checking
//the entries
Map.Entry pair = (Map.Entry<Integer,String>) it.next();
System.out.println(pair.getKey() + " " + pair.getValue());
//if the value "in-game" is encountered increment the check by 1
if(pair.getValue().equals("IN-GAME")){
System.out.println("We have a player in game");
check++;
}
//if "in-game" is encountered more than once, then print an alarm
if(check > 1){
System.out.println("More than 1 player in game. There's something wrong");
}
}
//if there's only one player with "in-game", inform me
if(check == 1){
System.out.println("We have exactly one player in the game");
}
}
}
The above code informs you, there are more than one player with "in-game" property on.

Trying to remove duplicate elements

I'm messing around trying to learn to use HashSets to remove duplicate elements in my output but I'm running into some trouble.
My goal is to select a text file when the program is run and for it to display the words of the text file without duplicates, punctuation, or capital letters. All of it works fine except for removing the duplicates.
This is my first time using a Set like this. Any suggestions as to what I'm missing? Thanks!
Partial text file input for example: "Four score and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty, and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation, or any nation, so conceived and so dedicated, can long endure"
import java.util.Scanner;
import java.util.List;
import java.io.*;
import java.util.*;
import javax.swing.JFileChooser;
public class Lab7 {
public interface OrderedList<T extends Comparable<T>> extends Iterable<T>
{
public void add(T element);
public T removeFront();
public T removeRear();
public int size();
public boolean isEmpty();
public boolean contains(T element);
public Iterator<T> iterator();
}
public static void main(String[] arg) throws FileNotFoundException
{
Scanner scan = null;
JFileChooser chooser = new JFileChooser("../Text");
int returnValue = chooser.showOpenDialog(null);
if( returnValue == JFileChooser.APPROVE_OPTION)
{
File file = chooser.getSelectedFile();
scan = new Scanner(file);
}
else
return;
int count = 0;
Set<String> set = new LinkedHashSet<String>();
while(scan.hasNext())
{
String[] noDuplicate = {scan.next().replaceAll("[\\W]", "").toLowerCase()};
List<String> list = Arrays.asList(noDuplicate);
set.addAll(list);
count++;
}
scan.close();
System.out.println(set);
System.out.println();
System.out.println(chooser.getName() + " has " + count + " words.");
}
}
Your problem is that you are creating a new HashSet every time you read a word using the Scanner, so there is no chance for it to do de-duplication. You can fix it with the following steps. Also, normal HashSet does not retain ordering.
Create the HashSet once, before the Scanner loop.
Use a LinkedHashSet, so that order is preserved in the same order that you added it.
Inside the loop, use set.add(item);. As the other answer mentions, you do not need to create a one-element list.
Adding the code for completeness.
public static void main(String[] arg) throws FileNotFoundException
{
Scanner scan = null;
scan = new Scanner(new File("Input.txt"));
int count = 0;
Set<String> set = new LinkedHashSet<String>();
while(scan.hasNext())
{
String word = scan.next().replaceAll("[\\W]", "").toLowerCase();
set.add(word);
count++;
}
scan.close();
// System.out.println(set);
System.out.println();
System.out.println("Input.txt has " + count + " words.");
// How do I print a set by myself?
for (String word : set) {
// Also remove commas
System.out.println(word.replaceAll(",",""));
}
}
I would do it this way:
Set<String> set = new LinkedHashSet<String>();
while(scan.hasNext())
{
String noDuplicate = scan.next().replaceAll("[\\W]", "").toLowerCase();
set.add(noDuplicate);
}
scan.close();
System.out.println("The text has " + set.size() + " unique words.");
Your solution (Creating a one element array, converting that to a List, and converting that to a HashSet) is extremely inefficient, in addition to being incorrect. Just use the String you're originally working with, and add it to the LinkedHashSet (which will preserve ordering). At the end set.size() will show you the number of unique words in your sentence.

Iterating through an array List and creating new ArrayLists when values are different, is this even possible?

I am fairly new to Java and I have stumbled across a problem I cannot figure out for the life of me. First let me explain what I am trying to do then I will show you the code I have so far.
I have a webservice that returns an array of arrays(which include company and lines of business strings). I wish to transform this into a string list, which I did in the first line of code below. Then I wish to Iterate through the list and every I come across a different value for company, I want to create a new ArrayList and add the associated line of business to the new list. Example output of webservice: 80,80,64,64 (this is presorted so the same companies will always be grouped together) the associated lobs would be 1,2,3,4 respectively. What I want: arraylist[0]: 1,2 arrayList[1]: 3,4
What I have so far:
List coList = Arrays.asList(coArray);
//create list of lists
List<List<String>> listOfLists = new ArrayList<List<String>>();
String cmp = "";
for (int i=0;i<coList.size();i++){//loop over coList and find diff in companies
String currentCo = ((__LOBList)coList.get(i)).getCompany();
String currentLob = ((__LOBList)coList.get(i)).getLobNum();
if(i<coArray.length-1){
String nextCo = ((__LOBList)coList.get(i+1)).getCompany();
if((currentCo.equals(nextCo))){
//do nothing companies are equal
}else{
log("NOT EQUAL"); //insert logic to create a new array??
ArrayList<String> newList = new ArrayList<String>();
// for(int j=0;j<coList.size();j++){
newList.add( ((__LOBList)coList.get(i)).getLobNum());
// }
for(int k=0; k<listOfLists.size();k++){//loop over all lists
for(int l=0;l<listOfLists.get(k).size();l++){ //get first list and loop through
}
listOfLists.add(newList);
}
}
}
}
My problem here is that it is not adding the elements to the new string array. It does correctly loop through coList and I put a log where the companies are not equal so I do know where I need to create a new arrayList but I cannot get it to work for the life of me, please help!
Yes you can do this but it's really annoying to write in Java. Note: This is a brain dead simple in a functional programming language like Clojure or Haskell. It's simply a function called group-by. In java, here's how I'd do this:
Initialize a List of Lists.
Create a last pointer that is a List. This holds the last list you've added to.
Iterate the raw data and populate into the last as long as "nothing's changed". If something has changed, create a new last.
I'll show you how:
package com.sandbox;
import java.util.ArrayList;
import java.util.List;
public class Sandbox {
public static void main(String[] args) {
ArrayList<String> rawInput = new ArrayList<String>();
rawInput.add("80");
rawInput.add("80");
rawInput.add("60");
rawInput.add("60");
new Sandbox().groupBy(rawInput);
}
public void groupBy(List<String> rawInput) {
List<List<String>> output = new ArrayList<>();
List<String> last = null;
for (String field : rawInput) {
if (last == null || !last.get(0).equals(field)) {
last = new ArrayList<String>();
last.add(field);
output.add(last);
} else {
last.add(field);
}
}
for (List<String> strings : output) {
System.out.println(strings);
}
}
}
This outputs:
[80, 80]
[60, 60]
Of course, you can do what the other guys are suggesting but this changes your data type. They're suggesting "the right tool for the job", but they're not mentioning guava's Multimap. This will make your life way easier if you decide to change your data type to a map.
Here's an example of how to use it from this article:
public class MutliMapTest {
public static void main(String... args) {
Multimap<String, String> myMultimap = ArrayListMultimap.create();
// Adding some key/value
myMultimap.put("Fruits", "Bannana");
myMultimap.put("Fruits", "Apple");
myMultimap.put("Fruits", "Pear");
myMultimap.put("Vegetables", "Carrot");
// Getting the size
int size = myMultimap.size();
System.out.println(size); // 4
// Getting values
Collection<string> fruits = myMultimap.get("Fruits");
System.out.println(fruits); // [Bannana, Apple, Pear]
Collection<string> vegetables = myMultimap.get("Vegetables");
System.out.println(vegetables); // [Carrot]
// Iterating over entire Mutlimap
for(String value : myMultimap.values()) {
System.out.println(value);
}
// Removing a single value
myMultimap.remove("Fruits","Pear");
System.out.println(myMultimap.get("Fruits")); // [Bannana, Pear]
// Remove all values for a key
myMultimap.removeAll("Fruits");
System.out.println(myMultimap.get("Fruits")); // [] (Empty Collection!)
}
}
It sounds to me like a better choice would be a Map of Lists. Let the company ID be the key in the Map and append each new item for that company ID to the List that's the value.
Use the right tool for the job. Arrays are too low level.
Create a Map<String, List<Bussiness>>
Each time you retrieve a company name, first check if the key is already in the map. If it is, retrieve the list and add the Bussiness object to it. If it is not, insert the new value when a empty List and insert the value being evaluated.
try to use foreach instead of for
just like
foreach(List firstGroup in listOfLists)
foreach(String s in firstGroup)
............
Thanks for the input everyone!
I ended up going with a list of lists:
import java.util.*;
import search.LOBList;
public class arraySearch {
/**
* #param args
*/
public static void main(String[] args) {
LOBList test = new LOBList();
test.setCompany("80");
test.setLOB("106");
LOBList test1 = new LOBList();
test1.setCompany("80");
test1.setLOB("601");
LOBList test2 = new LOBList();
test2.setCompany("80");
test2.setLOB("602");
LOBList test3 = new LOBList();
test3.setCompany("90");
test3.setLOB("102");
LOBList test4 = new LOBList();
test4.setCompany("90");
test4.setLOB("102");
LOBList test5 = new LOBList();
test5.setCompany("100");
test5.setLOB("102");
LOBList BREAK = new LOBList();
BREAK.setCompany("BREAK");
BREAK.setLOB("BREAK");
BREAK.setcompany_lob("BREAK");
// create arraylist
ArrayList<LOBList> arlst=new ArrayList<LOBList>();
// populate the list
arlst.add(0,test);
arlst.add(1,test1);
arlst.add(2,test2);
arlst.add(3,test3);
arlst.add(4,test4);
arlst.add(5,test5);
//declare variables
int idx = 0;
String nextVal = "";
//loops through list returned from service, inserts 'BREAK' between different groups of companies
for(idx=0;idx<arlst.size();idx++){
String current = arlst.get(idx).getCompany();
if(idx != arlst.size()-1){
String next = arlst.get(idx+1).getCompany();
nextVal = next;
if(!(current.equals(next))){
arlst.add(idx+1,BREAK);
idx++;
}
}
}
//add last break at end of arrayList
arlst.add(arlst.size(),BREAK);
for(int i=0;i<arlst.size();i++){
System.out.println("co:" + arlst.get(i).getCompany());
}
//master array list
ArrayList<ArrayList<LOBList>> mymasterList=new ArrayList<ArrayList<LOBList>>();
mymasterList = searchListCreateNewLists(arlst);
//print log, prints all elements in all arrays
for(int i=0;i<mymasterList.size();i++){
for(int j=0;j<mymasterList.get(i).size();j++){
System.out.println("search method: " + mymasterList.get(i).get(j).getCompany());
}
System.out.println("end of current list");
}
}
//method to loop over company array, finds break, creates new array list for each company group,
//adds this to a list of lists(masterList)
public static ArrayList<ArrayList<LOBList>> searchListCreateNewLists(ArrayList<LOBList> list){
ArrayList<ArrayList<LOBList>> masterList=new ArrayList<ArrayList<LOBList>>();
int end = 0;
int start = 0;
int index = 0;
for(int i=0;i<list.size();i++){
if(list.get(i).getCompany().equals("BREAK")){
end = i;//end is current index
masterList.add(new ArrayList<LOBList>());
for(int j = start;j<end;j++){
masterList.get(index).add(list.get(j));
}
index++;
start = i+1;
}
}
return masterList;
}
}
The output is:
search method: 80
search method: 80
search method: 80
end of current list
search method: 90
search method: 90
end of current list
search method: 100
end of current list
So all company LOBList objects with Company: 80, are grouped together in a list, as are 90 and 100.
To iterate through the list you can use
ListIterator litr = coList.listIterator();
while(litr.hasNext()){
}

Sorting an ArrayList<String> in a TreeMap

I am piping in a file. I am tracking word pairs from the file. Using a treemap the keys are all sorted. However, when i add words to those keys they are not sorted.
here is the part i need help on in the process function:
private static void process(){
if(!result.containsKey(thisWord)){
result.put(thisWord, new ArrayList<String>());
}
// Add nextWord to the list of adjacent words to thisWord:
result.get(thisWord).add(nextWord); // nextword is not sorted within the key
thisword is sorted
nextWord is not..
Can i use Collections.sort(result); somehow?
im just not sure how i get to the nextWord within the result to do that.
or, is there no way to do it within my situation. I would rather not change things unless you recommend it.
This is the program
import java.util.Map.Entry;
import java.util.TreeSet;
import java.io.*;
import java.util.*;
public class program1 {
private static List<String> inputWords = new ArrayList<String>();
private static Map<String, List<String>> result = new TreeMap<String, List<String>>();
public static void main(String[] args) {
collectInput();
process();
generateOutput();
}
private static void collectInput(){
Scanner sc = new Scanner(System.in);
String word;
while (sc.hasNext()) { // is there another word?
word = sc.next(); // get next word
if (word.equals("---"))
{
break;
}
inputWords.add(word);
}
}
private static void process(){
// Iterate through every word in our input list
for(int i = 0; i < inputWords.size() - 1; i++){
// Create references to this word and next word:
String thisWord = inputWords.get(i);
String nextWord = inputWords.get(i+1);
// If this word is not in the result Map yet,
// then add it and create a new empy list for it.
if(!result.containsKey(thisWord)){
result.put(thisWord, new ArrayList<String>());
}
// Add nextWord to the list of adjacent words to thisWord:
result.get(thisWord).add(nextWord); // need to sort nextword
// Collections.sort(result);
}
}
private static void generateOutput()
{
for(Entry e : result.entrySet()){
System.out.println(e.getKey() + ":");
// Count the number of unique instances in the list:
Map<String, Integer> count = new HashMap<String, Integer>();
List<String> words = (List)e.getValue();
for(String s : words){
if(!count.containsKey(s)){
count.put(s, 1);
}
else{
count.put(s, count.get(s) + 1);
}
}
// Print the occurances of following symbols:
for(Entry f : count.entrySet()){
System.out.println(" " + f.getKey() + ", " + f.getValue() );
}
}
System.out.println();
}
}
If you want the collection of "nextword"s sorted, why not use a TreeSet rather than an ArrayList? The only reason I can see against it is if you might have duplicates. If duplicates are allowed, then yes, use Collections.sort on the ArrayList when you're done adding to them. Or look in the Apache Commons or Google collection classes - I don't know them off the top of my head, but I'm sure there is a sorted List that allows duplicates in one or both of them.
result.get(thisWord).add(nextWord);
Collections.sort(result.get(thisWord));
Y Don't you try some thing like this
Collections.sort(inputWords);

Categories