2011-09-14 4 views
5

java.util.Map.Entry के रूप में मैं जानता हूँ कि एक public static interfacejava.util में पैकेज कि रिटर्न मानचित्र का संग्रह दृश्य लेकिन जहां तक ​​अब मैं स्थिर इंटरफ़ेस साथ उलझन में हूँ और के रूप में यह Map.Entry है यह एक आंतरिक है इंटरफ़ेस यदि हां, तो हमारे पास जावाMap.Entry इंटरफ़ेस

लोगों को देखें कि मैं उलझन में हूं, कृपया मुझे किसी भी संभावित तरीके से मेरी सहायता करें।

+9

stackoverflow सवालों के बारे में है ... आपके सवाल का क्या है? –

उत्तर

8

Entry की परिभाषा Map की परिभाषा (जावा द्वारा अनुमति) के अंदर रहने के लिए होता है। static होने का अर्थ है कि को संदर्भित करने के लिए आपको Map के उदाहरण की आवश्यकता नहीं है।

उदाहरण के लिए Map.Entry का उपयोग करने का तरीका यह देखना सबसे आसान है। यहां बताया गया है कि आप मानचित्र पर पुन: सक्रिय कैसे हो सकते हैं

Map<Integer, String> map = new HashMap<Integer, String>(); 

for (Map.Entry<Integer, String> entry : map.entrySet()) { 
    Integer key = entry.getKey(); 
    String value = entry.getValue(); 
    // do something with key and/or value etc 
    // you may also alter the entry's value inside this loop via entry.setValue() 
} 
2

वास्तव में भ्रमित होने के लिए कुछ भी नहीं है।

हां, जावा इंटरफेस कक्षाओं या अन्य इंटरफेस के सदस्य होने की अनुमति देता है।

नहीं, इसका मतलब कुछ खास नहीं है। यह इस तरह के इंटरफ़ेस का उपयोग कैसे कर सकता है या आप इसके साथ क्या कर सकते हैं इसके बारे में बिल्कुल कुछ भी नहीं बदलता है।

यह केवल उस इंटरफ़ेस का नाम बदलता है और इसके और उसके संलग्न प्रकार के बीच एक मजबूत वैचारिक लिंक बनाता है। इस मामले में, Map.EntryMap की प्रविष्टि का प्रतिनिधित्व करता है। एपीआई के डिजाइनरों ने जाहिर तौर पर महसूस किया कि इसे एक सदस्य प्रकार बनाकर इस कनेक्शन पर दबाव डालने का अर्थ है।

0

हां, यह Map इंटरफ़ेस का आंतरिक इंटरफ़ेस है।


/** 
    * A map entry (key-value pair). The <tt>Map.entrySet</tt> method returns 
    * a collection-view of the map, whose elements are of this class. The 
    * <i>only</i> way to obtain a reference to a map entry is from the 
    * iterator of this collection-view. These <tt>Map.Entry</tt> objects are 
    * valid <i>only</i> for the duration of the iteration; more formally, 
    * the behavior of a map entry is undefined if the backing map has been 
    * modified after the entry was returned by the iterator, except through 
    * the <tt>setValue</tt> operation on the map entry. 
    * 
    * @see Map#entrySet() 
    * @since 1.2 
    */ 
    interface Entry<K,V> { 
     /** 
    * Returns the key corresponding to this entry. 
    * 
    * @return the key corresponding to this entry 
     * @throws IllegalStateException implementations may, but are not 
     *   required to, throw this exception if the entry has been 
     *   removed from the backing map. 
    */ 
    K getKey(); 

     /** 
    * Returns the value corresponding to this entry. If the mapping 
    * has been removed from the backing map (by the iterator's 
    * <tt>remove</tt> operation), the results of this call are undefined. 
    * 
    * @return the value corresponding to this entry 
     * @throws IllegalStateException implementations may, but are not 
     *   required to, throw this exception if the entry has been 
     *   removed from the backing map. 
    */ 
    V getValue(); 

     /** 
    * Replaces the value corresponding to this entry with the specified 
    * value (optional operation). (Writes through to the map.) The 
    * behavior of this call is undefined if the mapping has already been 
    * removed from the map (by the iterator's <tt>remove</tt> operation). 
    * 
     * @param value new value to be stored in this entry 
     * @return old value corresponding to the entry 
     * @throws UnsupportedOperationException if the <tt>put</tt> operation 
     *   is not supported by the backing map 
     * @throws ClassCastException if the class of the specified value 
     *   prevents it from being stored in the backing map 
     * @throws NullPointerException if the backing map does not permit 
     *   null values, and the specified value is null 
     * @throws IllegalArgumentException if some property of this value 
     *   prevents it from being stored in the backing map 
     * @throws IllegalStateException implementations may, but are not 
     *   required to, throw this exception if the entry has been 
     *   removed from the backing map. 
     */ 
    V setValue(V value); 

    /** 
    * Compares the specified object with this entry for equality. 
    * Returns <tt>true</tt> if the given object is also a map entry and 
    * the two entries represent the same mapping. More formally, two 
    * entries <tt>e1</tt> and <tt>e2</tt> represent the same mapping 
    * if<pre> 
     *  (e1.getKey()==null ? 
     *  e2.getKey()==null : e1.getKey().equals(e2.getKey())) &amp;&amp; 
     *  (e1.getValue()==null ? 
     *  e2.getValue()==null : e1.getValue().equals(e2.getValue())) 
     * </pre> 
    * This ensures that the <tt>equals</tt> method works properly across 
    * different implementations of the <tt>Map.Entry</tt> interface. 
    * 
    * @param o object to be compared for equality with this map entry 
    * @return <tt>true</tt> if the specified object is equal to this map 
    *   entry 
     */ 
    boolean equals(Object o); 

    /** 
    * Returns the hash code value for this map entry. The hash code 
    * of a map entry <tt>e</tt> is defined to be: <pre> 
    *  (e.getKey()==null ? 0 : e.getKey().hashCode())^
    *  (e.getValue()==null ? 0 : e.getValue().hashCode()) 
     * </pre> 
    * This ensures that <tt>e1.equals(e2)</tt> implies that 
    * <tt>e1.hashCode()==e2.hashCode()</tt> for any two Entries 
    * <tt>e1</tt> and <tt>e2</tt>, as required by the general 
    * contract of <tt>Object.hashCode</tt>. 
    * 
    * @return the hash code value for this map entry 
    * @see Object#hashCode() 
    * @see Object#equals(Object) 
    * @see #equals(Object) 
    */ 
    int hashCode(); 
    } 

