java - Maping key values from arrays of values where time complexity matters -


I have two such arrays.

  long key 1 = 1 L; Long key 2 = 2 l; Long key 3 = 3 l; Long key 4 = 4 l; Long key 5 = 2 L; Long key 6 = 3 l; Long key 7 = 1 l; Long key 8 = 2 l; Long key 9 = 4 l; Myobacter Ob1 = New MyObject (1l); MyObject ob2 = New MyObject (3l); MyObject obz = 3 new myobject (2l); MyObject ob4 = New MyObject (1l); MyObject Ob 5 = New MyObject (4L); MyObject OB6 = New MyObject (3L); MyObject ob7 = New MyObject (4l); MyObject ob8 = New MyObject (2l); MyObject ob9 = New MyObject (1l); Long [] key = new long [] {key1, key2, key3, key4, key5, key5, key7, key8, key9}; MyObject [] Objects = New MyObact [] {OB1, OB2, OB3, OB4, OM5, OB6, OB7, OM8, OB 9};   

I want to map the objects with the key. Therefore, each key will list the objects associated with it.

(more or less is my problem .. "A, Ax, Aero", "B" with "Box, Ball .. ATC" with map 'A')

I am successful by the way I am doing it, but the problem is complex of time, it is more than O (N * N) (I'm taking a key, comparing it with the keys of each item Yes.)

Does anyone help me to work less than the complexity of time (n * n). Cases of time complexity for me

Using JDK 1.6

  My program: import java.util.ArrayList; Import java.util.HashMap; Import java.util.list; Import java.util.Map; Public Class Bar {Long 1 = 1 L; Long key 2 = 2 l; Long key 3 = 3 l; Long key 4 = 4 l; Long key 5 = 2 L; Long key 6 = 3 l; Long key 7 = 1 l; Long key 8 = 2 l; Long key 9 = 4 l; Myobacter Ob1 = New MyObject (1l); MyObject ob2 = New MyObject (3l); MyObject ob3 = New MyObject (2l); MyObject ob4 = New MyObject (1l); MyObject ob5 = New MyObject (4l); MyObject OB6 = New MyObject (3L); MyObject ob7 = New MyObject (4l); MyObject ob8 = New MyObject (2l); MyObject ob9 = New MyObject (1l); Long [] key = new long [] {key1, key2, key3, key4, key5, key5, key7, key8, key9}; MyObject [] Objects = New MyObact [] {OB1, OB2, OB3, OB4, OB5, OB6, OB7, OM8, OB 9}; Public Zero Mapper () {map & lt; Long, list & lt; MyObject & gt; & Gt; KeyToObjectMap = new hashmap & lt; Long, list & lt; MyObject & gt; & Gt; (); {For (iTunes i.ici; i & lt; keys.length; i ++) {for (MyObject object: objects) {if (keyToObjectMap.containsKey (keys [i])) {if (keys [i] .equals (object.getKey))} {List & lt; MyObject & gt; Objs = keyToObjectMap.get (keys [i]); If (objs.contains (object)) {objs.add (object); } KeyToObjectMap.put (keys [i], objs); }} And if (key [i] .equals (object.getKey ())) {list & lt; MyObject & gt; Objs = new arreelist & lt; MyObject & gt; (); Objs.add (object); KeyToObjectMap.put (keys [i], objs); }}}} Public static zero main (string [] args) {bar b = new bar (); B.mapper (); }} Class MyObject {long key; String description = "description"; Myobacter (Long) {this.key = key; } Public Long Match () {Return Key; }}    

You can do this in (o). Maps & lt; Long, set & lt; MyObject & gt; & Gt; KeyToObjectMap = new hashmap & lt; & Gt; (); (MyObject O: Objects) {Set & lt; MyObject & gt; Set = kate to objectmap.get (ogateke)); If (set == empty) keyToObjectMap.put (o.getKey (), set = new hashset & lt;> ()); Set.add (o); }

In Java 8 you can

  Maps & lt; Long, set & lt; MyObject & gt; & Gt; Map = objects.stream () .collect (collectivrographing by (MyObact :: Mille, collectors.Tuset ());    

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -