Adding and getting Comm Ports from RXTX - java

I am currently working on a project which needs to communicate with a device connected on a commport. I have one function below which searches for serial ports, adds them to a hashmap then returns the hashmap. I noticed a problem where whenever I try to get something from the HashMap, it give a java.lang.nullPointerException Am I trying to get the port from the map incorrectly? Please let me know if I need to post more code.
private Enumeration ports = null;
public HashMap<String, CommPortIdentifier> searchForPorts() {
ports = CommPortIdentifier.getPortIdentifiers();
while (ports.hasMoreElements()) {
CommPortIdentifier curPort = (CommPortIdentifier) ports.nextElement();
if (curPort.getPortType() == CommPortIdentifier.PORT_SERIAL) {
System.out.println("Adding: " + curPort.getName() + "-" + curPort);
portMap.put(curPort.getName(), curPort);
System.out.println(portMap.get(curPort.getName())); //works: prints out gnu.io.CommPortIdentifier#9f116cc
}
}
log.fine("Successfully looked for ports");
Iterator it = portMap.entrySet().iterator();
String s="";
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
s = pair.getKey().toString();
System.out.println(s); //prints out COM24 like it should
it.remove();
}
System.out.println(portMap.get(s)); //Prints out null??
return portMap;
}
Portions of this code are taken from here.

You remove elements in your map using it.remove() as it's stated in the javadoc: Removes from the underlying collection the last element returned by this iterator
To access next element using an iterator you just need to use it.next() (assuming there is remaining elements).
Another solution is to use a foreach loop like this:
for(Map.Entry pair : portMap.entrySet()){
s = pair.getKey().toString();
System.out.println(s);
}

Related

How do I find in Java a specific element, for printing, in a list of objects with multiple elements?

Long story short I parsed all the json from here
into a list of objects. But I'm having trouble trying to find a specific object. With all the examples on searching lists online I can't seem to be able to get it.
I ask the user to input a number into int checkId and checkUserId and then compare it. If it matches it should print out the title.
Iterator < Post > iter = posts.iterator();
while (iter.hasNext()) {
if (Objects.equals(iter.next().getUserId(), checkUserId)) {
System.out.println("found UserId");
if (Objects.equals(iter.next().getId(), checkId)) {
System.out.println("found Id");
//prints the title of object
}
}
}
And then I tried to use a stream
List<Post> result = posts.stream()
.filter(title -> checkId.equals(getId()))
.findAny()
.orElse(null);
All the code I cloned it from this great guy. https://github.com/danvega/httpclient-tutorial
Your first attempt does not work because you are advancing the iterator twice on each iteration by calling next. Instead, store the result of Iterator#next and use it.
Iterator<Post> iter = posts.iterator();
while(iter.hasNext()){
Post post = iter.next();
if(Objects.equals(post.getUserId(), checkUserId)) {
System.out.println("found UserId");
System.out.println(post.getTitle());
}
}
With streams:
List<String> titles = posts.stream().filter(post-> checkId.equals(post.getId()))
.map(Post::getTitle).collect(Collectors.toList());
titles.forEach(System.out::println);

Getting key value for Hashmap with If condition

I am storing the key = "name" and value = "state" in a Hashmap.
Now once I get the hashmap with all keys and values
I want to iterate the hashmap and have to check whether the state(value) is running or not
If the state is not running I want to print the name of that server(which is key in hashmap)
Code I am using is
for(int z=0; z<=containsAll.size();z++) {
if(!containsAll.containsValue("Running")) {
System.out.println(containsAll.keySet());
}
}
Here contains all is the name of my Hashmap. Can someone help me in getting the name for which state is not running
if (containsAll != null) {
containsAll.forEach((k, v) -> {
if (v != null && !v.contains("Running")) {
System.out.println(k);
}
});
}
Iterate every key-value pair of the map, and if the value don't contain "Running", print the key.
you can traverse the map using entrySet()
Iterator it = containsAll.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
System.out.println(pair.getKey() + " = " + pair.getValue());
if(!((pair.getValue()).equals("Running")){
System.out.println(pair.getKey()+" is not running");
}
it.remove();
}
I'd make a new class to represent a server and within this class I'd define the state as boolean and the name as string. Furthermore I'd use a list of those Objects to iterate through and do something like this(given, the list is typed List):
...
for(MyServerObject mso : containsAll){
if(mso.isRunning())
System.out.println(mso.getName());
}
...
If this is not possible as you get the Map as is from somewhere else try the following (I'm assuming your Map is typed Map<String,String>):
...
Iterator<Map.Entry<String, String>> iterator = containsAll.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> entry = iterator.next();
if("Running".equalsIgnoreCase(entry.getValue())
System.out.println(entry.getKey() + " is running!");
}
...

How to efficiently delete Multiple Session Keys in Java?

I am trying to optimize below code snippet. I want to clear multiple session keys in java by using a single loop. So the requirement is that I don't want to clear all the session keys, I want to keep some of the session keys. For example, in below code snippet, I am trying to remove keys containing ID_NAME_ and keep the one with the id passed to the method.
Below is the code snippet I wrote which works fine:
private void clearPreviousIdFromSession(HttpServletRequest request, String id) {
HttpSession session = request.getSession();
Enumeration keys = session.getAttributeNames();
ArrayList<String> keyArrs = new ArrayList<>();
while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
System.out.println("Keys for session : " + key);
if (key.contains("ID_NAME_"+id)) {
continue;
} else if(key.contains("ID_NAME_")) {
keyArrs.add(key);
}
}
for(String k : keyArrs){
System.out.println(k);
session.setAttribute(k, null);
session.removeAttribute(k);
}
}
Below is the code I was trying to make it into a single loop, but getting an error because removeAttribute() will remove the object bound with the specified name from this session. In this case, I think key will be removed and won't be able to check for the nextElement in the list.
private void clearPreviousIdFromSession(HttpServletRequest request, String id) {
HttpSession session = request.getSession();
Enumeration keys = session.getAttributeNames();
//ArrayList<String> keyArrs = new ArrayList<>();
String key;
while (keys.hasMoreElements()) {
key = (String) keys.nextElement();
System.out.println("Keys for session : " + key);
if (key.contains("ID_NAME_" + id)) {
continue;
} else if (key.contains("ID_NAME_")) {
session.removeAttribute(key);
}
}
}
Any suggestions to improve my code. I have tried looking here and some old questions of stack overflow questions and here, but still having difficulty. TIA
Your want to optimize something that is already well designed.
As you should not remove elements during iteration of them, you collect elements to delete into a List and after iteration you iterate on this List to delete them.
This approach is nice and the logic flow is "optimized".
If you had to really change something in the actual code, it could be the redundant operation :
session.setAttribute(k, null);
session.removeAttribute(k);
You should choose only one of them :
for(String k : keyArrs){
System.out.println(k);
session.removeAttribute(k);
}
since session.setAttribute(k, null) has the same effect as calling session.removeAttribute(k).
if (key.contains("ID_REPO_" + id)) {
continue;
} else if (key.contains("ID_REPO_")) {
session.removeAttribute(key);
}
refactoring this to java stream:
key.stream()
.filter(key -> !key.contains("ID_REPO_" + id)) //filter all keys that do not have string
.forEach(key -> session.removeAttribute(key)) // execute lambda on every key filtered

How to compare values in different collections that need different type of iteration loops?

Lets say you have an Iterator which will contains values that you need to compare with values that are located in a separate List.
Iterator<Map.Entry<String, Object>> it = aObj.items();
while (it.hasNext()) {
Map.Entry<String, Object> item = it.next();
nameValue = item.getNameValue();
keyValue = item.getKeyValue();
System.out.println("Name: " + nameValue);
System.out.println("Value: " + keyValue);
}
This outputs:
Name: header
Value: 22222
Lets say you have a separate list (in which you want to compare the above values with):
List<Items> items = new ArrayList<>();
for (Item item : items) {
itemNameValue = item.getName();
itemKeyValue = item.getKey();
System.out.println("Name: " + itemNameValue);
System.out.println("Value: " + itemKeyValue);
}
This outputs:
Name: header
Value: 44444
Since these are different types of loops (one is a while loop and the other one is a for each loop)
how can you compare for example:
if (nameValue.equals(itemNameValue())) {
// do something?
}
I need to iterate over both collections / data structures at the same time...
Would this be the solution?
String nameValue = "";
Object keyValue = "";
String itemNameValue = "";
String itemKeyValue = "";
Iterator<Map.Entry<String, Object>> it = aObj.items();
while (it.hasNext()) {
Map.Entry<String, Object> item = it.next();
nameValue = item.getNameValue();
keyValue = item.getKeyValue();
for (Item item : items) {
itemNameValue = item.getName();
itemKeyValue = item.getKey();
}
if (nameValue.equals(itemNameValue())) {
// do something?
}
}
Basically, what I am trying to ask (in a very simplified way is this):
(1) The collection that needs to be iterated in a while loop is just test input (sample data)
(2) The array list from the second collection is really a list of data which was returned from a database call (DAO) and placed into the ArrayList.
I am trying to verify if the input from Iterator inside the while loop is the same as the values from the ArrayList (which came from a database). Since these are different data structures requiring different looping mechanisms. How could I iterate through both data structures at the same time and compare them? The second data structure (the array list) is the actual set of values that are correct.
I don't know if there's a guarantee that each iteration would be comparing the same items if I use a nested loop?
Thank you for taking the time to read this...
The problem you are facing is a direct result of a BAD Application design.
The underline incorrect assumption of this question is that the map and the list will hold the objects in the same sequence.
List --> A data structure that is ordered by not sorted
Map --> A data structure that is neither ordered nor sorted
This is not to say that these two data structures don't work well together. However, using them to store the same list should only result from an awkward program design.
Even though to answer your question, you can use the below code to accomplish this:
Iterator<Map.Entry<String, Object>> it = aObj.items();
List<Items> items = dbCall.getItems(); // Get the list of Items from the DB
int index = 0;
while (it.hasNext()) {
Map.Entry<String, Object> itemFromMap = it.next();
Item itemFromList = items.get(index);
if(itemFromMap.getNameValue().equals(itemFromList.getName()) &&
itemFromMap.getKeyValue().equals(itemFromList.getKey())){
// If you prefer a single .equals() method over &&, then you can implement a Comparator<Item>
return false;
}
index++;
}
return true;

Infinite Loop Iterating Through Hashtable

I'm trying find the most popular word in an array using Hashtables. For some reason the while loop is looping infinitely. I've debugged and the element never changes from the first one it gets. Any ideas on why this is happening?
Here is my code:
import java.util.Hashtable;
public class MyClass {
public String mostPopularString (String []words) {
if (words == null)
return null;
if (words.length == 0)
return null;
Hashtable<String, Integer> wordsHash = new Hashtable<String, Integer>();
for (String thisWord : words)
{
if (wordsHash.containsKey(thisWord))
{
wordsHash.put(thisWord, wordsHash.get(thisWord) + 1);
}
else
{
wordsHash.put(thisWord, 1);
}
}
Integer mostPopularCount = 0;
String mostPopularWord = null;
boolean tie = false;
while (wordsHash.keys().hasMoreElements())
{
String currentWord = (String) wordsHash.keys().nextElement();
if (wordsHash.get(currentWord) > mostPopularCount)
{
mostPopularCount = wordsHash.get(currentWord);
mostPopularWord = currentWord;
tie = false;
}
else if (wordsHash.get(currentWord) == mostPopularCount)
{
tie = true;
}
}
if (tie)
return null;
else
return mostPopularWord;
}
}
You're calling wordsHash.keys() on each iteration of the loop, which gives you a fresh Enumeration<String> on each iteration - you're then calling it again inside the loop.
You want to call it once, and then iterate over the single Enumeration<String>:
Enumeration<String> iterator = wordsHash.keys();
while (iterator.hasMoreElements())
{
String currentWord = iterator.nextElement();
...
}
Note that as you're also getting the value for each element, you'd be better off iterating over the entrySet() rather than the keys().
You'd also be better off using HashMap instead of Hashtable, as then you could just use an enhanced for loop...
The problem is in line
while (wordsHash.keys().hasMoreElements())
each time through the loop, you are getting a new copy of the enumeration. You'll want to get the keyset once, and iterate over that.
It would probably be easier to use an enhanced for Loop here as well
for (Map.Entry<String,Integer> entry : wordsHash.entrySet()) {
String currentWord = entry.getKey();
Integer currentCount = entry.getValue();
//more code here
}
This should provide the behavior you want, while being simpler and easier to read.
The problem is that whenever you call wordsHash.keys(), it returns a new enumeration:
while (wordsHash.keys().hasMoreElements()) // <=== HERE
{
String currentWord = (String) wordsHash.keys().nextElement(); // <=== AND HERE
What you need to do is create a single enumeration and use it throughout the loop.
P.S. Why are you using Hashtable and not HashMap?
Every call to .keys() returns a new enumeration, with a new internal pointer for iterating:
Hashtable table = new Hashtable();
table.put("a", "a");
table.put("b", "b");
boolean b = table.keys() == table.keys();
System.out.println(b); // false
// the two calls to `.keys()` returned different instances of Enumeration
So assign your keys enumeration to a variable:
Enumeration keys = wordsHash.keys();
while (keys.hasMoreElements())
{
String currentWord = (String) keys.nextElement();
}
Change your code to:
Enumeration<String> keys = wordsHash.keys();
while (keys.hasMoreElements()) {
String currentWord = keys.nextElement();
So that a new enumeration pointing to the first key of the HashTable is not created every time that you enter the loop.
Nothing is modifying the wordsHash. That means that if wordsHash.keys().hasMoreElements() is true once, it'll continue to be true for the rest of the program. This causes an infinite loop. You either need to remove the keys as you go along or you should just use a for
you get a new Iterable ofer all keys each loop iteration: wordsHash.keys() as long as there is at least one key in it the while loop never ends.
Replace:
while (wordsHash.keys().hasMoreElements()){
String currentWord = (String) wordsHash.keys().nextElement();
by
for (String currentWord: wordsHash.keys()){
Also, unrelated to your Enumeration issue, this is probably a defect:
else if (wordsHash.get(currentWord) == mostPopularCount)
That's a reference comparison of a java.lang.Integer to another java.lang.Integer. It is not a comparison of the actual values they represent. It is working for "small" numbers because auto-boxing uses cached references, but will eventually break. You probably want:
else if (wordsHash.get(currentWord) == mostPopularCount.intValue())

Categories

Resources