इंटरफेस के बारे में अधिक जानकारी के लिए Interfaces ट्यूटोरियल और यह Static Nested Interfaces लेख देखें।

2

उदाहरण:

public class Outer { 
    public interface Bar { 
     Bar get(); 
    } 
} 

बार एक नेस्टेड इंटरफेस है। नेस्टेड इंटरफेस डिफ़ॉल्ट रूप से स्थिर हैं, तो आप के रूप में अच्छी लिख सकते हैं:

public class Outer { 
    public static interface Bar { 
     Bar get(); 
    } 
} 

अब, इस संदर्भ में क्या मतलब है कि स्थिर इंटरफ़ेस एक स्थिर सदस्य, वर्ग के एक सदस्य अर्थात है।

साथ ही आप वर्गों के साथ ऐसा कर सकते हैं:

public class Tree { 
    private static class Node { 

    } 
} 

यहाँ, नोड यहां तक ​​कि निजी है, जिसका अर्थ यह पेड़ के भीतर ही दिखाई दे। तो, इसका क्या फायदा है? नोड को सार्वजनिक कक्षा क्यों नहीं बनाते? बेहतर encapsulation की वजह से। सबसे पहले, नोड पेड़ का कार्यान्वयन विवरण है, इसलिए आप इसे दिखाना नहीं चाहते हैं। दूसरा, यदि आप सार्वजनिक एपीआई के माध्यम से नोड का पर्दाफाश करते हैं, तो कुछ क्लाइंट (प्रोग्रामर) इसका इस्तेमाल अपने कोड में कर सकते हैं। अब, इस वर्ग पर उनकी कठोर निर्भरता है। यदि किसी बिंदु पर आप अपने पेड़ का प्रतिनिधित्व बदलना चाहते हैं, और आप नोड क्लास को बदल/हटाते हैं, तो क्लाइंट कोड टूट सकता है। और अंतिम लेकिन कम से कम नहीं, आपका सार्वजनिक एपीआई छोटा हो जाता है, जो भी वांछनीय है।

तो, स्थिर सदस्य वर्ग/इंटरफेस का उपयोग कब करें? अधिकतर, यदि आप किसी प्रकार की समग्र वस्तु (जैसे पेड़, या एक लिंक्ड लिस्ट) बनाते हैं या जब वर्ग केवल बाहरी वर्ग के संदर्भ में समझ में आता है।

0

जावा nested interfaces की अनुमति देता है। आप उन्हें कक्षाओं या इंटरफेस में घोंसला कर सकते हैं। उदाहरण के लिए, Map.EntryMap इंटरफ़ेस में परिभाषित एक नेस्टेड इंटरफ़ेस है।

Map कार्यान्वयन (TreeMap, HashMap) Map.Entry के निजी कार्यान्वयन, जो कक्षा के बाहर दिखाई नहीं देते हैं प्रदान करते हैं।

Bohemian's answer पते Map.Entry का उपयोग कैसे करें।

0

आंतरिक इंटरफेस पूरी तरह से सार्वजनिक और स्थैतिक हैं।

आप इस प्रकार आंतरिक इंटरफेस हो सकता है:

1. interface A { 
      ..... 
      ..... 
      interface B { 
          .... 
          .... 
      } 

    } 



2. class A { 
       .... 
       .... 
       interface B { 
          .... 
          .... 
       } 

    } 

आप a.b से ऊपर भीतरी इंटरफेस (बी) का उपयोग कर सकते हैं जहां एक कक्षा या इसके बाद के संस्करण दो मामलों के अनुसार एक अंतरफलक है।

उदाहरण के लिए,

class x implements A.B 
{ 
     .... 
     .... 
} 
संबंधित मुद्दे