Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm working on a game and I'm implementing objects where you can define in "TiledMap editor" what items a certain object holds.
So I've got to an idea where I can enter the Item ID's right there like {22:4, id:amount}. When I parse the map, I retrieve that array as a string, is there a way to convert it to an array?
Thanks in advance!
Firstly, you probably want a Map, not an array or a List.
Map<String,String> processParams(String list) {
Map<String,String> = new HashMap<String,String>();
int openBracket = list.indexOf("{");
int closeBracket = list.lastIndexOf("}");
String params = list.substring(openBracket+1,closeBracket);
String paramList = params.split(",");
for(String param: paramList) {
String pData = param.trim().split(":");
map.put(param[0].trim(),param[1].trim());
}
return map;
}
processParams("{22:4, id:amount}");
Of course, it's actually a JSON-like structure so there's probably pre-existing parsers.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
public List testList(List numberList) {
List realList = new ArrayList();
for (Iterator iterator = numberList.iterator(); iterator.hasNext();) {
String num = (String) iterator.next();
realList.add(call(num));
}
return realList;
}
I want to write this code as a Java stream type.
I think you nead that:
numberList.stream()
.map(arg-> arg.toString())
.map(arg -> call(arg))
.collect(Collectors.toList());
public List testList(List numberList) {
return numberList.stream()
.map(s -> call((String) s))
.collect(Collectors.toList());
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Suppose there are 2 classes:
class Subject {
private int subjectId;
private String Name;
}
class Course {
private int courseId;
private List<Subject> subjects;
}
I want to convert a Course object into the Map<subjectId, courseId>. How can I achieve this using Java 8 streams?
Converting a single Course object to the requested Map:
Map<Integer,Integer> map =
course.getSubjects()
.stream()
.collect(Collectors.toMap(Subject::getId, s -> course.getId()));
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have an ArrayList like this:
[{1=R111, 2=Red, 3=50000}, {1=R123, 2=Blue , 3=50000}]
and i want to remove the array by value (R111 or R123).
how to remove the array using array.remove method for array like that?
I've try this link
but it's doesn't work for my problem.
Assuming your ArrayList is this:
List<String[]> arrayList = new ArrayList<>();
arrayList.add(new String[]{"R111","Red","50000"});
arrayList.add(new String[]{"R123","Blue","50000"});
you can do something like:
for (Iterator<String[]> iterator = arrayList.iterator();iterator.hasNext();) {
String[] stringArray = iterator.next();
if("R111".equals(stringArray[0])) {
iterator.remove();
}
}
You can safely remove an element using iterator.remove() while iterating the ArrayList. Also see The collection Interface.
An alternative shorter approach using Streams would be:
Optional<String[]> array = arrayList.stream().filter(a -> "R111".equals(a[0])).findFirst();
array.ifPresent(strings -> arrayList.remove(strings));
Thanks pieter, I used Iterator like this:
for (Iterator<HashMap<String, String>> iterator = RegulerMenu.iterator(); iterator.hasNext();) {
HashMap<String, String> stringArray = iterator.next();
if("R111".equals(stringArray.get("1"))) {
iterator.remove();
}
}
It's work now, Thankyou verymuch.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am trying to search for a specific value in a HashMap, using an iterator, currently I have this method. I am very new to Java so your help would be greatly appreciated. helper.readAMap is hashmap which stores responses, which are generated when a user types in a certain word.
public String generateResponse(String words)
{
HashMap<String, String> map = new HashMap();
map = helper.readAMap("replies.txt");
Iterator<String> it = map.keySet();
while(it.hasNext()) {
String word = it.next();
String response = map.get(word);
if(response != null) {
return response;
}
}
return pickDefaultResponse();
}
Here:
if(key.equals(words)) {
You compare a String to a HashSet of Strings. That is like comparing an apple to a pear; it will always be false.
So you either want a single String as argument to your method, or you want to generate responses to all of the words.
I expect you want to do this:
if(words.contains(key)) { // Your input contains the key
return map.get(key); // Retrieve the response to the key from the map
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to compare if the value is contained in the name in my hashmap, I want to be able to do this for all of my items in my hashmap. I have already populated my hashmap and just want to compare if if the value is contained in the name.
I currently have
Map<String, String> barcodeMap = Maps.newHashMap();
while ((nextLine = reader.readNext()) != null)
{
barcodeMap.put(nextLine[1], nextLine[0]);
}
I want to compare for example if nextLine[0] is abc and nextLine[1] is abc123, I want to compare if abc is in abc123 and if it is then make nextLine[1] abc
try this
Map map = ...
for(Entry e : map.entrySet) {
Object k = e.getKey();
Object v = e.getValue();
... compare
}
Use containsValue() method to validate the value object present in the map.
http://docs.oracle.com/javase/7/docs/api/java/util/Map.html#containsValue%28java.lang.Object%29
map.containsValue(value);