3 Replies - 938 Views - Last Post: 07 October 2012 - 06:49 AM Rate Topic: -----

#1 aquafatz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 23-September 12

Print first 20 elements of hash map

Posted 06 October 2012 - 09:51 AM

I have a hash map that stores a word(key) and its frequency of occurence(value).
I want to print the last twenty entries only.
For an array, I would have a loop that runs from the length to (n-20). But I can't figure how to do the same for a hash map.
I just know how to loop through all the entries.
for (Map.Entry<String, Integer> entry : set)


Thanks for the help!
Is This A Good Question/Topic? 0
  • +

Replies To: Print first 20 elements of hash map

#2 Ytry  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 120
  • Joined: 25-July 12

Re: Print first 20 elements of hash map

Posted 06 October 2012 - 10:08 AM

If you know how to loop through the entries, just loop through them and print the results. I am not entirely sure if this works with hashmaps, as I don't use them often, but couldn't you just print the index similar to printing the index of an array?

This post has been edited by Ytry: 06 October 2012 - 10:26 AM

Was This Post Helpful? 0
  • +
  • -

#3 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1398
  • View blog
  • Posts: 3,097
  • Joined: 05-April 11

Re: Print first 20 elements of hash map

Posted 06 October 2012 - 10:33 AM

so if I understand you correctly, you want to iterate through the last 20 elements that got put in the map?

The HashMap doesn't have this functionality by itself, so you would have to make a solution yourself

All you would need is an array with some queue like functionality
It is not the hardest thing in the world to implement :)
	public DELETE() {
		LastInserts<String> list = new LastInserts<String>(5);
		list.add("A");
		list.add("B");
		list.add("C");
		list.add("D");
		list.add("E");
		
		for (String element : list) 
			System.out.println(element);
		
		list.add("F");
		
		System.out.println();
		for (String element : list) 
			System.out.println(element);
	}
	
	private class LastInserts<T> implements Iterable<T> {
		private int current; //The index the next element should be inserted at in the array
		private Object[] elements;
		
		public LastInserts(int size) {
			elements = new Object[size];
		}
		
		public void add(T element) {
			elements[current] = element;
			current++;
			current %= elements.length;
		}
		
		@Override
		public Iterator<T> iterator() {
			return new MyIterator();
		}
		
		private class MyIterator implements Iterator<T> {
			private int index;
			
			@Override
			public boolean hasNext() {
				return index < elements.length;
			}

			@Override
			public T next() {
				return (T) elements[index++];
			}

			@Override
			public void remove() {
				throw new NotImplementedException();
			}
		}
	}


Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2720
  • View blog
  • Posts: 11,447
  • Joined: 20-September 08

Re: Print first 20 elements of hash map

Posted 07 October 2012 - 06:49 AM

Quote

I want to print the last twenty entries only.


This is what as known as a 'category error'. Your question implies some kind of connection between a Map and human-perceptible ordering, and there is none. If you could get the last twenty entries, and you can't ordinarily, then those entries would effectively appear random.

What is your GOAL?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1