How can I foreach a hashmap?

Henry Stuart

I have a hashmap with the values :

1 - 18;
1 - 19;
1 - 20;
3 - 21;
3 - 22;
8 - 23;
8 -24;
8 - 25;
11 - 26;

And I gotta loop it and check whether the key is equal the specific value, which will be a parameter. So, let's say my parameter is the number 3. It should return this to me:

21, 22.

Thanks!

Bojan Kseneman
Map<Integer, Integer> map = ...;

for (Integer key : map.keySet()) {
    // do something
}

Or

Iterator<Map.Entry<Integer, Integer>> entries = map.entrySet().iterator();
while (entries.hasNext()) {
    Map.Entry<Integer, Integer> entry = entries.next();
    System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}

EDIT: According to your comment this is what you need, just paste this somewhere and observe and play with it a bit, I tryied to comment the imporant sutff

private void hashMapTest() {
    HashMap<Integer, List<Integer>> testMap = new HashMap<>();
    int i; // Iterator

    // ArrayLists that hold the integer values at specific key
    List<Integer> list1 = new ArrayList<>();
    List<Integer> list2 = new ArrayList<>();
    List<Integer> list3 = new ArrayList<>();
    List<Integer> list4 = new ArrayList<>();

    // Fill the lists with some demo data.
    for (i = 0; i < 5; ++i) {
        list1.add(i);
    }

    for (; i < 10; ++i) {
        list2.add(i);
    }

    for (; i < 15; ++i) {
        list3.add(i);
    }

    for (; i < 20; ++i) {
        list4.add(i);
    }

    // Add the data holders to the map. Note the keys in HashMap must be unique!!
    testMap.put(1, list1);
    testMap.put(2, list2);
    testMap.put(3, list3);
    testMap.put(4, list4);

    // Let's you want to get the values with the key 3 amd print it out
    for (Integer integer : testMap.get(3)) {
        Log.d("loop test", "Integer: " + integer);
    }

    // Now let's say you want to add some value to one of the lists, say key 2
    testMap.get(2).add(12314151);
    testMap.get(2).add(1231);
    testMap.get(2).add(77565);

    // And print it
    for (Integer integer : testMap.get(2)) {
        Log.d("loop test2", "Integer: " + integer);
    }

}

Will produce something like this

05-22 21:34:35.765  20092-20092/? D/loop test﹕ Integer: 10
05-22 21:34:35.765  20092-20092/? D/loop test﹕ Integer: 11
05-22 21:34:35.765  20092-20092/? D/loop test﹕ Integer: 12
05-22 21:34:35.766  20092-20092/? D/loop test﹕ Integer: 13
05-22 21:34:35.766  20092-20092/? D/loop test﹕ Integer: 14
05-22 21:34:35.766  20092-20092/? D/loop test2﹕ Integer: 5
05-22 21:34:35.766  20092-20092/? D/loop test2﹕ Integer: 6
05-22 21:34:35.766  20092-20092/? D/loop test2﹕ Integer: 7
05-22 21:34:35.766  20092-20092/? D/loop test2﹕ Integer: 8
05-22 21:34:35.766  20092-20092/? D/loop test2﹕ Integer: 9
05-22 21:34:35.766  20092-20092/? D/loop test2﹕ Integer: 12314151
05-22 21:34:35.766  20092-20092/? D/loop test2﹕ Integer: 1231
05-22 21:34:35.766  20092-20092/? D/loop test2﹕ Integer: 77565

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How can I get a column of value from hashmap

분류에서Dev

How can I use a custom hash function in a HashSet or HashMap?

분류에서Dev

How Can I Pull An Entire Result From A Foreach Loop

분류에서Dev

How can I fix this code that uses a HashMap<i32, PriorityQueue<i32, i32>> to sort a matrix diagonally?

분류에서Dev

How can I write an if statement using a variable in a foreach loop using Powershell?

분류에서Dev

How can i fill a C# listview by using foreach loop a column and another foreach for second column so that 2nd loop do not over write first

분류에서Dev

How do I use a HashMap from another class or function?

분류에서Dev

tcsh: how can we display the body of a foreach loop in history

분류에서Dev

How can I perform an OR with ScalaTest

분류에서Dev

How can I run owncloud?

분류에서Dev

How can I organize this array

분류에서Dev

How can I rewrite this a generic?

분류에서Dev

How can I get a RejectedExecutionException

분류에서Dev

How can i manage this request?

분류에서Dev

How can I rotate a Rectangle?

분류에서Dev

How can I get a solution for this?

분류에서Dev

How can I stop the timer?

분류에서Dev

How can I repack JPEG?

분류에서Dev

How i can use a criteria in cicle for i

분류에서Dev

How can I run a desktop in the cloud that I can remotely connect to?

분류에서Dev

If I have apply(i: Int), how can I have map?

분류에서Dev

How can I install Visual Studio?

분류에서Dev

How can I benchmark my HDD?

분류에서Dev

How can I evenly space icons in the panel?

분류에서Dev

How can I "diff" two files with Nautilus?

분류에서Dev

How can I view file header information?

분류에서Dev

How can I create launcher icon for PhpStorm?

분류에서Dev

How can I join a nested Perl hash?

분류에서Dev

How can I switch between open applications?

Related 관련 기사

뜨겁다태그

보관