Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #38519

Is there a better way to group and sort objects?

Newsgroups comp.lang.java.programmer
Date 2018-08-07 06:00 -0700
Message-ID <8c045ff4-98c5-4253-9136-b26b34cf8f77@googlegroups.com> (permalink)
Subject Is there a better way to group and sort objects?
From Eric Douglas <e.d.programmer@gmail.com>

Show all headers | View raw


I want to combine and sort objects by key properties and merge their secondary properties.  Is there a better way?  This is what I came up with..

public abstract class TestStatic {
	private static class TestClass extends Object implements Comparable<Object> {
		private int var1;
		private int var2;

		@Override
		public int compareTo(Object compareMe) {
			if (compareMe == null || compareMe.getClass() != getClass()) {
				return 1;
			}
			return var1 - ((TestClass) compareMe).getVar1();
		}

		@Override
		public boolean equals(Object compareMe) {
			if (compareMe == null || compareMe.getClass() != getClass()) {
				return false;
			}
			if (var1 != ((TestClass) compareMe).getVar1()) {
				return false;
			}
			return true;
		}
		
		public String toString() {
			return String.valueOf(var1);
		}

		public void setVar1(int value) {
			var1 = value;
		}

		public int getVar1() {
			return var1;
		}

		public void setVar2(int value) {
			var2 = value;
		}

		public int getVar2() {
			return var2;
		}
	}
	public static void main(final String[] args) {
		HashMap<String,TestClass> hm = new HashMap<>();
		TestClass test1 = new TestClass();
		test1.setVar1(3);
		test1.setVar2(12);
		hm.put(test1.toString(),test1);
		TestClass test2 = new TestClass();
		test2.setVar1(4);
		if (hm.containsKey(test2.toString())) {
			test2 = hm.get(test2.toString());
			test2.setVar2(test2.getVar2() + 42);
		} else {
			test2.setVar2(42);
			hm.put(test2.toString(), test2);
		}
		TestClass test3 = new TestClass();
		test3.setVar1(3);
		if (hm.containsKey(test3.toString())) {
			test3 = hm.get(test3.toString());
			test3.setVar2(test3.getVar2() + 55);
		} else {
			test3.setVar2(55);
			hm.put(test3.toString(), test3);
		}
		ArrayList<TestClass> al = new ArrayList<>();
		al.addAll(hm.values());
		Collections.sort(al);
		for (TestClass t1 : al) {
			System.out.println(t1.getVar1() + " " + t1.getVar2());
		}
	}
}

Back to comp.lang.java.programmer | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Is there a better way to group and sort objects? Eric Douglas <e.d.programmer@gmail.com> - 2018-08-07 06:00 -0700
  Re: Is there a better way to group and sort objects? Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-08-07 09:44 -0400
    Re: Is there a better way to group and sort objects? Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-08-07 11:33 -0400
    Re: Is there a better way to group and sort objects? Eric Douglas <e.d.programmer@gmail.com> - 2018-08-07 10:22 -0700
      Re: Is there a better way to group and sort objects? Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-08-07 14:16 -0400
        Re: Is there a better way to group and sort objects? Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-08-07 14:36 -0400
        Re: Is there a better way to group and sort objects? Eric Douglas <e.d.programmer@gmail.com> - 2018-08-07 12:13 -0700
          Re: Is there a better way to group and sort objects? Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-08-07 16:00 -0400
  Re: Is there a better way to group and sort objects? Martin Gregorie <martin@mydomain.invalid> - 2018-08-08 08:20 +0000
    Re: Is there a better way to group and sort objects? Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-08-08 06:57 -0400
      Re: Is there a better way to group and sort objects? Martin Gregorie <martin@mydomain.invalid> - 2018-08-08 11:08 +0000
      Re: Is there a better way to group and sort objects? Martin Gregorie <martin@mydomain.invalid> - 2018-08-08 11:22 +0000
        Re: Is there a better way to group and sort objects? Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-08-08 07:45 -0400
          Re: Is there a better way to group and sort objects? Martin Gregorie <martin@mydomain.invalid> - 2018-08-08 14:12 +0000
        Re: Is there a better way to group and sort objects? Eric Douglas <e.d.programmer@gmail.com> - 2018-08-08 05:50 -0700
          Re: Is there a better way to group and sort objects? Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-08-08 09:23 -0400
            Re: Is there a better way to group and sort objects? Eric Douglas <e.d.programmer@gmail.com> - 2018-08-08 06:47 -0700
              Re: Is there a better way to group and sort objects? Eric Sosman <esosman@comcast-dot-net.invalid> - 2018-08-08 10:31 -0400
                Re: Is there a better way to group and sort objects? Eric Douglas <e.d.programmer@gmail.com> - 2018-08-08 08:24 -0700
          Re: Is there a better way to group and sort objects? Robert Klemme <shortcutter@googlemail.com> - 2018-08-14 14:06 +0200
            Re: Is there a better way to group and sort objects? Eric Douglas <e.d.programmer@gmail.com> - 2018-08-14 05:49 -0700
  Re: Is there a better way to group and sort objects? Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2018-08-09 16:06 +0200
    Re: Is there a better way to group and sort objects? Eric Douglas <e.d.programmer@gmail.com> - 2018-08-14 05:47 -0700

csiph-web