Adding collections framework(Comparator and Comparable)

parents
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/Assignment_6.iml" filepath="$PROJECT_DIR$/Assignment_6.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="5b0f06d3-5d39-44e0-a1a7-e6f1998316df" name="Changes" comment="">
<change afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/modules.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/uiDesigner.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Assignment_6.iml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/MainSort.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/SortName.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/Student.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/StudentCompare.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/Test.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Class" />
</list>
</option>
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="MarkdownSettingsMigration">
<option name="stateVersion" value="1" />
</component>
<component name="ProjectId" id="29I8WNbs1UitDIcbJYJGk8REGhM" />
<component name="ProjectLevelVcsManager" settingsEditedManually="true" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"SHARE_PROJECT_CONFIGURATION_FILES": "true"
}
}]]></component>
<component name="RunManager" selected="Application.MainSort">
<configuration name="MainSort" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="MainSort" />
<module name="Assignment_6" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="MyList" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="MyList" />
<module name="Assignment_6" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="MyQueue" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="MyQueue" />
<module name="Assignment_6" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<recent_temporary>
<list>
<item itemvalue="Application.MainSort" />
<item itemvalue="Application.MyList" />
<item itemvalue="Application.MyQueue" />
</list>
</recent_temporary>
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="5b0f06d3-5d39-44e0-a1a7-e6f1998316df" name="Changes" comment="" />
<created>1652789272465</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1652789272465</updated>
</task>
<servers />
</component>
<component name="Vcs.Log.Tabs.Properties">
<option name="TAB_STATES">
<map>
<entry key="MAIN">
<value>
<State />
</value>
</entry>
</map>
</option>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
import java.util.*;
public class MainSort {
public static void main(String[]args) {
// // Part 3. Sort objects in an ArrayList.
// Student s1 = new Student("qadeer","123jkl",23);
// Student s2 = new Student("naveed","456jkl",26);
// Student s3 = new Student("sahal","678jkl",22);
// Student s4 = new Student("sahal","111jkl",1);
// Student s5 = new Student("sahal","213jkl",2);
// Student s6 = new Student("sahal","132jkl",3);
//
// ArrayList<Student> studentList=new ArrayList<>();
// studentList.add(s1);
// studentList.add(s2);
// studentList.add(s3);
// studentList.add(s4);
// studentList.add(s5);
// studentList.add(s6);
//
// System.out.println("Unsorted");
//
// for (Student value : studentList) {
// System.out.println(value);
// }
//
// Collections.sort(studentList, new SortName());
//
// System.out.println("After Sorting");
// for (Student student : studentList) {
// System.out.println(student);
// }
// using Comparable
StudentCompare s1 = new StudentCompare("qadeer", "sfsdf123", 23);
StudentCompare s2 = new StudentCompare("naveed", "sfsdf456", 21);
StudentCompare s3 = new StudentCompare("aSahal", "sfsdf678", 2);
StudentCompare s4 = new StudentCompare("sahalj", "sfsdf678", 1);
StudentCompare s5 = new StudentCompare("sahaldgd", "sfsdf678", 3);
ArrayList<StudentCompare> studentCompareList = new ArrayList<>();
studentCompareList.add(s1);
studentCompareList.add(s2);
studentCompareList.add(s3);
studentCompareList.add(s4);
studentCompareList.add(s5);
Collections.sort(studentCompareList);
for (StudentCompare student: studentCompareList) {
System.out.println(student);
}
}
}
import java.util.*;
import java.util.Comparator;
public class MyList {
public static void main(String[] args) {
// Part 1. Search item in a ArrayList.
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(32);
numbers.add(12);
numbers.add(100);
numbers.add(54);
numbers.add(28);
numbers.sort(Comparator.naturalOrder());
// Searching for an item. (returns index as int)
// Using indexOf() method
System.out.println(numbers.indexOf(32));
// Using contains() method. (returns boolean)
System.out.println(numbers.contains(32));
// Part 2. Search last item in an ArrayList and a LinkedList.
// Compare search times.
List<Integer> myArrayList = new ArrayList<>();
List<Integer> myLinkedList = new LinkedList<>();
Collections.addAll(myArrayList, 23, 44, -9, 14, 123, 56, 223, 445, 33, 45, 89, 1, 0, 21, 100);
Collections.addAll(myLinkedList, 23, 44, -9, 14, 123, 56, 223, 445, 33, 45, 89, 1, 0, 21, 100);
// code snippet to determine code execution time.
int item = 100;
System.out.println("Time for ArrayList");
long start = System.nanoTime();
for (int iter: myArrayList) {
if (iter == item) {
break;
}
}
long end = System.nanoTime();
long elapsedTime = end - start;
System.out.println(elapsedTime);
System.out.println("Time for LinkedList");
long start1 = System.nanoTime();
for (int iter: myLinkedList) {
if (iter == item) {
break;
}
}
long end1= System.nanoTime();
long elapsedTime1 = end1 - start1;
System.out.println(elapsedTime1);
}
}
import java.util.Comparator;
class SortName implements Comparator<Student> {
// SortAge sortByAge = new SortAge();
public int compare(Student a, Student b) {
if (a.name == b.name) {
// return sortByAge.compare(a, b);
return a.age - b.age;
}
return a.name.compareTo(b.name);
}
}
public class Student {
public String name;
public String id;
public int age;
Student(String name, String id, int age) {
this.name = name;
this.id = id;
this.age = age;
}
public String toString() {
return "id: " + this.id + " name: " + this.name + " age: " + this.age;
}
}
public class StudentCompare implements Comparable<StudentCompare> {
String name;
String id;
int age;
StudentCompare(String name, String id, int age) {
this.name = name;
this.id = id;
this.age = age;
}
public String toString() {
return "id: " + this.id + " name: " + this.name + " age: " + this.age;
}
@Override
public int compareTo(StudentCompare st) {
if (name == st.name) {
return age - st.age;
}
return name.compareTo(st.name);
}
}
import java.util.*;
public class Test {
public static void main(String[] args) {
// System.out.println(compare());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment