help with java hash map - java

can some one please explain what is happening in the code below and how it ends up with 36?
thanks
edit by Amir Rachum
public class HashMap2009 {
public static void main (String[] args) {
Map<String, Integer> myMap2009 =
new HashMap<String, Integer>();
myMap2009.put("one", new Integer(1));
myMap2009.put("three", new Integer(3));
myMap2009.put("five", new Integer(5));
myMap2009.put("seven", new Integer(7));
myMap2009.put("nine", new Integer(9));
System.out.println(oddOne(myMap2009));
}
private static int oddOne(Map<String, Integer> myMap2009) {
if (myMap2009.isEmpty())
return 11;
else {
Set<String> st = myMap2009.keySet();
String key = st.iterator().next();
int num = myMap2009.get(key);
myMap2009.remove(key);
return num + oddOne(myMap2009);
}
}
}

This is a simple example of recursion, which results in adding up all the keys in the map one by one and when the map is empty, it adds another 11. This sums up to 36.

That's a recursive function, that each time it is called, add the value of the first element in the map and then remove it.
If the map is empty it return 11
So: 9+7+5+3+1+11 = 36 ( 9,7,5,3,1 for each value in the map and 11 for when it is empty )
BTW, this looks to me as a terrible way to teach recursion ( because the map creates too much noise )
A simpler ( and I think more effective ) way would've been:
import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
public class ArrayList2009 {
public static void main( String [] args ) {
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(3);
list.add(5);
list.add(7);
list.add(9);
System.out.println( addOne( list ) );
}
private static int addOne( List<Integer> list ){
if ( list.isEmpty() ) {
return 11;
} else {
Iterator<Integer> i = list.iterator();
int num = i.next();
i.remove();
return num + addOne( list );
}
}
}
Which does exactly the same, but introduce less noise because the List interface easier to understand.

When calling oddOne it will get
the first number
remove the number
add it to the result of oddOne (with the number removed)
this repeats till empty whne oddOne return 11
so we end up with
1 + (3 + (5 + (7 + (9 + 11)))) = 36
actually the order will e all jumbled up as it is a hashmap but this has no influence on adding numbers

You're making recursive calls, removing one element from the map per call.
You could start out with num == 1 (a map is unordered) and you remove this from your map. Then you do the recursive call, which gives you num == 3. This continues until your map is empty, which results in 1 + 3 + 5 + 7 + 9, and an additional 11 for your empty map.
Take a look at recursion: http://en.wikipedia.org/wiki/Recursion

Related

Split a List of Strings based on value in Java

What I want to do is to split an array of strings, when the first 6 characters in the string are zeroes ("000000") or when all the digits in the string are zeroes. Limiting to 6 characters won't be very dynamic.
I got this code, and it does what I want to achieve.
import java.util.*;
public class Main
{
public static void main(String[] args) {
ArrayList<String> unsplitted = new ArrayList<String>();
unsplitted.add("000000: this_should_go_into_first_array");
unsplitted.add("000234: something1");
unsplitted.add("0000ff: something2");
unsplitted.add("000111: something3");
unsplitted.add("000051: something4");
unsplitted.add("007543: something5");
unsplitted.add("000000: and_this_should_go_into_second_array");
unsplitted.add("005612: something7");
unsplitted.add("005712: something8");
System.out.println("Unsplitted list: "+ unsplitted);
List<String> arrlist1 = unsplitted.subList(0, 6);
List<String> arrlist2 = unsplitted.subList(6, unsplitted.size());
System.out.println("Sublist of arrlist1: "+ arrlist1);
System.out.println("Sublist of arrlist2: "+ arrlist2);
}
}
Which prints out the wanted results
Sublist of arrlist1: [000000: this_should_go_into_first_array, 000234: something1, 0000ff: something2, 000111: something3, 000051: something4, 007543: somethi
ng5]
Sublist of arrlist2: [000000: and_this_should_go_into_second_array, 005612: something7, 005712: something8]
However, I don't know the indexes for the zeroes beforehand, so how can I achieve the same result by finding the zeroes dynamically?
You can simply iterate in your array and create "bucket" each time you detect your 000000 string :
ArrayList<String> unsplitted = new ArrayList<String>();
unsplitted.add("000000: this_should_go_into_first_array");
unsplitted.add("000234: something1");
unsplitted.add("0000ff: something2");
unsplitted.add("000111: something3");
unsplitted.add("000051: something4");
unsplitted.add("007543: something5");
unsplitted.add("000000: and_this_should_go_into_second_array");
unsplitted.add("005612: something7");
unsplitted.add("005712: something8");
List<List<String>> results = new ArrayList<>();
unsplitted.forEach(w -> {
if(w.startsWith("000000") || results.isEmpty()) {
// no bucket or detect 000000
List<String> bucket = new ArrayList<>();
bucket.add(w);
results.add(bucket);
}
else {
// not contains 00000 put the value in the last bucket
results.get(results.size() - 1).add(w);
}
});
results.forEach(w -> {
System.out.println("Sublist " + w);
});
Is it the result that you expected ?
The result :
Sublist [000000: this_should_go_into_first_array, 000234: something1, 0000ff: something2, 000111: something3, 000051: something4, 007543: something5]
Sublist [000000: and_this_should_go_into_second_array, 005612: something7, 005712: something8]
The question is quite interesting. There are different way to implement this, but I am going to show you a solution where it can be applied with any length of the first part, which we can consider as a key.
As you said in your introduction, it wouldn't be dynamic if the check was limited to only 6 characters. Based on this, as an example, you can take the position of the character ':' as reference and apply a partitioning among the elements of the array.
Here is the solution I propose:
import java.util.*;
import java.util.function.*;
import java.util.stream.*;
public class Main
{
public static void main(String[] args) {
ArrayList<String> unsplitted = new ArrayList<String>();
unsplitted.add("000000: this_should_go_into_first_array");
unsplitted.add("000234: something1");
unsplitted.add("0000ff: something2");
unsplitted.add("000111: something3");
unsplitted.add("000051: something4");
unsplitted.add("007543: something5");
unsplitted.add("000000: and_this_should_go_into_second_array");
unsplitted.add("005612: something7");
unsplitted.add("005712: something8");
System.out.println("Non-split list: "+ unsplitted);
Predicate<String> filter = (String s) -> {
int indexOfCol = s.indexOf(":");
return s.substring(0, indexOfCol).equals("0".repeat(indexOfCol));
};
Map<Boolean, List<String>> splitMap = unsplitted.stream()
.collect(Collectors.partitioningBy(filter));
List<String> arrayZeroStart = splitMap.get(true);
List<String> arrayNonZeroStart = splitMap.get(false);
System.out.println("Sublist of arrayZeroStart: "+ arrayZeroStart);
System.out.println("Sublist of arrayWithout: "+ arrayNonZeroStart);
}
}
And here is the output:
Non-split list: [000000: this_should_go_into_first_array, 000234: something1, 0000ff:
something2, 000111: something3, 000051: something4, 007543: something5, 000000: and_this_should_go_into_second_array, 005612: something7, 005712: something8]
Sublist of arrayZeroStart: [000000: this_should_go_into_first_array, 000000: and_this_should_go_into_second_array]
Sublist of arrayWithout: [000234: something1, 0000ff: something2, 000111: something3, 000051: something4, 007543: something5, 005612: something7, 005712: something8]

how to find the duplicates in ArrayList using hashmap in java?

my program is reading large txt files(in MBs) which contain the source ip and destination ip(for example 192.168.125.10,112.25.2.1) ,,,Here read is an ArrayList in which the data is present.
i have generated unique ids(uid int type) using srcip and destip and now i am storing in
static ArrayList<Integer[]> prev = new ArrayList<Integer[]>();
where Array is
:-
static Integer[] multi1;
multi1 = new Integer[]{(int)uid,count,flag};
i have to print the all uids with there count or their frequencies using hashmap.
Plz give some solution...
for (ArrayList<String> read : readFiles.values())
{
if(file_count<=2)
{
for(int i=0 ; i<read.size() ; i++)
{
String str1=read.get(i).split(",")[0];//get only srcIP
String str2=read.get(i).split(",")[1];//get only destIP
StringTokenizer tokenizer1=new StringTokenizer(str1,".");
StringTokenizer tokenizer2=new StringTokenizer(str2,".");
if(tokenizer1.hasMoreTokens()&&tokenizer2.hasMoreTokens())
{
sip_oct1=Integer.parseInt(tokenizer1.nextToken());
sip_oct2=Integer.parseInt(tokenizer1.nextToken());
sip_oct3=Integer.parseInt(tokenizer1.nextToken());
sip_oct4=Integer.parseInt(tokenizer1.nextToken());
dip_oct1=Integer.parseInt(tokenizer2.nextToken());
dip_oct2=Integer.parseInt(tokenizer2.nextToken());
dip_oct3=Integer.parseInt(tokenizer2.nextToken());
dip_oct4=Integer.parseInt(tokenizer2.nextToken());
uid=uniqueIdGenerator(sip_oct1,sip_oct2,sip_oct3,sip_oct4,dip_oct1,dip_oct2,dip_oct3,dip_oct4);
}
multi1 = new Integer[]{(int)uid,count,flag};
prev.add(multi1);
System.out.println(prev.get(i)[0]);//getting uids from prev
Map<ArrayList<Integer []> , Integer> map = new HashMap<ArrayList<Integer[]>, Integer>();
for (int j=0 ; j<prev.size() ; j++)
{
Integer temp=map.get(prev.get(i)[0]);
count = map.get(temp);
map.put(temp, (count == null) ? 1 : count++);
}
printMap(map);
System.out.println("uids--->"+prev.get(i)[0]+" Count--- >"+count+" flag--->"+prev.get(i)[2]);
}
}
file_count++;
}
}
public static void printMap(Map<ArrayList<Integer[]>, Integer> map)
{
for (Entry<ArrayList<Integer[]>, Integer> entry : map.entrySet())
{
System.out.println(" Value : "+ entry.getValue()+"key : "+entry.getKey());
}
}
public static double uniqueIdGenerator(int oc1,int oc2,int oc3,int oc4,int oc5,int oc6,int oc7,int oc8)
{
int a,b;
double c;
a=((oc1*10+oc2)*10+oc3)*10+oc4;
b=((oc5*10+oc6)*10+oc7)*10+oc8;
c= Math.log(a)+Math.log(b);
return Math.round(c*1000);
}
Now understanding what you want, there are (at least) 2 ways of doing this.
1st: Make a list with the uid's. Then a second list where you can have a value (your uid) and keep a count. Was thinking of HashMap, but there you can not easily change the count. Maybe an ArrayList of a list with 2 values.
Then loop over your list with the uid's, check with a second for loop if the uid is already in the second list. If it is, add one to the count. If it is not, add it to the list.
2nd: Do the same thing, but then with classes (very Java). Then you can put even more info into the class ;)
Hope this helps!
*edit: #RC. indeed gives cleaner code.

Ways to iterate over a list in Java

