Iterator with compareTo java [closed] - java

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 7 years ago.
Improve this question
As a general concept, say we have a compareTo method which returns 1, 0 or -1.
How is it possible to use an iterator to find out the maximum value in an array, using the compareTo method?

Simply:
Iterator<E> it = collection.iterator;
E max = null;
while(it.hasNext()) {
if(max == null) {
max = it.next();
} else {
e = it.next();
if(e.compareTo(max) > 0) {
max = e;
}
}
}

If you just want to find the maximum element in an array (without explicitly using the iterator and compareTo), then you can convert your array into a Collection or Stream:
Integer[] objectArray = {3,66,4,22,4};
List<Integer> objectList = Arrays.asList(objectArray);
System.out.println(Collections.max(objectList));
int[] primitiveArray = {3,66,4,22,4};
IntStream intStream = Arrays.stream(primitiveArray);
System.out.println(intStream.max().getAsInt());
Collections.max uses an iterator and compareTo under the hood (as you can see by reading its source code), but the second way doesn't (because it's using primitives).

Related

I need someone who can write Java Streams. I want to write this code as a Java stream type [closed]

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());
}

Java Remove ArrayList by Value [closed]

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.

using an iterator to search for a specific value in a HashMap [closed]

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
}

Removing item from generic list java? [closed]

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 8 years ago.
Improve this question
I need to remove an item from a generic list in java, but I don't know how to do this. If it was a list of int, I would just set it to zero, if it was strings I would set it to null. How can I do this with a generic list, and I can't use an methods of Arraylist or anything like that, I have to write the method myself.
You can remove an individual object instance with List.remove(Object) or you can remove a specific instance from a specific index with List.remove(int). You can also call Iterator.remove() while you iterate the List. So, for example, to remove every item from a List you could do
Iterator<?> iter = list.iterator();
while (iter.hasNext()) {
iter.remove();
}
I would think that if you are implementing a list yourself you should move all elements after the element you are deleting down one position, set the last one to null, and if you are keeping track of the size of your list, reduce this by one. Something like this
public Object remove(int remove_index){
Object temp = list[remove_index];
for(int i=remove_index;i<size-1;i++){
list[i] = list[i+1];
}
list[--size] = null;
return temp;
}
static <T> List<T> remove(List<? extends T> inputList, int removeIndex)
{
List<T> result = new ArrayList<T>( inputList.size() - 1 );
for (int i = 0 ; i < inputList.size() ; i++)
{
if ( i != removeIndex )
{
result.add( inputList.get(i) );
}
}
return result;
}

how to compare the key and the value of a hashmap [closed]

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);

Categories

Resources