Studyon Minte9.com
Java SE




Associative (Map)



Java

package testexample; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; import java.util.Properties; import java.util.TreeMap; public class Test { public static void main(String[] args) { // --- Array (Simple) --- // String[] sData = new String[]{"2", "3"}; for (int i=0; i<sData.length; i++) { System.out.println(sData[i]); // Output: 2, 3 } System.out.println(sData[1]); // Output: 3 System.out.println(); // --- Associative (Properties) --- // Properties pData = new Properties(){{ put("1", "aa"); put("2", "cc"); put("3", "bb"); }}; for(Object i : pData.keySet()) { System.out.println(pData.getProperty((String) i)); } // Outputs: bb, cc, aa // --- Unordered --- // System.out.println(); // --- HashMap (Unordered) --- // Map<String, String> hmData = new HashMap<String, String>(){{ put("a", "4"); put("b", "5"); }}; for (Object i : hmData.keySet()) { System.out.println(hmData.get(i));// Output: 5, 4 // --- Unordered --- // } System.out.println(hmData.get("b")); // Output: 5 System.out.println(); // --- LinkedHashMap (Ordered) --- // Map<String, String> lmData = new LinkedHashMap<String, String>(){{ put("a", "4"); put("c", "7"); put("b", "3"); }}; for (Object i : lmData.keySet()) { System.out.println(lmData.get(i));// Output: 4, 7, 3 // --- Ordered --- // } System.out.println(lmData.get("b")); // Output: 3 System.out.println(); // --- TreeMap (Ordered) --- // Map<String, String> tmData = new TreeMap<String, String>(){{ put("a", "4"); put("c", "7"); put("b", "3"); }}; for (Object i : tmData.keySet()) { System.out.println(tmData.get(i));// Output: 4, 3, 7 // --- Sorted --- // } System.out.println(tmData.get("b")); // Output: 3 System.out.println(); // --- TreeMap (Multidimensional) --- // Map<Integer, Properties> mData = new TreeMap<Integer, Properties>(){{ put(0, new Properties(){{ put("x", "10"); put("y", "15"); }}); put(1, new Properties(){{ put("x", "20"); put("y", "25"); }}); }}; for (Object i : mData.keySet()) { Properties p = mData.get((Integer) i); for (Object j : p.keySet()) { System.out.println(p.getProperty((String) j)); // Output: 10, 15 / 20, 25 } System.out.println(p.getProperty("y")); // Output: 15 / 25 System.out.println(); } System.out.println(mData.get(1).getProperty("x")); // Output: 20 System.out.println(); // --- Treemap (Integers) --- // Map<Integer, Integer> iData = new LinkedHashMap<Integer, Integer>(){{ put(1, 5); put(4, 15); put(3, 10); }}; for(Object i : iData.keySet()) { System.out.println(iData.get(i)); // Output 5, 15, 10 } System.out.println(iData.get(2)); // Output: null // --- key not exists --- // System.out.println(); } }
  Php
<?php $data = array( // Associative array "x" => "a", "y" => "b", ); foreach($data as $k=>$v) { echo $k . $v . " "; } // Outputs: xa yb ?>
  Javascript
<script type='text/javascript'> var data = {"x":"a", "y":"b"}; // Associative array (Json) for(var i in data) { document.write(i + data[i] + " "); } // Output: xa yb </script>
  jQuery
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js "></script> <script type='text/javascript'> var data = {x:"a", y:"b"}; // Json key could be used without quotes $.each(data, function(key, value){ console.log(key + value); }); // Console: xa yb </script>