Being somewhat new to the Java language I'm trying to familiarize myself with all the ways (or at least the non-pathological ones) that one might iterate through a list (or perhaps other collections) and the advantages or disadvantages of each.
Given a List<E> list object, I know of the following ways to loop through all elements:
Basic for loop (of course, there're equivalent while / do while loops as well)
// Not recommended (see below)!
for (int i = 0; i < list.size(); i++) {
E element = list.get(i);
// 1 - can call methods of element
// 2 - can use 'i' to make index-based calls to methods of list
// ...
}
Note: As #amarseillan pointed out, this form is a poor choice
for iterating over Lists, because the actual implementation of
the get method may not be as efficient as when using an Iterator.
For example, LinkedList implementations must traverse all of
the elements preceding i to get the i-th element.
In the above example there's no way for the List implementation to
"save its place" to make future iterations more efficient.
For an ArrayList it doesn't really matter, because the complexity/cost of get is constant time (O(1)) whereas for a LinkedList is it proportional to the size of the list (O(n)).
For more information about the computational complexity of the built-in Collections implementations, check out this question.
Enhanced for loop (nicely explained in this question)
for (E element : list) {
// 1 - can call methods of element
// ...
}
Iterator
for (Iterator<E> iter = list.iterator(); iter.hasNext(); ) {
E element = iter.next();
// 1 - can call methods of element
// 2 - can use iter.remove() to remove the current element from the list
// ...
}
ListIterator
for (ListIterator<E> iter = list.listIterator(); iter.hasNext(); ) {
E element = iter.next();
// 1 - can call methods of element
// 2 - can use iter.remove() to remove the current element from the list
// 3 - can use iter.add(...) to insert a new element into the list
// between element and iter->next()
// 4 - can use iter.set(...) to replace the current element
// ...
}
Functional Java
list.stream().map(e -> e + 1); // Can apply a transformation function for e
Iterable.forEach, Stream.forEach, ...
(A map method from Java 8's Stream API (see #i_am_zero's answer).)
In Java 8 collection classes that implement Iterable (for example, all Lists) now have a forEach method, which can be used instead of the for loop statement demonstrated above. (Here is another question that provides a good comparison.)
Arrays.asList(1,2,3,4).forEach(System.out::println);
// 1 - can call methods of an element
// 2 - would need reference to containing object to remove an item
// (TODO: someone please confirm / deny this)
// 3 - functionally separates iteration from the action
// being performed with each item.
Arrays.asList(1,2,3,4).stream().forEach(System.out::println);
// Same capabilities as above plus potentially greater
// utilization of parallelism
// (caution: consequently, order of execution is not guaranteed,
// see [Stream.forEachOrdered][stream-foreach-ordered] for more
// information about this).
What other ways are there, if any?
(BTW, my interest does not stem at all from a desire to optimize performance; I just want to know what forms are available to me as a developer.)
The three forms of looping are nearly identical. The enhanced for loop:
for (E element : list) {
. . .
}
is, according to the Java Language Specification, identical in effect to the explicit use of an iterator with a traditional for loop. In the third case, you can only modify the list contents by removing the current element and, then, only if you do it through the remove method of the iterator itself. With index-based iteration, you are free to modify the list in any way. However, adding or removing elements that come before the current index risks having your loop skipping elements or processing the same element multiple times; you need to adjust the loop index properly when you make such changes.
In all cases, element is a reference to the actual list element. None of the iteration methods makes a copy of anything in the list. Changes to the internal state of element will always be seen in the internal state of the corresponding element on the list.
Essentially, there are only two ways to iterate over a list: by using an index or by using an iterator. The enhanced for loop is just a syntactic shortcut introduced in Java 5 to avoid the tedium of explicitly defining an iterator. For both styles, you can come up with essentially trivial variations using for, while or do while blocks, but they all boil down to the same thing (or, rather, two things).
EDIT: As #iX3 points out in a comment, you can use a ListIterator to set the current element of a list as you are iterating. You would need to use List#listIterator() instead of List#iterator() to initialize the loop variable (which, obviously, would have to be declared a ListIterator rather than an Iterator).
Example of each kind listed in the question:
ListIterationExample.java
import java.util.*;
public class ListIterationExample {
public static void main(String []args){
List<Integer> numbers = new ArrayList<Integer>();
// populates list with initial values
for (Integer i : Arrays.asList(0,1,2,3,4,5,6,7))
numbers.add(i);
printList(numbers); // 0,1,2,3,4,5,6,7
// replaces each element with twice its value
for (int index=0; index < numbers.size(); index++) {
numbers.set(index, numbers.get(index)*2);
}
printList(numbers); // 0,2,4,6,8,10,12,14
// does nothing because list is not being changed
for (Integer number : numbers) {
number++; // number = new Integer(number+1);
}
printList(numbers); // 0,2,4,6,8,10,12,14
// same as above -- just different syntax
for (Iterator<Integer> iter = numbers.iterator(); iter.hasNext(); ) {
Integer number = iter.next();
number++;
}
printList(numbers); // 0,2,4,6,8,10,12,14
// ListIterator<?> provides an "add" method to insert elements
// between the current element and the cursor
for (ListIterator<Integer> iter = numbers.listIterator(); iter.hasNext(); ) {
Integer number = iter.next();
iter.add(number+1); // insert a number right before this
}
printList(numbers); // 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15
// Iterator<?> provides a "remove" method to delete elements
// between the current element and the cursor
for (Iterator<Integer> iter = numbers.iterator(); iter.hasNext(); ) {
Integer number = iter.next();
if (number % 2 == 0) // if number is even
iter.remove(); // remove it from the collection
}
printList(numbers); // 1,3,5,7,9,11,13,15
// ListIterator<?> provides a "set" method to replace elements
for (ListIterator<Integer> iter = numbers.listIterator(); iter.hasNext(); ) {
Integer number = iter.next();
iter.set(number/2); // divide each element by 2
}
printList(numbers); // 0,1,2,3,4,5,6,7
}
public static void printList(List<Integer> numbers) {
StringBuilder sb = new StringBuilder();
for (Integer number : numbers) {
sb.append(number);
sb.append(",");
}
sb.deleteCharAt(sb.length()-1); // remove trailing comma
System.out.println(sb.toString());
}
}
The basic loop is not recommended as you do not know the implementation of the list.
If that was a LinkedList, each call to
list.get(i)
would be iterating over the list, resulting in N^2 time complexity.
A JDK8-style iteration:
public class IterationDemo {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 2, 3);
list.stream().forEach(elem -> System.out.println("element " + elem));
}
}
In Java 8 we have multiple ways to iterate over collection classes.
Using Iterable forEach
The collections that implement Iterable (for example all lists) now have forEach method. We can use method-reference introduced in Java 8.
Arrays.asList(1,2,3,4).forEach(System.out::println);
Using Streams forEach and forEachOrdered
We can also iterate over a list using Stream as:
Arrays.asList(1,2,3,4).stream().forEach(System.out::println);
Arrays.asList(1,2,3,4).stream().forEachOrdered(System.out::println);
We should prefer forEachOrdered over forEach because the behaviour of forEach is explicitly nondeterministic where as the forEachOrdered performs an action for each element of this stream, in the encounter order of the stream if the stream has a defined encounter order. So forEach does not guarantee that the order would be kept.
The advantage with streams is that we can also make use of parallel streams wherever appropriate. If the objective is only to print the items irrespective of the order then we can use parallel stream as:
Arrays.asList(1,2,3,4).parallelStream().forEach(System.out::println);
I don't know what you consider pathological, but let me provide some alternatives you could have not seen before:
List<E> sl= list ;
while( ! sl.empty() ) {
E element= sl.get(0) ;
.....
sl= sl.subList(1,sl.size());
}
Or its recursive version:
void visit(List<E> list) {
if( list.isEmpty() ) return;
E element= list.get(0) ;
....
visit(list.subList(1,list.size()));
}
Also, a recursive version of the classical for(int i=0... :
void visit(List<E> list,int pos) {
if( pos >= list.size() ) return;
E element= list.get(pos) ;
....
visit(list,pos+1);
}
I mention them because you are "somewhat new to Java" and this could be interesting.
You can use forEach starting from Java 8:
List<String> nameList = new ArrayList<>(
Arrays.asList("USA", "USSR", "UK"));
nameList.forEach((v) -> System.out.println(v));
In java 8 you can use List.forEach() method with lambda expression to iterate over a list.
import java.util.ArrayList;
import java.util.List;
public class TestA {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("Apple");
list.add("Orange");
list.add("Banana");
list.forEach(
(name) -> {
System.out.println(name);
}
);
}
}
In Java 8 or above, you can iterate a Hashset using forEach() method.
import java.util.HashSet;
public class HashSetTest {
public static void main(String[] args) {
HashSet<String> hSet = new HashSet<String>();
// Adding elements into your HashSet usind add()
hSet.add("test1");
hSet.add("test2");
hSet.add("test3");
// Iterating over hash set items
hSet.forEach(x -> System.out.println(x));
// Or you can write shorter:
hSet.forEach(System.out::println);
}
}
For a backward search you should use the following:
for (ListIterator<SomeClass> iterator = list.listIterator(list.size()); iterator.hasPrevious();) {
SomeClass item = iterator.previous();
...
item.remove(); // For instance.
}
If you want to know a position, use iterator.previousIndex(). It also helps to write an inner loop that compares two positions in the list (iterators are not equal).
Right, many alternatives are listed. The easiest and cleanest would be just using the enhanced for statement as below. The Expression is of some type that is iterable.
for ( FormalParameter : Expression ) Statement
For example, to iterate through, List<String> ids, we can simply so,
for (String str : ids) {
// Do something
}
Above you'll find all differents ways to iterate over a LIST.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class test1 {
public static void main(String[] args) {
//******* Exercise 1 : Write a Java program to create a new array list, add some colors (string) and print out the collection.
List<String> colors = new ArrayList<String>();
colors.add("Black");
colors.add("Red");
colors.add("Green");
colors.add("Blue");
System.out.println(colors);
//******* Exercise 2 : Write a Java program to iterate through all elements in a array list.
System.out.println("//******* Exercise 2");
List<Integer> list2 = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
// iteration type 1 : using FOR loop
System.out.println("// iteration type 1");
for(Integer nb : list2) {
System.out.print(nb + ", ");
}
System.out.println("\n");
// iteration type 2 : using FOR loop
System.out.println("// iteration type 2");
for(int i=0; i < list2.size(); i++) {
System.out.print(list2.get(i) + ", ");
}System.out.println("\n");
// iteration type 3 : using Do-While loop
System.out.println("// iteration type 3");
int index21 = 0;
do {
System.out.print(list2.get(index21) + ", ");
index21++;
}while(index21<list2.size());
System.out.println("\n");
// iteration type 4 : using While loop
System.out.println("// iteration type 4");
int index22 = 0;
while(index22<list2.size()) {
System.out.print(list2.get(index22) + ", ");
index22++;
}
System.out.println("\n");
// iteration type 5 : using Iterable forEach loop
System.out.println("// iteration type 5");
list2.forEach(elt -> {
System.out.print(elt + ", ");
});
System.out.println("\n");
// iteration type 6 : using Iterator
System.out.println("// iteration type 6");
Iterator<Integer> listIterator = list2.iterator();
while(listIterator.hasNext()) {
System.out.print( listIterator.next() + ", ");
}
System.out.println("\n");
// iteration type 7 : using Iterator (From the beginning)
System.out.println("// iteration type 7");
ListIterator<Integer> listIterator21 = list2.listIterator(list2.size());
while(listIterator21.hasPrevious()) {
System.out.print( listIterator21.previous() + ", ");
}
System.out.println("\n");
// iteration type 8 : using Iterator (From the End)
System.out.println("// iteration type 8");
ListIterator<Integer> listIterator22 = list2.listIterator();
while(listIterator22.hasNext()) {
System.out.print( listIterator22.next() + ", ");
}
System.out.println("\n");
}
}
You could always switch out the first and third examples with a while loop and a little more code. This gives you the advantage of being able to use the do-while:
int i = 0;
do{
E element = list.get(i);
i++;
}
while (i < list.size());
Of course, this kind of thing might cause a NullPointerException if the list.size() returns 0, becuase it always gets executed at least once. This can be fixed by testing if element is null before using its attributes / methods tho. Still, it's a lot simpler and easier to use the for loop

How to avoid Java.util.IllegalStateException in the following code?

I have a List of integer's with duplicate values in it. What I need to do is find the duplicate integers, add their value and then add the result to the list by removing the duplicates found. Here is what I am doing:
List<Integer> list1 = new ArrayList<Integer>();
list1.add(2);
list1.add(5);
list1.add(3);
list1.add(5);
list1.add(4);
List<Integer> list2 = new ArrayList<Integer>();
Iterator<Integer> it = list1.iterator();
while (it.hasNext()) {
Integer int1 = it.next();
if (list2.isEmpty()) {
list2.add(int1);
it.remove();
} else {
ListIterator<Integer> it2 = list2.listIterator();
while (it2.hasNext()) {
Integer int2 = it2.next();
if (int2 != int1) {
it2.add(int1);
it.remove();// I get exception here
} else {
it2.remove();
it.remove();
Integer newint = int1 + int2;
it2.add(newint);
}
}
}
}
for(Integer in : list2){
System.out.println(in);
}
Output should look like
2
10
3
4
Thanks for your time.
As the other posters have said, you can't remove while iterating. Even though there are 'tricks', messing with a collection while iterating is a surefire way to get weird runtime bugs.
Anyway, you are working way too hard on the problem.
Here's a quick and dirty solution with a fraction of the code:
private List<Integer> sumAndUniqDuplicates(List<Integer> list) {
LinkedHashMap<Integer, Integer> lookup = new LinkedHashMap<Integer, Integer>();
for (Integer value : list) {
Integer prevValue = lookup.get(value);
prevValue = (prevValue == null) ? 0 : prevValue;
lookup.put(value, prevValue + value);
}
return new ArrayList<Integer>(lookup.values());
}
If you are allowed to use a Map you could do something simple like this (incoming pseudocode):
create empty Map m
for each Integer x in list1 do
if m does not contain key x
m.put(x, x)
else
m.put(x, m.get(x) + x)
endif
done
Your result are the values of m (which is a Collection).
Edit: You said you have LatLng instead of Integers - I don't know LatLng but after a quick google I'd take a shot at the following, assuming that you want to "add" up your LatLng points:
create empty Map<LatLng, LatLng> m
for each LatLng x in list1 do
if not m.containsKey(x)
m.put(x, x)
else
m.put(x, LatLng.newInstance(m.get(x).getLatitude() + x.getLatitude(),
m.get(x).getLongitude() + x.getLongitude()))
endif
done
The only problem I can see here is that this m.containsKey(x) depends on the correct implementation of equals, which I'm not sure after reading this
It is because you remove the same element twice. First time in if(list2.isEmpty()) (because list2 is empty in the beginning and immediately after that in the else body.
From the documentation for the remove method:
Removes from the underlying collection the last element returned by the iterator (optional operation). This method can be called only once per call to next.
You cannot remove the current element twice. You need to rethink your logic.

Is there an object like a "Set" that can contain only unique string values, but also contain a count on the number of occurrences of the string value?

In Java is there an object like a "Set" that can contain only unique string values, but also contain a count on the number of occurrences of the string value?
The idea is simple
With a data set ala...
A
B
B
C
C
C
I'd like to add each line of text to a Set-like object. Each time that a non-unique text is added to the set I'd like to also have a numeric value associated with the set to display how many times it was added. So if I ran it on the above data set the output would be something like:
A : 1
B : 2
C : 3
any ideas?
You want a "Bag", like the Bag in Apache Commons Collections or the Multiset in Google Collections. You can add the same value to it multiple times, and it'll record the counts of each value. You can then interrogate the counts.
You'd do something like this with Apache Commons' Bag:
Bag myBag = new HashBag();
myBag.add("Orange");
myBag.add("Apple", 4);
myBag.add("Apple");
myBag.remove("Apple", 2);
int apples = myBag.getCount("Apple"); // Should be 3.
int kumquats = myBag.getCount("Kumquat"); // Should be 0.
And this with Google Collections' Multiset.
Multiset<String> myMultiset= HashMultiset.create();
myMultiset.add("Orange");
myMultiset.add("Apple", 4);
myMultiset.add("Apple");
myMultiset.remove("Apple", 2);
int apples = myMultiset.count("Apple"); // 3
int kumquats = myMultiset.count("Kumquats"); // 0
The problem with Apache Collections in general is that it isn't being very actively maintained, and it doesn't yet support Java Generics. To step into this gap, Google's written their own Collections which are extremely powerful. Be sure to evaluate Google Collections first.
Update: Google Collections also offers Multimap, a "collection similar to a Map, but which may associate multiple values with a single key".
Map<String, Integer> would be the best bet, to put in words what you want to do is to Map the amount of occurrences of a string. Basically have something like this:
public void add(String s) {
if (map.containsKey(s)) {
map.put(s, map.get(s) + 1);
} else {
map.put(s, 1);
}
}
Yeap, not directly in the core, but can be built easily with a Map.
Here's a naive implementation:
import java.util.Map;
import java.util.HashMap;
public class SetLike {
private Map<String, Integer> map = new HashMap<String,Integer>();
public void add( String s ) {
if( !map.containsKey( s ) ){
map.put( s, 0 );
}
map.put( s, map.get( s ) + 1 );
}
public void printValuesAndCounts() {
System.out.println( map );
}
public static void main( String [] args ){
String [] data = {"A","B","B","C","C","C"};
SetLike holder = new SetLike();
for( String value : data ) {
holder.add( value );
}
holder.printValuesAndCounts();
}
}
Test it
$ javac SetLike.java
$ java SetLike
{A=1, C=3, B=2}
Of course you can improve it much more. You can implement the Set interface, or a List, or a Collection, etc, you can add the iterators, implement Iterable and so on, it depends on what you want and what you need.
This will be helpful..
List<String> myList=new ArrayList<String>();
myList.add("A");
myList.add("B");
myList.add("B");
myList.add("C");
myList.add("C");
myList.add("C");
Set<String> set=new HashSet<String>(myList);
for (String value : set)
{
int occurance=Collections.frequency(myList, value);
System.out.println(value +" occur "+occurance + " times ");
}
Result :
A occur 1 times
B occur 2 times
C occur 3 times

Categories