As the title, I'm looking for a built-in Collection in java that can reorder after changing an element. For example, let Col is the Collection expected containing ordered integer elements: [0, 2, 4, 6, 8, 9, 11]. Now we change the 2nd element as 1, e.g 4 to 1. After that the Collection will accept the change and update the order of elements as [0, 1, 2, 6, 8, 9, 11]. I know some java collections that can keep the elements order after adding a new element but is not auto-update or very expensive after changing an element.  So I'm very grateful if you can give me any suggestion about this issue. Thank you
115801
I'm a beginner in Java.  Please suggest which collection(s) can/should be used for maintaining a sorted list in Java. I have tried Map and Set, but they weren't what I was looking for.
95623
How can I convert a List&lt;MyObject&gt; to an IEnumerable&lt;MyObject&gt; and then back again?  I want to do this in order to run a series of LINQ statements on the List, e. g. Sort()
67149
I'm trying to create an array of arrays that will be using repeated data, something like below:  int[] list1 = new int[4] { 1, 2, 3, 4 }; int[] list2 = new int[4] { 5, 6, 7, 8 }; int[] list3 = new int[4] { 1, 3, 2, 1 }; int[] list4 = new int[4] { 5, 4, 3, 2 };  int[,] lists = new int[4, 4] {  list1 ,  list2 ,  list3 ,  list4  };   I can't get it to work and so I'm wondering if I'm approaching this wrong.   What I'm attempting to do is create some sort of method to create a long list of the values so I can process them in a specific order, repeatedly. Something like,  int[,] lists = new int[90,4] { list1, list1, list3, list1, list2, (and so on)};  for (int i = 0; i &lt; 90; ++i) {    doStuff(lists[i]); }   and have the arrays passed to doStuff() in order. Am I going about this entirely wrong, or am I missing something for creating the array of arrays?
110869
A common need when using a database is to access records in order. For example, if I have a blog, I want to be able to reorder my blog posts in arbitrary order. These entries often have lots of relationships, so a relational database seems to make sense.  The common solution that I have seen is to add an integer column order:  CREATE TABLE AS your_table (id, title, sort_order) AS VALUES   (0, 'Lorem ipsum',   3),   (1, 'Dolor sit',     2),   (2, 'Amet, consect', 0),   (3, 'Elit fusce',    1);   Then, we can sort the rows by order to get them in the proper order.  However, this seems clumsy:   If I want to move record 0 to the start, I have to reorder every record If I want to insert a new record in the middle, I have to reorder every record after it If I want to remove a record, I have to reorder every record after it   It's easy to imagine a situations like:   Two records have the same order There are gaps in the order between records   These could happen fairly easily for a number of reasons.  This is the approach that applications like Joomla take:    You could argue that the interface here is bad, and that instead of humans directly editing numbers, they should use arrows or drag-and-drop—and you'd probably be right. But behind the scenes, the same thing is happening.  Some people have proposed using a decimal to store order, so that you can use "2.5" to insert a record in between the records at order 2 and 3. And while that helps a little, it's arguably even messier because you can end up with weird decimals (where do you stop? 2.75? 2.875? 2.8125?)  Is there a better way to store order in a table?
24747
I've got a large array of primitive types (double). How do I sort the elements in descending order?  Unfortunately the Java API doesn't support sorting of primitive types with a Comparator.  The first approach that probably comes to mind is to convert it to a list of objects (boxing):  double[] array = new double[1048576];     Arrays.stream(array).boxed().sorted(Collections.reverseOrder())…   However, boxing each primitive in the array is too slow and causes a lot of GC pressure!  Another approach would be to sort and then reverse:  double[] array = new double[1048576]; ... Arrays.sort(array); // reverse the array for (int i = 0; i &lt; array.length / 2; i++) {      // swap the elements      double temp = array[i];      array[i] = array[array.length - (i + 1)];      array[array.length - (i + 1)] = temp; }   This approach is also slow - particularly if the array is already sorted quite well.  What's a better alternative?
77231
I recently came across an algorithm that works on values assuming that they are draw from a monoid equipped with a total ordering relation.  I was wondering if there is a term for such a structure, since it seems related to concepts like Euclidean domains and fields (though the requirements are much less strict).  Does this entity have a name?  Or is it just "a monoid over totally ordered elements?"  Thanks!
31102
The example below throws an InvalidOperationException, "Collection was modified; enumeration operation may not execute." when executing the code.   var urls = new List&lt;string&gt;(); urls.Add("http://www.google.com");  foreach (string url in urls) {     // Get all links from the url     List&lt;string&gt; newUrls = GetLinks(url);      urls.AddRange(newUrls); // &lt;-- This is really the problematic row, adding values to the collection I'm looping }   How can I rewrite this in a better way? I'm guessing a recursive solution?
282
Conditions: do not modify the original lists; JDK only, no external libraries. Bonus points for a one-liner or a JDK 1.3 version. Is there a simpler way than: List&lt;String&gt; newList = new ArrayList&lt;String&gt;(); newList.addAll(listOne); newList.addAll(listTwo);
112859
Is there a better way to do this?  string[] s = {"zero", "one", "two", "three", "four", "five"};  var x =  s .Select((a,i) =&gt; new {Value = a, Index = i}) .Where(b =&gt; b.Value.StartsWith("t")) .Select(c =&gt; c.Index);   i.e. I'm looking for a more efficient or more elegant way to get the positions of the items matching the criteria.
38365
How can I left-align the numbers in an ordered list?  1.  an item // skip some items for brevity  9.  another item 10. notice the 1 is under the 9, and the item contents also line up   Change the character after the number in an ordered list?  1) an item   Also is there a CSS solution to change from numbers to alphabetic/roman lists instead of using the type attribute on the ol element.  I am mostly interested in answers that work on Firefox 3.
11681
I am looking for a name of one specific UI element. I have unsuccessful tried to identify it before but this attempt failed.   What I am looking for is an n-to-n selector where items move between two states. Lets say you have two buckets (states), one on the left, one on the right. There are multiple items in the left bucket and you can move them to the right bucket and back.  What is the name of this element? How is it different from normal list where you can select multiple items?
79800
Is there any book that explicitly contain the convention that a representation of the set that contain repeated element is the same as the one without repeated elements?  Like $\{1,1,2,3\} = \{1,2,3\}$.  I have looked over a few books and it didn't mention such thing. (Wikipedia has it, but it does not cite source).  In my years learning mathematics in both US and Hungary, this convention is known and applied. However recently I noticed some Chinese students claim they have never seen this before, and I don't remember I saw it in any book either.   I never found a book explicitly says what are the rules in how $\{a_1,a_2,a_3,\ldots,a_n\}$ specify a set. Some people believe it can only specify a set if $a_i\neq a_j \Leftrightarrow i\neq j$. The convention shows that doesn't have to be satisfied.
51825
I have the name of the "sort by property" in a string. I will need to use Lambda/Linq to sort the list of objects.  Ex:  public class Employee {   public string FirstName {set; get;}   public string LastName {set; get;}   public DateTime DOB {set; get;} }   public void Sort(ref List&lt;Employee&gt; list, string sortBy, string sortDirection) {   //Example data:   //sortBy = "FirstName"   //sortDirection = "ASC" or "DESC"    if (sortBy == "FirstName")   {     list = list.OrderBy(x =&gt; x.FirstName).toList();       }  }    Instead of using a bunch of ifs to check the fieldname (sortBy), is there a cleaner way of doing the sorting Is sort aware of datatype?
36708
Is there a built in way to limit the depth of a System.Collection.Generics.Stack? So that if you are at max capacity, pushing a new element would remove the bottom of the stack?  I know I can do it by converting to an array and rebuilding the stack, but I figured there's probably a method on it already.  EDIT: I wrote an extension method:      public static void Trim&lt;T&gt; (this Stack&lt;T&gt; stack, int trimCount)      {         if (stack.Count &lt;= trimCount)             return;         stack = new              Stack&lt;T&gt;             (                 stack                     .ToArray()                     .Take(trimCount)             );                }   So, it returns a new stack upon trimming, but isn't immutability the functional way =)  The reason for this is that I'm storing undo steps for an app in a stack, and I only want to store a finite amount of steps.
90875
How would I go about reordering divs without altering the HTML source code?  example, I want divs to appear in order #div2, #div1, #div3, but in the HTML they are:  &lt;div id="#div1"&gt;&lt;/div&gt;   &lt;div id="#div2"&gt;&lt;/div&gt;   &lt;div id="#div3"&gt;&lt;/div&gt;   Thanks!
117983
I am trying to remove duplicate and creating a set and list with non duplicate values related to field ord__c . Trying to minimize the code as far as possible. Let me know if I can handle the same using the below piece of code I am using :   Set &lt; Id &gt; OrId = new Set &lt; Id &gt; ();  Integer j = 0;         while (j &lt; ListR.size())         {           OrId.add(ListR.get(j).Ord__c);           if(OrId.contains(ListR.get(j).Ord__c))           {             ListR.remove(j);           }           else           {             j++;           }         }
96022
I had the following exception thrown at me when trying to process more than one selected records via a custom button in the list view.    Class Code  global class MerchandiseInventoryClass {      webservice static void ConverttoInventory(String[] merchandise_id)    {        //Convert Merchandise collection into Inventory Collection...         Merchandise__c[] merchandise_collection = [SELECT ID,Author__c,Book_Title__c,Edition__c,Publisher__c,Total_Merchandise_QtyTotal_Qty__c,Status__c FROM Merchandise__c WHERE ID = :merchandise_id];          Inventory__c i = new Inventory__c(); //Line 1        List&lt;Inventory__c&gt; newInventory = new List&lt;Inventory__c&gt;();        for(Merchandise__c m : merchandise_collection )        {            if(m.Status__c !='Fully Converted')            {            i.Author__c = m.Author__c ;            i.Book_Title__c = m.Book_Title__c;            i.Edition__c = m.Edition__c;            i.Publisher__c = m.Publisher__c;            i.Qty_Available__c = m.Total_Merchandise_QtyTotal_Qty__c;            i.Merchandise__c = m.Id;            m.Uninventoried_Qty__c = 0;            m.Status__c = 'Fully Converted';            newInventory.add(i);            }        }        insert newInventory;        update merchandise_collection;    }  }   By moving the Line 1 to within the for loop solved the issue for me.  Thanks to this post (refer the Best Answer in that post)    What puzzles me is that WHY i have to instantiate "i" for every iteration within the loop.  Why the list collection ("newInventory") is complaining that are duplicate elements and why should it even confuse in the first place when I am performing an "insert" even ?